想要识别子网名称以在Terraform中创建一个映射。

huangapple go评论69阅读模式
英文:

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 }

huangapple
  • 本文由 发表于 2023年8月9日 09:36:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864042-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定