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
address_space       = var.vnet_address_space

terraform.tfvars:

vnet_name             = "VirtualNetwork"
vnet_address_space    = "<ip>"
subnet1_address_prefix="<ip1>"
subnet2_address_prefix="<ip2>"
subnet3_address_prefix="<ip3">
In the vnet module, I have the below code:

resource "azurerm_virtual_network" "vnet" {
  name                = var.vnet_name
 location            = var.loc
 resource_group_name = var.rgname
 address_space       = var.vnet_address_space

} 
#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       = var.subnet1_address_prefix   
}
#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       = var.subnet2_address_prefix 

  
}
#Subnet Creation
resource "azurerm_subnet" "subnet-3" {
  name= var.subnet_name3
  name= var.subnet_name1
   resource_group_name  = var.rgname
   virtual_network_name = azurerm_virtual_network.vnet.name
   address_prefix       = var.subnet3_address_prefix 

  
}

I am getting the below error :

address_space = var.vnet_address_space |---------------- | var.vnet_address_space is "" Inappropriate value for attribute "address_space": list of string required.*

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

Your current var.vnet_address_space is just a string:

vnet_address_space    = "<ip>"

However address_space should be a list of strings, not a single string only.

The easiest way to change it to list of strings would be:

vnet_address_space    = ["<ip>"]

Alternatively, you can do it as follows:

resource "azurerm_virtual_network" "vnet" {
 name                = var.vnet_name
 location            = var.loc
 resource_group_name = var.rgname
 address_space       = [var.vnet_address_space]
}