遍历 terraform 中的对象映射

Iterating through map of objects in terraform

我有一个名为网络资源的模块,其代码用于创建多个 vnet 和子网(正在创建 vnet 并在该子网下创建子网):

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
}

现在,当我使用以下代码调用模块时:

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
}

我收到以下错误。请让我知道我需要在何处添加省略号

│ 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"

添加省略号后出现新错误

 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.

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

应如下所示添加它们:

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