本文介绍了AWS Terraform:通过匹配标签名称中的子字符串来过滤特定子网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 6 个子网,我想从中过滤出 3 个匹配子字符串 internal 的子网并在 rds 中使用.

I have 6 subnets, I want to filter 3 subnets from them matching substring internal and use in rds.

标签名称有内部词,并希望基于该词进行过滤.

Tag name has internal word and want to filter based on that.

谁能帮帮我?

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 子字符串

Some tag name has internal substring

需要抓取所有标签名有内部子串的子网id

Need to grab all subnet id whose tag name has internal substring

values = ["*"] 返回 6 ids,然而,values = ["any word not work"]values = ["*internal*"] 不起作用.

values = ["*"] return 6 ids, however, values = ["any word not work"] or values = ["*internal*"] doesn't work.

以下是错误:

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 个,这意味着应该有部分好的东西和部分坏的东西.

There should be 6 but I am getting only 3, that means there should be partially good things and partially bad things.

这 3 个子网在标签名称中没有 internal 子字符串.

These 3 subnets doesn't have internal substring in tag name.

这意味着它正在解析.aws_subnet_ids 没有过滤选项.

It means it's parsing. aws_subnet_ids doesn't have filter option.

应该有.一场比赛,这很简单,但是,我需要多场比赛.

There should be instead. For one match, it will be simple, however, I need multiple matches.

现在我猜这个错误是因为循环运行了 6 次.

In my guess now the error is because of loops which runs for 6 times.

这是没有过滤器的相同输出:

Here is same output without filter:

                  "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 有这个功能,但是方式不同.这里解决了我的问题:

aws_subnet_ids has this feature, however, different way. Here it solved my problem:

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

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

感谢您的评论:D

这篇关于AWS Terraform:通过匹配标签名称中的子字符串来过滤特定子网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 17:50