本文介绍了Terraform如何在路由表中获取动态路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Terraform 如何根据工作空间在路由表中获取动态路由
UAT
资源aws_vpn_gateway"micorsoft_vpn_gw"{vpc_id = aws_vpc.default.id标签 = {名称 = 微软"}}资源aws_route_table"私人"{vpc_id = aws_vpc.default.id路线 {cidr_block = "0.0.0.0/0";nat_gateway_id = aws_nat_gateway.default.id}路线 {cidr_block = "0.0.0.0/0";gateway_id = aws_vpn_gateway.micorsoft_vpn_gw.id}标签 = 合并(地图(名称",${var.namespace}-${var.environment}"),var.tags)}
开发
资源aws_route_table"私人"{vpc_id = aws_vpc.default.id路线 {cidr_block = "0.0.0.0/0";nat_gateway_id = aws_nat_gateway.default.id}标签 = 合并(地图(名称",${var.namespace}-${var.environment}"),var.tags)}
如何实现这是基于工作空间/环境的动态方式
解决方案
您可以使用 动态块 .
资源aws_route_table"私人"{vpc_id = aws_vpc.default.id路线 {cidr_block = "0.0.0.0/0";nat_gateway_id = aws_nat_gateway.default.id}动态路线"{for_each = var.env == "DEV";?toset([]) : toset([1])内容 {cidr_block = "0.0.0.0/0";gateway_id = aws_vpn_gateway.micorsoft_vpn_gw.id}}标签 = 合并(地图(名称",${var.namespace}-${var.environment}"),var.tags)}
基本上,当 var.env
为 DEV
时,不会创建第二个 route
.
Terraform how to get dynamic route in route table based on the workspace
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)
}
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)
}
How to achieve this is dynamic way based on workdspace/environment
解决方案
You can use dynamic block for that.
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)
}
Basically, when the var.env
is DEV
, no second route
will be created.
这篇关于Terraform如何在路由表中获取动态路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!