【发布时间】:2021-04-05 16:38:35
【问题描述】:
我目前正在尝试利用 for_each 函数创建 aws_route 资源,并将这些路由与使用 count 函数构建的许多 aws_route_table 资源相关联。我想知道是否有办法让我仍然使用 aws_route 资源中的 for_each 函数并引用 aws_route_table 资源。
我的最终目标是创建 ~5 条路由并将其应用到 ~7 条路由表。
这是我的参考代码:
resource "aws_route_table" "private" {
vpc_id = aws_vpc.vpc.id
count = length(split(",", var.private_subnets))
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = join(",", aws_nat_gateway.nat.*.id)
}
route {
cidr_block = var.stage_vpc_cidr
vpc_peering_connection_id = var.stage_peering_connection_id
}
route {
cidr_block = var.production_vpc_cidr
vpc_peering_connection_id = var.production_peering_connection_id
}
tags = {
Name = "${var.name}-private.${element(split(",", var.azs), count.index)}"
Department = var.tag_department
Lifecycle = var.tag_lifecycle
Account = var.tag_account
Application = var.tag_application
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_route" "vpn-route" {
for_each = toset(var.vpn_subnets)
route_table_id = element(aws_route_table.private.*.id, count.index)
gateway_id = var.vpn_gateway_id
destination_cidr_block = each.value
}
和我的变量
variable "vpn_subnets" {
type = list
default = ["192.168.0.0/24","192.168.2.0/24","192.168.4.0/24","192.168.5.0/24","192.168.10.0/24","192.168.253.0/24"]
}
【问题讨论】:
标签: terraform terraform-provider-aws