title: AWS Terraform: Filter specific subnets by matching substring in tag name
body: <p>I have 6 subnets, I want to filter 3 subnets from them matching substring <code>internal</code> and use in rds.</p>
  <p>Tag name has internal word and want to filter based on that.</p>
  <p>Could anyone please help me?</p>
  <pre><code>data &quot;aws_vpc&quot; &quot;vpc_nonprod-sctransportationops-vpc&quot; {
    tags {
      Name = &quot;vpc_nonprod-sctransportationops-vpc&quot;
    }
  }
      
  data &quot;aws_subnet_ids&quot; &quot;all&quot; {
    vpc_id = &quot;${data.aws_vpc.vpc_nonprod-sctransportationops-vpc.id}&quot;
  }
      
  output &quot;aws_subnet_ids&quot; {
    value = &quot;${data.aws_subnet_ids.all.ids}&quot;
  }
  # 6 subnets
  # Now look up details for each subnet
       
  
  data &quot;aws_subnet&quot; &quot;filtered_subnets&quot; {
    count = &quot;${length(data.aws_subnet_ids.all.ids)}&quot;
    id    = &quot;${data.aws_subnet_ids.all.ids[count.index]}&quot;
  
    filter {
      name   = &quot;tag:Name&quot;
      values = [&quot;*internal*&quot;]
    }
  }
  </code></pre>
  <p>Some tag name has <code>internal</code> substring</p>
  <p>Need to grab all subnet id whose tag name has internal substring</p>
  <p><code>values = [&quot;*&quot;]</code> return <code>6</code> ids, however, <code>values = [&quot;any word not work&quot;]</code> or <code>values = [&quot;*internal*&quot;]</code> doesn't work.</p>
  <p>Following are error:</p>
  <pre><code>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
  </code></pre>
  <p>There should be 6 but I am getting only 3, that means there should be  partially good things and partially bad things.</p>
  <p>These 3 subnets doesn't have <code>internal</code> substring in tag name.</p>
  <p>It means it's parsing. <code>aws_subnet_ids</code> doesn't have filter option.</p>
  <p>There should be instead. For one match, it will be simple, however, I need multiple matches.</p>
  <p>In my guess now the error is because of loops which runs for 6 times.</p>
  <p>Here is same output without filter:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>                  "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"
                  }</code></pre>
  </div>
  </div>
  </p>