UPDATE-> 对于所有有这个特殊问题的人,我找到了解决方案,它就在这个问题的末尾。
更新 2-> 我在这里提出的最后一个解决方案是错误的,请参阅最后的更新。
我正在从数据块中检索cidr_blocks列表以用作aws_ec2_transit_gateway_route上的值,但到目前为止,我无法遍历该列表以获取单个值并将其设置在适当的位置。 我的 data_block.tf 的重要部分如下所示:
data "aws_vpc" "account_vpc" {
provider = aws.dev
count = "${length(data.aws_vpcs.account_vpcs.ids)}"
id = element(tolist(data.aws_vpcs.account_vpcs.ids), 0)
}
data "aws_subnet_ids" "account_subnets" {
provider = aws.dev
vpc_id = element(tolist(data.aws_vpcs.account_vpcs.ids), 0)
}
data "aws_subnet" "cidrblocks" {
provider = aws.dev
for_each = data.aws_subnet_ids.account_subnets.ids
id = each.value
}
我打算使用它的部分是这个,tgw_rt.tf:
resource "aws_ec2_transit_gateway_route" "shared-routes" {
provider = aws.shared
#count = length(data.aws_subnet.cidrblocks.cidr_block)
#destination_cidr_block = lookup(data.aws_subnet.cidrblocks.cidr_block[count.index], element(keys(data.aws_subnet.cidrblocks.cidr_block[count.index]),0), "127.0.0.1/32")
#destination_cidr_block = data.aws_subnet.cidrblocks.cidr_block[count.index]
#destination_cidr_block = [data.aws_subnet.cidrblocks.*.cidr_block]
destination_cidr_block = [for s in data.aws_subnet.cidrblocks : s.cidr_block]
/* for_each = [for s in data.aws_subnet.cidrblocks: {
destination_cidr_block = s.cidr_block
}] */
#destination_cidr_block = [for s in data.aws_subnet.cidrblocks : s.cidr_block]
#destination_cidr_block = data.aws_subnet.cidrblocks.cidr_block[count.index]
transit_gateway_attachment_id = aws_ec2_transit_gateway_vpc_attachment.fromshared.id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.shared.id
}
评论中的部分是我迄今为止尝试过的,没有任何效果。 使用该未注释部分时当前发生的错误是
Error: Incorrect attribute value type
on modules/tgw/tgw_rt.tf line 20, in resource "aws_ec2_transit_gateway_route" "shared-routes":
20: destination_cidr_block = [for s in data.aws_subnet.cidrblocks : s.cidr_block]
|----------------
| data.aws_subnet.cidrblocks is object with 3 attributes
Inappropriate value for attribute "destination_cidr_block": string required.
如果这里在场的一位地球神能对这个问题有所了解,我将不胜感激。
解决方案 - 这是错误的由于它抱怨它是一个具有 3 个属性(3 个 Cidr 块(的对象,为了迭代,我不得不使用它:
destination_cidr_block = element([for s in data.aws_subnet.cidrblocks : s.cidr_block], 0)
正确的解决方案 解决方案是在建议中添加一小部分@kyle我必须使用一个对象来表示数据并将其转换为地图,您摇滚@kyle:
for_each = {for s in data.aws_subnet.cidrblocks: s.cidr_block => s}
destination_cidr_block = each.value.cidr_block
提前谢谢大家
我没有使用过data.aws_subnet
,但我认为您的for_each
尝试很接近-
resource "aws_ec2_transit_gateway_route" "shared-routes" {
...
for_each = [for s in data.aws_subnet.cidrblocks: s.cidr_block]
destination_cidr_block = each.value
...
}