英文:
Would like to identify subnet names to create a map in terraform
问题
我一直在尝试找到一种方法从AWS中获取子网的名称... 到目前为止没有成功。我可以找到ARN、ID、CIDR等等很多其他信息。但是名称却找不到。帮助将不胜感激!
这是我的目标。创建一个子网映射,其中子网名称作为键,子网ID作为值。显然,名称不是data.aws_subnet
的支持属性。我在'谷歌'上搜寻了很多,但没有找到。
data "aws_subnets" "private" {
tags = {
Name = "private*"
}
filter {
name = "state"
values = ["available"]
}
}
data "aws_subnet" "private" {
for_each = toset(data.aws_subnets.private.ids)
id = each.value
}
# 想要创建一个映射,以子网名称 => 子网ID... 但是,名称是不受支持的属性。
private_subnet_ids = { for s in data.aws_subnet.private : trimprefix(s.name, "private") => s.id }
英文:
I've been trying to find a way to get the subnet names of out of aws... so far no luck. I can find the arn, ids, cidrs... and a bunch of other stuff. But, not names. Help is appreciated!
This is what I'm aiming for. To create a subnet map with the subnet name as the key, and the subnet id as the value. Obviously name IS NOT a supported attribute of data.aws_subnet
. I have scoured the 'google' with not luck.
data "aws_subnets" "private" {
tags = {
Name = "private*"
}
filter {
name = "state"
values = ["available"]
}
}
data "aws_subnet" "private" {
for_each = toset(data.aws_subnets.private.ids)
id = each.value
}
# wanting to create a map with subnet.name => subnet.id... but, name is an unsupported attribute.
private_subnet_ids = { for s in data.aws_subnet.private : trimprefix(s.name, "private") => s.id }
答案1
得分: 0
你必须使用 tags["Name"]
:
private_subnet_ids = { for s in data.aws_subnet.private : trimprefix(s.tags["Name"], "private") => s.id }
英文:
You have to use tags["Name"]
:
private_subnet_ids = { for s in data.aws_subnet.private : trimprefix(s.tags["Name"], "private") => s.id }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论