0
votes

When i'm trying to initialize terraform, i'm getting following error only with vnet module, But terraform initilization working with azure_resource_group, azure_virtual_machine modules

Terraform 0.13 and earlier allowed provider version constraints inside the provider configuration block, but that is now deprecated and will be removed in a future version of Terraform. To silence this warning, move the provider version constraint into the required_providers block.

Error: Failed to query available provider packages

Could not retrieve the list of available versions for provider hashicorp/azure: provider registry registry.terraform.io does not have a provider named registry.terraform.io/hashicorp/azure

If you have just upgraded directly from Terraform v0.12 to Terraform v0.14 then please upgrade to Terraform v0.13 first and follow the upgrade guide for that release, which might help you address this problem.

Did you intend to use terraform-providers/azure? If so, you must specify that source address in each module which requires that provider. To see which modules are currently depending on hashicorp/azure,

enter image description here

enter image description here

main.tf

resource "azurerm_virtual_network" "vnet" {
  for_each = { for n in var.networks : n.name => n }
  name                = each.value.name
  address_space       = each.value.address_space
  location            = each.value.location
  resource_group_name = each.value.rg_name

  dynamic "subnet" {
    for_each = each.value.subnets
    content {
      name           = subnet.value.name
      address_prefix = subnet.value.address_prefixes
    }
  }
}

variables.tf
variable networks {
    type = list(object({
        name           = string
        address_space  = list(string)
        rg_name        = string
        location       = string
        subnets        = list(object({
                name             = string
                address_prefixes = string
            }))    
  }))
}

module (main.tf)
module "azurevnet"{
    source                  = "./vnet"
    networks                = var.networks
}

provider.tf

provider "azurerm" {
  version = "=2.37.0"
}
1

1 Answers

0
votes

As I know, the registry terraform-providers/azure is a deprecated provider. This model will not add new things anymore and Azure already change into ARM model. So I recommend you use the terraform-providers/azurerm model only and it supports more Azure features.

Update:

And use the azurerm model, the directory structure would look like this:

enter image description here

main.tf

module "azurevnet" {
    source      = "./vnet"
    networks    = var.networks
}

providers.tf

provider "azurerm" {
    features {}
    version = "=2.37.0"
}

vnet/main.tf

variable "networks" {}

resource "azurerm_virtual_network" "vnet" {
  for_each = { for n in var.networks : n.name => n }
  name                = each.value.name
  address_space       = each.value.address_space
  location            = each.value.location
  resource_group_name = each.value.rg_name

  dynamic "subnet" {
    for_each = each.value.subnets
    content {
      name           = subnet.value.name
      address_prefix = subnet.value.address_prefixes
    }
  }
}

I only give the code for VNet, but other resources will be in the same format. And you can also do not use the providers.tf file and put the content into the main.tf file.