gpt4 book ai didi

azure - 在 Terraform 中使用 for_each 创建多个子网时出现问题

转载 作者:行者123 更新时间:2023-12-03 05:19:36 28 4
gpt4 key购买 nike

上下文:这是我在下面的帖子中所做的事情的延续。

what is correct way of reference to value in object/map type value in terraform

目标:尝试在 terraform 中使用 for_each 创建循环子网

我的 terraform.tfvars.json:(只有我提到了与我面临的问题相关的变量)

 "subnets" : {
"Dev" :
[
{"gw_snet":{
"name" : "GatewaySubnet",
"address_prefixes" : ["10.1.1.0/24"]
},
"dns-snet" : {
"name" : "InboundDNSSubnet",
"address_prefixes" : ["10.1.2.0/24"]
},
"common_snet" : {
"name" : "Common",
"address_prefixes" : ["10.1.3.0/24"]
},
"clientdata_snet" : {
"name" : "ClientDataSubnet",
"address_prefixes" : ["10.1.4.0/20"]
}}
],
"Stage" :
[
{"gw_snet":{
"name" : "GatewaySubnet",
"address_prefixes" : ["10.2.1.0/24"]
},
"dns-snet" : {
"name" : "InboundDNSSubnet",
"address_prefixes" : ["10.2.2.0/24"]
},
"common_snet" : {
"name" : "Common",
"address_prefixes" : ["10.2.3.0/24"]
},
"clientdata_snet" : {
"name" : "ClientDataSubnet",
"address_prefixes" : ["10.2.4.0/20"]
}}
],
"Prod" :
[
{"gw_snet":{
"name" : "GatewaySubnet",
"address_prefixes" : ["10.3.1.0/24"]
},
"dns-snet" : {
"name" : "InboundDNSSubnet",
"address_prefixes" : ["10.3.2.0/24"]
},
"common_snet" : {
"name" : "Common",
"address_prefixes" : ["10.3.3.0/24"]
},
"clientdata_snet" : {
"name" : "ClientDataSubnet",
"address_prefixes" : ["10.3.4.0/20"]
}}
]
}

我的 vnet 创建代码:

resource "azurerm_virtual_network" "vnet" {
name = var.hub_vnet_name
location = azurerm_resource_group.rg[0].location
resource_group_name = azurerm_resource_group.rg[0].name
for_each = {for k,v in var.vnet_address_space: k=>v if k == "Dev"}
address_space = each.value
dns_servers = var.dns_servers
tags = {
environment = "${var.env}"
costcentre = "14500"
}
dynamic "ddos_protection_plan" {
for_each = local.if_ddos_enabled

content {
id = azurerm_network_ddos_protection_plan.ddos[0].id
enable = false
}
}
}

我正在尝试使用 for_each 创建子网,如下所示

  resource "azurerm_subnet" "mysubnet" {
for_each = {for k,v in var.subnets: k=>v if k == "Dev"}
name = each.value.name
address_prefixes = [each.value.address_prefixes]
virtual_network_name = var.hub_vnet_name
resource_group_name = var.resource_group_name
}

我得到的错误:

我的 terraform 计划中没有错误,它也没有创建 vnet,因为我的计划未经验证。

我的子网变量定义正确吗?

我猜下面的内容根本不起作用......访问此嵌套值的正确方法?

  name                 = each.value.name
address_prefixes = [each.value.address_prefixes]

请帮我找出问题

最佳答案

我认为这看起来比实际情况复杂得多。我相信,您所寻求的是lookup功能。只需在 map 中查找您的 var.env 即可。您当前的数据结构没有多大意义。我在这里将其展示为本地人,只有很少的人足以展示其结构。

locals {
subnets = {
"Dev" = [
{
"some_name_a" = {
name = "SomeOtherNameA",
address_prefixes = ["10.1.1.0/24"]
},
"some_name_b" = {
name = "SomeOtherNameB",
address_prefixes = ["10.1.2.0/24"]
}
}
],
"Stage" = [
{
"some_name_a" = {
name = "SomeOtherNameA",
address_prefixes = ["10.1.1.0/24"]
},
"some_name_b" = {
name = "SomeOtherNameB",
address_prefixes = ["10.1.2.0/24"]
}
}
]
}
}

因此,每个环境部分都是一个长度为一个对象的列表,其中每个网络名称都有一个 key ,您不需要容纳实际定义配置的对象。您需要的要简单得多。

locals {
subnets = {
"Dev" = [
{
name = "SomeOtherNameA",
address_prefixes = ["10.1.1.0/24"]
},
{
name = "SomeOtherNameB",
address_prefixes = ["10.1.2.0/24"]
}
],
"Stage" = [
{
name = "SomeOtherNameA",
address_prefixes = ["10.1.1.0/24"]
},
{
name = "SomeOtherNameB",
address_prefixes = ["10.1.2.0/24"]
}
]
}
}

在这种情况下,您可以使用:

resource "azurerm_subnet" "mysubnet" {
for_each = { for v in lookup(var.subnets, var.env, []) : v.name => v.address_prefixes }

name = each.key
address_prefixes = each.value
virtual_network_name = var.hub_vnet_name
resource_group_name = var.resource_group_name
}

或者更简单,根据您的数据:

locals {
subnets_simple = {
"Dev" = {
"SomeOtherNameA" = ["10.1.1.0/24"]
"SomeOtherNameB" = ["10.1.2.0/24"]
},
"Stage" = {
"SomeOtherNameA" = ["10.1.1.0/24"]
"SomeOtherNameB" = ["10.1.2.0/24"]
},
}
}

在这种情况下,您应该能够简单地使用:

resource "azurerm_subnet" "mysubnet" {
for_each = lookup(var.subnets, var.env, {})

name = each.key
address_prefixes = each.value
virtual_network_name = var.hub_vnet_name
resource_group_name = var.resource_group_name
}

关于azure - 在 Terraform 中使用 for_each 创建多个子网时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73351761/

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