Home > Software engineering >  How to add multiple address spaces while creating Azure VNET using Terraform
How to add multiple address spaces while creating Azure VNET using Terraform

Time:07-08

Objective: Trying to create Azure VNet (virtual network) with multiple address spaces with Terraform aka this vnet should be created with 4 address spaces

Code that I am using:

main.tf:

    #-------------------------------------
    # VNET Creation - Default is "true"
    #-------------------------------------
    resource "azurerm_virtual_network" "vnet" {
      name                = lower("vnet-${var.hub_vnet_name}-${var.location}")
      location            = var.location
      resource_group_name = var.resource_group_name
      address_space       = [var.vnet_address_space]
      dns_servers         = [var.dns_servers]
      tags                = merge({ "ResourceName" = 
lower("vnet-${var.hub_vnet_name}-${var.location}") }, var.tags, )

variable.tf

variable "vnet_address_space" {
  description = "The address space to be used for the Azure virtual network."
  default     = ["10.350.0.0/24","10.351.0.0/20","10.352.0.0/24","10.353.0.0/24"]
}

terraform.tfvars

"vnet_address_space":["10.250.0.0/24","10.251.0.0/20","10.252.0.0/24","10.253.0.0/24"]

Error I am getting:

Error:

 Incorrect attribute value type
│ 
│   on main.tf line 71, in resource "azurerm_virtual_network" "vnet":
│   71:   address_space       = [var.vnet_address_space]
│     ├────────────────
│     │ var.vnet_address_space is tuple with 4 elements
│ 
│ Inappropriate value for attribute "address_space": element 0: string required.

Please let me know what mistake I am doing. Thanks in advance.

CodePudding user response:

Your var.vnet_address_space is already a list. So it should be:

address_space       = var.vnet_address_space
  • Related