如何在创建本地地图的同时迭代地形地图列表



我有一个变量,如下

subnets = [
{      
app_name = "app1" 
subnets = {
"us-west-1a" = ["10.85.1.128/26", "10.85.1.192/26", "10.85.2.128/26", "10.85.2.192/26", "10.85.3.64/26", "10.85.3.128/26", "10.85.3.192/26", "10.85.4.0/26", "10.85.4.64/26", "10.85.4.128/26", "10.85.4.192/26", "10.85.5.0/26", "10.85.5.64/26", "10.85.5.128/26", "10.85.6.64/26", "10.85.6.128/26", "10.85.6.192/26"]
"us-west-1b" = ["10.85.1.128/26", "10.85.1.192/26", "10.85.2.128/26", "10.85.2.192/26", "10.85.3.64/26", "10.85.3.128/26", "10.85.3.192/26", "10.85.4.0/26", "10.85.4.64/26", "10.85.4.128/26", "10.85.4.192/26", "10.85.5.0/26", "10.85.5.64/26", "10.85.5.128/26", "10.85.6.64/26", "10.85.6.128/26", "10.85.6.192/26"]
"us-west-1c" = ["10.85.1.128/26", "10.85.1.192/26", "10.85.2.128/26", "10.85.2.192/26", "10.85.3.64/26", "10.85.3.128/26", "10.85.3.192/26", "10.85.4.0/26", "10.85.4.64/26", "10.85.4.128/26", "10.85.4.192/26", "10.85.5.0/26", "10.85.5.64/26", "10.85.5.128/26", "10.85.6.64/26", "10.85.6.128/26", "10.85.6.192/26"]
}
},
{
app_name = "app2"
subnets = {
"us-west-1a" = ["10.85.1.128/26", "10.85.1.192/26", "10.85.2.128/26", "10.85.2.192/26", "10.85.3.64/26", "10.85.3.128/26", "10.85.3.192/26", "10.85.4.0/26", "10.85.4.64/26", "10.85.4.128/26", "10.85.4.192/26", "10.85.5.0/26", "10.85.5.64/26", "10.85.5.128/26", "10.85.6.64/26", "10.85.6.128/26", "10.85.6.192/26"]
"us-west-1b" = ["10.86.1.128/26", "10.86.1.192/26", "10.86.2.128/26", "10.86.2.192/26", "10.86.3.64/26", "10.86.3.128/26", "10.86.3.192/26", "10.86.4.0/26", "10.86.4.64/26", "10.86.4.128/26", "10.86.4.192/26", "10.86.5.0/26", "10.86.5.64/26", "10.86.5.128/26", "10.86.6.64/26", "10.86.6.128/26", "10.86.6.192/26"]
}
}
]

我想创建一个本地应该给出如下的值

{app_name = app1
cidr_block = "10.85.1.128/26"
az = us-west-1a}
{app_name = app1
cidr_block = "10.85.1.192/26"
az = us-west-1a}
.............
{app_name = app2
cidr_block = "10.85.1.128/26"
az = us-west-1a}
{app_name = app2
cidr_block = "10.86.1.128/26"
az = us-west-1b}

我试着像下面这样。但我无法拆分列表并再进行1次迭代

locals {
int_subnets = toset(flatten([
for app in var.subnets: [
for az,cidr in app.subnets : {
app_name = app.app_name
cidr_block = cidr
az = az
}
]]))
resource "aws_subnet" "example" {
for_each = {
for s in local.int_subnets : "${s.app_name} ${s.cidr_block}" => s
}
vpc_id            = var.vpc_id
cidr_block        = each.value.cidr_block
availability_zone = each.value.az
tags = {
Name = "${each.value.app_name}-Int-${split("-", each.value.az)[2]}"
}
}
}

低于错误

on main.tf line 49, in resource "aws_subnet" "example":
│   49:     for s in local.int_subnets : "${s.app_name} ${s.cidr_block}" => s
│     ├────────────────
│     │ s.cidr_block is tuple with 17 elements
│ 
│ Cannot include the given value in a string template: string required.

请让我知道是否有可能再进行1次迭代,并将cidr块列表拆分为字符串,或者我们对相同的有其他更好的方法吗

我相信你可能想要这样的东西:

locals {
int_subnets = toset(flatten([
for app in var.subnets : [
for az in keys(app.subnets) : [
for subnet in flatten(app.subnets[az]) : [
{ "app_name" : app.app_name,
"az" : az,
"cidr_block " : subnet
}
]
]
]
]))
}

这将产生以下格式的输出:

int_subnet = ([
{
"app_name" = "app1"
"az" = "us-west-1a"
"cidr_block " = "10.85.1.128/26"
},
{
"app_name" = "app1"
"az" = "us-west-1a"
"cidr_block " = "10.85.1.192/26"
},
...
{
"app_name" = "app2"
"az" = "us-west-1a"
"cidr_block " = "10.85.1.128/26"
},
{
"app_name" = "app2"
"az" = "us-west-1a"
"cidr_block " = "10.85.1.192/26"
},
...

最新更新