0
votes

I have written the below code for creating a virtual network in terraform azure: I have files in the root folder for main.tf,variables.tf and terraform.tfvars. I have got two modules for resource group and virtual network.

In the root folder I have the below code: main.tf

#Resource Group Creation
module "resource_group" {
  source     = "./modules/resource_group"
  rgname_var = var.resource_group
  loc_var    = var.location

}
#Virtual Network Creation
module "vnet" {
  source                = "./modules/vnet"
  loc                   = module.resource_group.loc_o
  rgname                = module.resource_group.rgname_o

terraform.tfvars:

vnet_name             = "VirtualNetwork"
vnet_address_space    = [<3 subnet ips>]

variables.tf:

variable "vnet_name" {
  default = ""
}

variable "vnet_address_space" {
  default = ""

}

In the vnet module, I have the below code:

resource "azurerm_virtual_network" "vnet" {
  name                = var.vnet_name
  count               = length(var.vnet_address_space)
  address_space       = var.vnet_address_space[count.index]
  location            = var.loc
  resource_group_name = var.rgname

} 
#Subnet Creation
resource "azurerm_subnet" "subnet-1" {
  name= var.subnet_name1
  resource_group_name  = var.rgname
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefix       = azurerm_virtual_network.vnet[0].vnet_address_sace

  
}
#Subnet Creation
resource "azurerm_subnet" "subnet-2" {
  name= var.subnet_name2
  resource_group_name  = var.rgname
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefix       = azurerm_virtual_network.vnet[1].vnet_address_sace

  
}
#Subnet Creation
resource "azurerm_subnet" "subnet-3" {
  name= var.subnet_name3
  resource_group_name  = var.rgname
  virtual_network_name = azurerm_virtual_network.vnet.name
  address_prefix       = azurerm_virtual_network.vnet[2].vnet_address_sace

  
}

I am getting the below error for three subnets:

19: address_prefix = azurerm_virtual_network.vnet[0].vnet_address_sace This object has no argument, nested block, or exported attribute named "vnet_address_sace".

I have tried giving one ip for virtual network address space at the first place,because it was working for single subnets. then the system was expecting list instead of string. Any idea how to set the values for virtual network?

1

1 Answers

1
votes

According to the azurerm_virtual_network cocs, the correct argument is address_space, not vnet_address_sace. So in the code, it should be:

azurerm_virtual_network.vnet[0].address_space

not

azurerm_virtual_network.vnet[0].vnet_address_sace