title: How to filter aws subnets in terraform
body: <p>I am porting a custom made deployment infrastructure to terraform. In that custom codebase there is something that says - fetch all available subnets from default region vpc but only those that have 20 or more available IPv4 addresses.</p>
  <p>So I was experimenting with this code</p>
  <pre class="lang-hcl prettyprint-override"><code>data &quot;aws_vpc&quot; &quot;main&quot; {
    default = true
  }
  
  data &quot;aws_subnets&quot; &quot;vpcsubnets&quot; {
    filter {
      name   = &quot;vpc-id&quot;
      values = [data.aws_vpc.main.id]
    }
    filter {
      name   = &quot;default-for-az&quot;
      values = [true]
    }
    filter {
      name   = &quot;state&quot;
      values = [&quot;available&quot;]
    }
  }
  
  output &quot;ids2&quot; {
    value = {
      for k, v in data.aws_subnets.vpcsubnets : k =&gt; v if v.available_ip_address_count &gt; 20
    }
  }
  
  </code></pre>
  <p>But I get errors like these</p>
  <pre><code> Error: Invalid reference
  │ 
  │   on main.tf line 51, in output &quot;ids2&quot;:
  │   51:     for k, vid in data.aws_subnets.vpcsubnets : k =&gt; v if v.available_ip_address_count &gt; 20
  │ 
  │ A reference to a resource type must be followed by at least one attribute access, specifying the resource name.
  </code></pre>
  <p>Using Terraform 1.0.8 and aws provider 3.62</p>