Answers: <p>It seems that you already know which CIDR ranges you wish to look up, and that you are trying to find the corresponding subnet id for each of them.</p>
  <p>You can achieve that by using the <code>aws_subnet</code> data source with <a href="https://www.terraform.io/language/meta-arguments/for_each" rel="nofollow noreferrer"><code>for_each</code></a>, to tell Terraform to make a separate call to that data source for each of your CIDR ranges. I'm assuming that the <code>my_subnets</code> example in your question is inside a <code>locals</code> block and so it has declared and defined <code>local.my_subnets</code> to hold that list of strings.</p>
  <pre><code>data &quot;aws_subnet&quot; &quot;example&quot; {
    for_each = toset(local.my_subnets)
  
    cidr_block = each.key
  }
  </code></pre>
  <p>A resource with <code>for_each</code> set appears in expressions as a map of objects where the map keys are the same as the <code>for_each</code> map keys (or, in this case, set elements). Therefore you can derive from this a mapping from CIDR block to subnet ID:</p>
  <pre><code>locals {
    subnet_ids = tomap({
      for cidr, subnet in data.aws_subnet.example : cidr =&gt; subnet.id
    })
  }
  </code></pre>
  <p>or, if you don't actually care about the correspondence between the CIDR blocks and the ids and just want a flat set of ids, you can just ignore the instance keys:</p>
  <pre><code>locals {
    subnet_ids = toset([
      for subnet in data.aws_subnet.example : subnet.id
    ])
  }
  </code></pre>
  <p>The second of these seems to be what is expected by the <code>subnet_ids</code> argument you want to assign to:</p>
  <pre><code>  vpc_options {
      subnet_ids = [
        for subnet in data.aws_subnet.example : subnet.id
      ]
    }
  </code></pre>
  <p>(I omitted the <code>toset</code> call when inlining this expression in the argument, because the schema for that <code>vpc_options</code> block will already tell Terraform whether <code>subnet_ids</code> is a set or a list, and so Terraform can convert automatically in that case.)</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-02-01T09:48:00.490Z
Answers: thanks, that's exactly what I was looking for
Answers: Most Wanted
Answers: CC BY-SA 4.0
Answers: 2022-01-31T17:57:11.803Z
Answers: 2022-01-31T17:57:11.803Z
Answers: Martin Atkins
Body: <p>I have a list of CIDRs for my subnets and want to get their ids. Is it possible to do with terraform?</p>
  <pre><code>my_subnets = [&quot;212.12.1.0/24&quot;, &quot;212.12.2.0/24&quot;, &quot;212.12.3.0/24&quot;]
  </code></pre>
  <p>I know VPC id as well, how to get an array of subnets ids via <a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet" rel="nofollow noreferrer">data source</a>?</p>
  <p>The idea is to pass that next as an argument to another module</p>
  <pre><code>vpc_options {
      # invalid code, just for illustration
      subnet_ids = data.aws_subnet.my_subnets[*].id
    }
  </code></pre>
  
Tags: <amazon-web-services><terraform><aws-vpc><terraform-module><aws-subnets>
Title: Terraform get subnet id by it's CIDR block
UserDisplayName: Most Wanted
ThreadText: # Terraform get subnet id by it's CIDR block
  
  (Asked by: username_0 on 2022-01-31)
  
  I have a list of CIDRs for my subnets and want to get their ids. Is it possible to do with terraform?
  
  ```
  my_subnets = ["212.12.1.0/24", "212.12.2.0/24", "212.12.3.0/24"]
  
  ```
  
  I know VPC id as well, how to get an array of subnets ids via [data source](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet)?
  
  The idea is to pass that next as an argument to another module
  
  ```
  vpc_options {
      # invalid code, just for illustration
      subnet_ids = data.aws_subnet.my_subnets[*].id
    }
  
  ```
  
  **Tags:** amazon-web-services, terraform, aws-vpc, terraform-module, aws-subnets
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-01-31. Score: 1 | Accepted answer
  
  It seems that you already know which CIDR ranges you wish to look up, and that you are trying to find the corresponding subnet id for each of them.
  
  You can achieve that by using the `aws_subnet` data source with [`for_each`](https://www.terraform.io/language/meta-arguments/for_each), to tell Terraform to make a separate call to that data source for each of your CIDR ranges. I'm assuming that the `my_subnets` example in your question is inside a `locals` block and so it has declared and defined `local.my_subnets` to hold that list of strings.
  
  ```
  data "aws_subnet" "example" {
    for_each = toset(local.my_subnets)
  
    cidr_block = each.key
  }
  
  ```
  
  A resource with `for_each` set appears in expressions as a map of objects where the map keys are the same as the `for_each` map keys (or, in this case, set elements). Therefore you can derive from this a mapping from CIDR block to subnet ID:
  
  ```
  locals {
    subnet_ids = tomap({
      for cidr, subnet in data.aws_subnet.example : cidr => subnet.id
    })
  }
  
  ```
  
  or, if you don't actually care about the correspondence between the CIDR blocks and the ids and just want a flat set of ids, you can just ignore the instance keys:
  
  ```
  locals {
    subnet_ids = toset([
      for subnet in data.aws_subnet.example : subnet.id
    ])
  }
  
  ```
  
  The second of these seems to be what is expected by the `subnet_ids` argument you want to assign to:
  
  ```
    vpc_options {
      subnet_ids = [
        for subnet in data.aws_subnet.example : subnet.id
      ]
    }
  
  ```
  
  (I omitted the `toset` call when inlining this expression in the argument, because the schema for that `vpc_options` block will already tell Terraform whether `subnet_ids` is a set or a list, and so Terraform can convert automatically in that case.)
  
  > **Comments:**
  > **username_0**: thanks, that's exactly what I was looking for  
  
  ---
  
  