【发布时间】:2021-05-22 17:18:39
【问题描述】:
添加类似 Python 的伪代码来解释我想通过 Terraform 实现的目标。
我有 6 个中转网关路由表、10 个中转网关 VPN 附件 我需要这个来传输网关路由表关联和传播
尝试创建可以在资源函数 aws_ec2_transit_gateway_route_table_association 中引用的本地“map/dict” 资源函数需要transit_gateway_attachment_id和transit_gateway_route_table_id
tg_rt = [
{
"name" = "A"
"tg_rt_id" = "tgw-rtb-aa"
},
{
"name" = "B"
"tg_rt_id" = "tgw-rtb-bb"
},
{
"name" = "C"
"tg_rt_id" = "tgw-rtb-cc"
},
{
"name" = "D"
"tg_rt_id" = "tgw-rtb-dd"
},
{
"name" = "E"
"tg_rt_id" = "tgw-rtb-ee"
},
{
"name" = "F"
"tg_rt_id" = "tgw-rtb-ff"
},
]
tga_vpn = [
{
"name" = "1"
"tga_id" = "tgw-attach-11"
},
{
"name" = "2"
"tga_id" = "tgw-attach-22"
},
{
"name" = "3"
"tga_id" = "tgw-attach-33"
},
{
"name" = "4"
"tga_id" = "tgw-attach-44"
},
{
"name" = "5"
"tga_id" = "tgw-attach-55"
},
{
"name" = "6"
"tga_id" = "tgw-attach-66"
},
{
"name" = "7"
"tga_id" = "tgw-attach-77"
},
{
"name" = "8"
"tga_id" = "tgw-attach-88"
},
]
for tgw_rt in tgw_rts: # list of dicts with id and name
for tga_vpn in tga_vpns: # # list of dict with id and name
if tgw_rt.name == "PROD" and tga_vpn.name in [1, 2, 3, 4]:
rt_id = tgw_rt.id
vpn_attachment_id = tga_vpn.id
elif tgw_rt.name == "DEV" and tga_vpn.name in [5, 6, 7, 8]:
rt_id = tgw_rt.id
vpn_attachment_id = tga_vpn.id
elif tgw_rt.name == "STAGING" and tga_vpn.name in [9, 10, 11, 12]:
rt_id = tgw_rt.id
vpn_attachment_id = tga_vpn.id
resource "aws_ec2_transit_gateway_route_table_association" "TGW-RT-VPN-ASSOCIATION" {
for_each = {
for tg_vpn_attach, tg_vpn_attach_details in local.above_map : tg_vpn_attach => tg_vpn_attach_details
}
transit_gateway_route_table_id = each.rt_id # for reference
transit_gateway_attachment_id = each.vpn_attachment_id # for reference
}
我再次部署了一个 TGW,其中包含六个路由表/路由以及几种 VPC 和 VPN 连接类型,并希望根据上述逻辑格式创建正确的关联/传播。
如果有任何问题,请告诉我,我们将不胜感激。
谢谢!
【问题讨论】:
-
最好的答案会像以前一样。将其拆分为多个资源。为什么不使用之前建议的方法?
-
@Marcin 有很多路由表6个,会有vpc关联,vpc传播和vpn关联,vpn传播的声明。
-
@Marcin 这将使它成为 24 个资源语句,这就是为什么我试图创建一个带有关联的映射,这样就会有 4 个语句 vpc 关联、vpc 传播和 vpn 关联以及 vpn 传播,路由是从循环中的同一资源创建的,所以我不能在资源 aws_ec2_transit_gateway_route_table_association 中有两个 for_each(vpn 附件 ID 和路由表 ID)正确吗?
-
顺便说一句。我注意到你以前的所有答案都有答案。您甚至在其中写下“这对您很有帮助。非常感谢!”,但没有接受任何答案。接受有用的答案是一种很好的做法,可以帮助其他读者。
标签: amazon-web-services terraform