<pre><code>data "aws_subnet" "subnets" {
  for_each = toset(var.aws_subnet_ids)
  id       = each.value
}

locals {
  cidr_blocks = [
    for subnet in data.aws_subnet.subnets :
      subnet.cidr_block
    ]

  cidr_blocks_rendered = [
    for cidr_block in local.cidr_blocks :
     {
       from_port   = 6443,
       to_port     = 6443,
       protocol    = "tcp",
       cidr_blocks = cidr_block
     }
  ]
}

output "output1-1" {
  value = local.cidr_blocks_rendered
}
</code></pre>

<p>This is what ultimately I needed.</p>

CC BY-SA 4.0
2020-09-01T02:04:44.900Z
Exactly what I was looking for, thank you.
sbaxter
CC BY-SA 4.0
2021-05-06T02:21:24.360Z
Shortcut: `data.aws_subnet.subnets[*].cidr_block`
Anton
CC BY-SA 4.0
2020-02-06T03:54:02.160Z
2020-02-06T03:54:02.160Z
Brando___
<p>I have a list of subnets IDs and I want to get an array of the cidr_blocks associated to each subnet.</p>

<pre><code>variable "aws_subnet_ids" {
  type = list(string)
  default = ["subnet-aaaaaaaa","subnet-bbbbbbbb"]
}

desired_output = ['10.0.1.0/24', '10.0.2.0/24']
</code></pre>

<p>I've been fiddling around with for loops and count data for a quite a while for what should be a really simple problem. But I'm having a hard time finding an appropriate way to actually do this.</p>

<p>This seemed like a decent example to work off of, but you can't actually use a for_each in a data source off a list of strings. <a href="https://www.terraform.io/docs/providers/aws/d/subnet_ids.html" rel="nofollow noreferrer">https://www.terraform.io/docs/providers/aws/d/subnet_ids.html</a></p>

<pre><code>data "aws_subnet" "example" {
  for_each = var.aws_subnet_ids
  cidr_blocks       = each.cidr_block
}

output "subnet_cidr_blocks" {
  value = [for s in data.aws_subnet.example : s.cidr_block]
}

</code></pre>

<terraform>
Terraform 0.12 - For each subnet-id in array, get cidr_block
Brando___
# Terraform 0.12 - For each subnet-id in array, get cidr_block

(Asked by: username_0 on 2020-02-05)

I have a list of subnets IDs and I want to get an array of the cidr_blocks associated to each subnet.
variable "aws_subnet_ids" {
  type = list(string)
  default = ["subnet-aaaaaaaa","subnet-bbbbbbbb"]
}

desired_output = ['10.0.1.0/24', '10.0.2.0/24']

I've been fiddling around with for loops and count data for a quite a while for what should be a really simple problem. But I'm having a hard time finding an appropriate way to actually do this.
This seemed like a decent example to work off of, but you can't actually use a for_each in a data source off a list of strings. https://www.terraform.io/docs/providers/aws/d/subnet_ids.html
data "aws_subnet" "example" {
  for_each = var.aws_subnet_ids
  cidr_blocks       = each.cidr_block
}

output "subnet_cidr_blocks" {
  value = [for s in data.aws_subnet.example : s.cidr_block]
}

**Tags:** terraform


---

## 1 answers

### Answer by username_0 on 2020-02-06. Score: 3 | Accepted answer

data "aws_subnet" "subnets" {
  for_each = toset(var.aws_subnet_ids)
  id       = each.value
}

locals {
  cidr_blocks = [
    for subnet in data.aws_subnet.subnets :
      subnet.cidr_block
    ]

  cidr_blocks_rendered = [
    for cidr_block in local.cidr_blocks :
     {
       from_port   = 6443,
       to_port     = 6443,
       protocol    = "tcp",
       cidr_blocks = cidr_block
     }
  ]
}

output "output1-1" {
  value = local.cidr_blocks_rendered
}

This is what ultimately I needed.

> **Comments:**
> **username_1**: Exactly what I was looking for, thank you.  
> **username_2**: Shortcut: `data.aws_subnet.subnets[*].cidr_block`  

---

