【发布时间】:2021-11-09 03:37:55
【问题描述】:
我有一个像下面这样的变量
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 阻止列表拆分为字符串,或者我们是否有其他更好的方法
【问题讨论】:
-
您的“一个应该给出如下价值的本地人”的例子并不完全清楚。您想为每个 AZ 或所有 AZ 拥有第一个 CIDR?即您的示例在 app1 和 app2 中具有不同的值
标签: terraform