我正在尝试按以下方式创建子网。我正在尝试循环父模块中的对象:
variable "list_of_subnets" {
description = "list of vpc subnets"
type = list(object({
subnet_type = string
cidr_block = string
av_zone = string
}))
default = [
{
subnet_type = "public"
cidr_block = "10.0.101.0/24"
av_zone = "us-west-1a"
},
{
subnet_type = "public"
cidr_block = "10.0.102.0/24"
av_zone = "us-west-1b"
},
{
subnet_type = "private"
cidr_block = "10.0.1.0/24"
av_zone = "us-west-1a"
},
{
subnet_type = "private"
cidr_block = "10.0.2.0/24"
av_zone = "us-west-1b"
},
{
subnet_type = "database"
cidr_block = "10.0.151.0/24"
av_zone = "us-west-1a"
},
{
subnet_type = "database"
cidr_block = "10.0.152.0/24"
av_zone = "us-west-1b"
}
]
}
上面是我的变量块,我通过它循环来创建多个子网:
module "subnets" {
source = "./modules/subnets"
for_each = {for i, v in var.list_of_subnets: i => v}
aws_vpc_id = module.VPC-Manual.vpc_id
sub_cidr = each.value.cidr_block
sub_av_zone = each.value.av_zone
subnet_type = each.value.subnet_type
}
“./modules/subnets:”现在,在子模块中,只需传递这些变量:
resource "aws_subnet" "aws_subnets" {
vpc_id = var.aws_vpc_id
cidr_block = var.sub_cidr
availability_zone = var.sub_av_zone
tags = {
subnet_type = var.subnet_type
}
}
在子输出块中尝试根据标签检索 id,以便仅检索公共子网 id:
output "public_subnet_ids" {
value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
}
父输出:
output "subnet_ids" {
value = module.subnets.public_subnet_ids
}
Terraform apply 出现以下错误:
Changes to Outputs:
+ vpc_id = (known after apply)
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (string).
╵
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (bool).
╵
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (string).
╵
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (string).
╵
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (string).
╵
╷
│ Error: Attempt to get attribute from null value
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ This value is null, so it does not have any attributes.
╵
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (bool).
╵
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (bool).
╵
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (bool).
╵
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (string).
╵
╷
│ Error: Attempt to get attribute from null value
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ This value is null, so it does not have any attributes.
╵
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (string).
╵
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (bool).
╵
╷
│ Error: Attempt to get attribute from null value
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ This value is null, so it does not have any attributes.
╵
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (bool).
╵
╷
│ Error: Attempt to get attribute from null value
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ This value is null, so it does not have any attributes.
╵
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (string).
╵
╷
│ Error: Unsupported attribute
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ Can't access attributes on a primitive-typed value (string).
╵
╷
│ Error: Missing map element
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ This map does not have an element with the key "tags".
╵
╷
│ Error: Missing map element
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ This map does not have an element with the key "tags".
╵
╷
│ Error: Attempt to get attribute from null value
│
│ on modules/subnets/output.tf line 3, in output "public_subnet_ids":
│ 3: value = [for subnet in aws_subnet.aws_subnets: subnet.tags["subnet_type"] == "public" ? "${subnet.id}" : null]
│
│ This value is null, so it does not have any attributes.
如果我正确地阅读了您的代码,您正在尝试为每个子网模块创建一个子网。但是,我相信您的意图是为所有子网提供一个模块,那么您的模块中不应该有
for_each
循环,而应在应用 list_of_subnets
循环的模块本身内部传递 for_each
到 aws_subnet
资源。
据说可以解决您当前的问题/错误,您不需要循环遍历 aws_subnet.aws_subnets 中的子网,因为它永远只是一个子网。
output "public_subnet_ids" {
value = aws_subnet.aws_subnets.tags_all["subnet_type"] == "public" ? "${aws_subnet.aws_subnets.id}" : null
}