Terraform: Can't access each.value.name in for loop

191 views Asked by At

I've seen similar questions here, but nothing that addresses accessing a member. I'm guessing this is something easy and my mind just isn't working.

I have a nested for loop that is producing what seems to be the output I need, but when I try to use this in a resource, it says that "each.value" does not have a property corresponding with the objects in the list.

I understand what it means that it's a tuple with two elements, but I thought the point of iterating with a for_each was to access each element of the tuple.

Thanks in advance

Test variable and loops:

variable "test" {
  type = object({
    vnets = list(object({
      name          = string
      address-space = string
      created-by    = string
      create-date   = string
      
      subnets = list(object({
        name           = string
        address-prefix = string
        delegations    = list(object({
          name    = string
          actions = list(string)
        }))
      }))
    }))
  })
}

output test {
  value = { for vnet in var.test.vnets : vnet.name => 
           [ for subnet in vnet.subnets : subnet ]}
}

Output of test:

  + test = {
      + app = [
          + {
              + address-prefix = "10.50.1.0/24"
              + delegations    = [
                  + {
                      + actions = [
                          + "Microsoft.Network/virtualNetworks/subnets/action",
                        ]
                      + name    = "Microsoft.Web/hostingEnvironments"
                    },
                ]
              + name           = "ase"
            },
        ]
      + dmz = [
          + {
              + address-prefix = "10.51.1.0/24"
              + delegations    = []
              + name           = "app-gateway"
            },
          + {
              + address-prefix = "10.51.2.0/24"
              + delegations    = []
              + name           = "firewall"
            },
        ]
    }

Resource declaration:

resource "azurerm_subnet" "subnet" {
  for_each = { for vnet in var.network.vnets : vnet.name =>
              [ for subnet in vnet.subnets : subnet ]}

  name                 = lower(format("az-sn-web-%s-%s-%s-%s-%s", each.key, each.value.name, var.tags["Env"], var.rg.location, var.tags["Env-Serial"]))
  resource_group_name  = var.rg.name
  virtual_network_name = lower(format("az-vn-web-%s-%s-%s-%s", each.key, var.tags["Env"], var.rg.location, var.tags["Env-Serial"]))
  address_prefixes     = [each.value.address-prefix]

  dynamic "delegation" {
    for_each = toset(each.value.delegations)

    content {
      name = format("az-del-web-%s-%s-%s-%s", split("/", delegation.value.name)[1], var.tags["Env"], var.rg.location, var.tags["Env-Serial"])
      
      service_delegation {
        name    = delegation.value.name
        actions = delegation.value.actions
      }
    }
  }
}

Error:

│ Error: Unsupported attribute
│
│   on modules\network\main.tf line 20, in resource "azurerm_subnet" "subnet":
│   20:   name                 = lower(format("az-sn-web-%s-%s-%s-%s-%s", each.key, each.value.name, var.tags["Env"], var.rg.location, var.tags["Env-Serial"]))
│     ├────────────────
│     │ each.value is tuple with 2 elements
│
│ This value does not have any attributes.
0

There are 0 answers