Home > Blockchain >  Iterating through map of objects in terraform
Iterating through map of objects in terraform

Time:04-26

I have a module named network resources with the below code which is used for creating multiple vnets and subnets( vnets are getting created and under that subnets are getting created):

variables.tf

variable "resource_group_name" {
  description = "Name of the resource group to be imported."
  type        = string
}

variable "location" {
  description = "The location of the vnet to create. Defaults to the location of the resource group."
  type        = string
  default     = null
}

variable "vnets" {
  type = map(object({
    address_space = string
    subnets = list(object({
      subnet_name    = string
      subnet_address = string
      service_endpoints = list(string)
    }))
  }))

  default = {
    "bupavnet1" = {
      address_space = "192.168.0.0/16",
      subnets       = []
    },
    "bupavnet2" = {
      address_space = "10.0.0.0/16",
      subnets = [
        {
          subnet_name    = "subnet1_bupavnet1"
          subnet_address = "10.0.2.0/24"
          service_endpoints = []
        },
        {
          subnet_name    = "GatewaySubnet"
          subnet_address = "10.0.0.0/24"
          service_endpoints = ["Microsoft.AzureCosmosDB","Microsoft.ContainerRegistry"]
          
        }
      ]
    },

    "bupavnet3" = {
      address_space = "10.80.0.0/16"
      subnets = [
        {
          subnet_name    = "subnet1_bupavnet3"
          subnet_address = "10.80.2.0/24"
          service_endpoints = ["Microsoft.AzureCosmosDB","Microsoft.ContainerRegistry"]
        },

        {
          subnet_name    = "subnet2_bupavnet3"
          subnet_address = "10.80.1.0/24"
          service_endpoints = ["Microsoft.AzureCosmosDB","Microsoft.ContainerRegistry"]
        },
        {
          subnet_name    = "GatewaySubnet"
          subnet_address = "10.80.0.0/24"
          service_endpoints = ["Microsoft.AzureCosmosDB","Microsoft.ContainerRegistry"]
        },
      ]
    }
  }
}

locals.tf

locals {
  subnets_flatlist = flatten([for key, val in var.vnets : [
    for subnet in val.subnets : {
      vnet_name         = key
      subnet_name       = subnet.subnet_name
      subnet_address    = subnet.subnet_address
      service_endpoints = subnet.service_endpoints
    }
    ]
  ])
  subnets = { for subnet in local.subnets_flatlist : subnet.subnet_name => subnet }
}

main.tf

data "azurerm_resource_group" "network" {
  name = var.resource_group_name
}



resource "azurerm_virtual_network" "vnets" {
  for_each            = var.vnets
  name                = each.key
  resource_group_name = data.azurerm_resource_group.network.name
  location            = data.azurerm_resource_group.network.location
  address_space       = [each.value.address_space]
}

resource "azurerm_subnet" "subnets" {
  for_each             = local.subnets
  name                 = each.value.subnet_name
  resource_group_name  = data.azurerm_resource_group.network.name
  virtual_network_name = azurerm_virtual_network.vnets[each.value.vnet_name].name
  address_prefixes     = [each.value.subnet_address]
  service_endpoints    = each.value.service_endpoints
}

Now when I am calling the module using the below code:

resource "azurerm_resource_group" "rg2" {
  name     = "rg2"
  location = "Australia East"
}

module "network" {
  source = "./network_resources"
  resource_group_name = azurerm_resource_group.rg2.name
  location = azurerm_resource_group.rg2.location
}

I am getting the below error. Please can you let me know where I need to add the ellipsis

│ Error: Duplicate object key
│
│   on network_resources\locals.tf line 11, in locals:
│   11:   subnets = { for subnet in local.subnets_flatlist : subnet.subnet_name => subnet }
│     ├────────────────
│     │ subnet.subnet_name is "GatewaySubnet"

New error after adding ellipsis

 Error: Unsupported attribute
│
│   on network_resources\main.tf line 17, in resource "azurerm_subnet" "subnets":
│   17:   name                 = each.value.subnet_name
│     ├────────────────
│     │ each.value is tuple with 2 elements
│
│ This value does not have any attributes.
╵
╷
│ Error: Unsupported attribute
│
│   on network_resources\main.tf line 17, in resource "azurerm_subnet" "subnets":
│   17:   name                 = each.value.subnet_name
│     ├────────────────
│     │ each.value is tuple with 1 element
│
│ This value does not have any attributes.
╵
╷
│ Error: Unsupported attribute
│
│   on network_resources\main.tf line 17, in resource "azurerm_subnet" "subnets":
│   17:   name                 = each.value.subnet_name
│     ├────────────────
│     │ each.value is tuple with 1 element
│
│ This value does not have any attributes.
╵
╷
│ Error: Unsupported attribute
│
│   on network_resources\main.tf line 17, in resource "azurerm_subnet" "subnets":
│   17:   name                 = each.value.subnet_name
│     ├────────────────
│     │ each.value is tuple with 1 element
│
│ This value does not have any attributes.

CodePudding user response:

Please can you let me know where I need to add the ellipsis

They should be added as shown below:

  subnets = { for subnet in local.subnets_flatlist : subnet.subnet_name => subnet... }
  • Related