gpt4 book ai didi

amazon-web-services - Terraform如何在路由表中获取动态路由

转载 作者:行者123 更新时间:2023-12-04 08:45:51 28 4
gpt4 key购买 nike

Terraform 如何基于工作区获取路由表中的动态路由

UAT

resource "aws_vpn_gateway" "micorsoft_vpn_gw" { 
vpc_id = aws_vpc.default.id

tags = {
Name = "micorsoft"
}
}

resource "aws_route_table" "private" {
vpc_id = aws_vpc.default.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.default.id
}

route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_vpn_gateway.micorsoft_vpn_gw.id
}

tags = merge(map("Name", "${var.namespace}-${var.environment}"), var.tags)
}

DEV

resource "aws_route_table" "private" { 
vpc_id = aws_vpc.default.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.default.id
}

tags = merge(map("Name", "${var.namespace}-${var.environment}"), var.tags)
}
如何实现这是基于工作空间/环境的动态方式

最佳答案

您可以使用 dynamic block为了那个原因。

resource "aws_route_table" "private" { 
vpc_id = aws_vpc.default.id

route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.default.id
}

dynamic "route" {
for_each = var.env == "DEV" ? toset([]) : toset([1])
content {
cidr_block = "0.0.0.0/0"
gateway_id = aws_vpn_gateway.micorsoft_vpn_gw.id
}
}

tags = merge(map("Name", "${var.namespace}-${var.environment}"), var.tags)
}
基本上,当 var.envDEV , 没有第二 route将被创建。

关于amazon-web-services - Terraform如何在路由表中获取动态路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64334897/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com