AWS Terraform:通过匹配标记名称中的子字符串来过滤特定的子网

时间:2018-02-15 23:34:14

标签: amazon-web-services terraform

我有6个子网,我想从匹配子串internal的子网中过滤3个子网并在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 substring

需要获取其标记名称具有内部子字符串

的所有子网ID

values = ["*"]返回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"
                }

1 个答案:

答案 0 :(得分:2)

aws_subnet_ids具有此功能,但方式不同。这解决了我的问题:

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

  tags {
    Name = "*internal*"
  }
}

感谢您审核是否有人这样做。