我创建了一个创建AWS子网的模块。在模块中,我使用for_each循环来创建子网。我假设我可以索引给定子网的资源ID,但循环似乎没有按可预测的顺序输出索引子网ID。。。不确定我在这里错过了什么。感谢您的帮助!
main.tf
module "subnets" {
source = "./modules/subnets/"
vpc_id = aws_vpc.main.id
subnets = [
{
name = "private-1a"
cidr_block = "10.0.0.0/28"
availability_zone = "us-east-1a"
},
{
name = "private-1b"
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1b"
},
{
name = "public-1a"
cidr_block = "10.0.2.0/24"
availability_zone = "us-east-1a"
},
{
name = "public-1b"
cidr_block = "10.0.3.0/24"
availability_zone = "us-east-1b"
}
]
}
output "private1a" {
value = module.security_subnets.subnet_id[0]
}
output "private1b" {
value = module.security_subnets.subnet_id[1]
}
output "public1a" {
value = module.security_subnets.subnet_id[2]
}
output "public1b" {
value = module.security_subnets.subnet_id[3]
}
模块
locals {
subnets = {
for i in var.subnets :
i.name => i
}
}
resource "aws_subnet" "main" {
for_each = local.subnets
vpc_id = var.vpc_id
cidr_block = each.value.cidr_block
availability_zone = each.value.availability_zone
tags = merge(
{
"Name" = format("%s", each.value.name)
}
)
}
output subnet_id {
value = values(aws_subnet.main)[*].id
}
一般来说,依赖返回项目的顺序不是一个好的做法,因为如果您决定在两者之间添加或删除某些内容,这可能会导致未来的问题。
然而,在您的情况下,退货的顺序是定义良好的。由于您使用的是数值,因此顺序为:
值由其对应的键按字典顺序返回,因此值的返回顺序与键从键返回的顺序相同。
我认为您可以使用count
而不是for_each
来实现这一点,这是在支持for_each
之前的做法。然后输出将与变量的顺序相同。
resource "aws_subnet" "main" {
count = length(var.subnets)
vpc_id = var.vpc_id
cidr_block = var.subnets[count.index].cidr_block
availability_zone = var.subnets[count.index].availability_zone
tags = merge(
{
"Name" = format("%s", var.subnets[count.index].name)
}
)
}
output "subnet_id" {
value = aws_subnet.main[*].id
}
// Not sure about this one
output "subnet_by_name" {
value = {for index, subnet in var.subnets: subnet.name => aws_subnet.main[index].id}
使用映射结构而不是subnets
变量的数组应该按索引返回值,然后在顺序发生变化时解决混合值的问题(比如删除了一个项(:
module "subnets" {
source = "./modules/subnets/"
vpc_id = aws_vpc.main.id
subnets = {
"private-1a" = {
name = "private-1a"
cidr_block = "10.0.0.0/28"
availability_zone = "us-east-1a"
},
"private-1b" = {
name = "private-1b"
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1b"
},
"public-1a" = {
name = "public-1a"
cidr_block = "10.0.2.0/24"
availability_zone = "us-east-1a"
},
"public-1b" = {
name = "public-1b"
cidr_block = "10.0.3.0/24"
availability_zone = "us-east-1b"
}
}
}
output "private1a" {
value = module.security_subnets.subnet_id["private-1a"]
}
// and so on