我想要识别子网名称以在 Terraform 中创建一个地图。

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

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.html
匿名

发表评论

匿名网友

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

确定