gpt4 book ai didi

azure - 迭代 terraform 中的对象 map

转载 作者:行者123 更新时间:2023-12-03 07:03:03 25 4
gpt4 key购买 nike

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

变量.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"]
},
]
}
}
}

本地人.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... }

关于azure - 迭代 terraform 中的对象 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71998250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com