我尝试在 Terraform 中创建嵌套循环来创建多个 aws TGW 表。以及我使用的每个表的多重传播和合并函数来创建字典,键是路由表 ID,值是传播 ID,然后用于每个在字典中循环。 代码
variable "tgw_route_tables" {
description = "Map of project names to configuration."
type = map(any)
default = {
TestEgypt = {
tgw_route_table_name = "",
association = "",
propagations = ["", "", ],
},
}
}
locals {
tgw_route_table_id = [aws_ec2_transit_gateway_route_table.transit_gateway_route_table[*].id]
propagation_map = [
for table_id in local.tgw_route_table_id : flatten([
for table in var.tgw_route_tables : [
for propagation in table.propagations : {
table_id = propagation
}
]
])
]
}
resource "aws_ec2_transit_gateway_route_table" "transit_gateway_route_table" {
transit_gateway_id = aws_ec2_transit_gateway.ENZA-TransitGateway-Ireland-PRD-001.id
for_each = var.tgw_route_tables
tags = {
"Name" = each.value.tgw_route_table_name
}
}
resource "aws_ec2_transit_gateway_route_table_association" "tgw_route_table_association" {
for_each = var.tgw_route_tables
transit_gateway_attachment_id = each.value.association
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.transit_gateway_route_table[each.key].id
}
resource "aws_ec2_transit_gateway_route_table_propagation" "tgw_route_table_propagation" {
for_each = local.propagation_map
transit_gateway_attachment_id = each.value
transit_gateway_route_table_id = each.key
}
但是我得到了这个错误:
"aws_ec2_transit_gateway_route_table_propagation" "tgw_route_table_propagation":
for_each = local.propagation_map
local.propagation_map is tuple with 1 element
我也试过了
table_id = propagation
我得到了错误。
您不需要内括号。应该是:
locals {
tgw_route_table_id = aws_ec2_transit_gateway_route_table.transit_gateway_route_table[*].id
propagation_map = merge({
for table_id in local.tgw_route_table_id : {
for table in var.tgw_route_tables : {
for propagation in table.propagations : table_id => propagation
}
}
})
}