amazon-web-services - AWS 地形 : Filter specific subnets by matching substring in tag name

标签 amazon-web-services terraform

我有 6 个子网,我想从中过滤 3 个子网匹配子字符串 internal并在 rds 中使用。
标签名称有内部词,并希望以此为基础进行过滤。
有人可以帮我吗?

data "aws_vpc" "vpc_nonprod-sctransportationops-vpc" {
  tags {
    Name = "vpc_nonprod-sctransportationops-vpc"
  }
}
    
data "aws_subnet_ids" "all" {
  vpc_id = "${data.aws_vpc.vpc_nonprod-sctransportationops-vpc.id}"
}
    
output "aws_subnet_ids" {
  value = "${data.aws_subnet_ids.all.ids}"
}
# 6 subnets
# Now look up details for each subnet
     

data "aws_subnet" "filtered_subnets" {
  count = "${length(data.aws_subnet_ids.all.ids)}"
  id    = "${data.aws_subnet_ids.all.ids[count.index]}"

  filter {
    name   = "tag:Name"
    values = ["*internal*"]
  }
}
一些标签名称有 internal子串
需要获取标签名称具有内部子字符串的所有子网IDvalues = ["*"]返回 6但是,ID,values = ["any word not work"]values = ["*internal*"]不起作用。
以下是错误:
Error: Error refreshing state: 1 error(s) occurred:

* data.aws_subnet.publicb: 3 error(s) occurred:

* data.aws_subnet.publicb[1]: data.aws_subnet.publicb.1: no matching subnet found
* data.aws_subnet.publicb[4]: data.aws_subnet.publicb.4: no matching subnet found
* data.aws_subnet.publicb[0]: data.aws_subnet.publicb.0: no matching subnet found
应该有 6 个,但我只得到 3 个,这意味着应该有部分好的东西和部分坏的东西。
这 3 个子网没有 internal标签名称中的子字符串。
这意味着它正在解析。 aws_subnet_ids没有过滤选项。
应该有。对于一场比赛,这很简单,但是,我需要多场比赛。
在我现在的猜测中,错误是因为循环运行了 6 次。
这是没有过滤器的相同输出:

                  "data.aws_subnet.filtered_subnets.2": {
                    "type": "aws_subnet",
                    "depends_on": [
                        "data.aws_subnet_ids.all"
                    ],
                    "primary": {
                        "id": "subnet-14058972",
                        "attributes": {
                            "assign_ipv6_address_on_creation": "false",
                            "availability_zone": "us-west-2a",
                            "cidr_block": "172.18.201.0/29",
                            "default_for_az": "false",
                            "id": "subnet-14038772",
                            "map_public_ip_on_launch": "false",
                            "state": "available",
                            "tags.%": "4",
                            "tags.Designation": "internal",
                            "tags.Name": "subnet_nonprod-sctransportationops-vpc_internal_az2",
                            "tags.Permissions": "f00000",
                            "tags.PhysicalLocation": "us-west-2a",
                            "vpc_id": "vpc-a47k07c2"
                        },
                        "meta": {},
                        "tainted": false
                    },
                    "deposed": [],
                    "provider": "provider.aws"
                }

最佳答案

aws_subnet_ids有这个功能,但是方式不同。在这里它解决了我的问题:

data "aws_subnet_ids" "all" {
  vpc_id = "${data.aws_vpc.vpc_nonprod-sctransportationops-vpc.id}"

  tags = {
    Name = "*internal*"
  }
}

感谢您的审核 :D

关于amazon-web-services - AWS 地形 : Filter specific subnets by matching substring in tag name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48817967/

相关文章:

node.js - 使用 AWS SNS 的浏览器通知

amazon-web-services - 如何设置从 docker-hub 到 AWS ECS 的连续部署?

ubuntu - 无法通过 SSH 连接到 Amazon EC2 帐户。 (公钥错误)

azure - 使用 Terraform 使用 Key Vault 维护的 RSA key 创建 AKS

amazon-web-services - 如何在带有 Terraform 的 AWS VPC 中的两个子网之间进行路由?

amazon-web-services - 按创建日期查询 S3 存储桶文件

hadoop - Amazon Web Services Map-Reduce 错误 Illegal character in path at index

Terraform - API 以不同的顺序返回数据,因此 terraform 看到了毫无意义的变化?

azure - 如何向 Terraform 中的 Azure Devops 项目授予 "Storage Blob Data Contributor"权限?

azure - 如何使用 terraform 在系统分配的身份上添加角色分配?