# How do you put multiple subnet cidrs into a securitygroup cidr\_block from a data lookup

**URL:** <https://discuss.hashicorp.com/t/how-do-you-put-multiple-subnet-cidrs-into-a-securitygroup-cidr-block-from-a-data-lookup/60669>\
**Category:** Terraform\
**Created:** [December 6, 2023, 11:16pm UTC](https://discuss.hashicorp.com/t/how-do-you-put-multiple-subnet-cidrs-into-a-securitygroup-cidr-block-from-a-data-lookup/60669 "2023-12-06T23:16:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![seth.floyd](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/seth.floyd/32/24217_2.png) [@seth.floyd](https://discuss.hashicorp.com/u/seth.floyd)\
**Post date:** [December 6, 2023, 11:16pm UTC](https://discuss.hashicorp.com/t/how-do-you-put-multiple-subnet-cidrs-into-a-securitygroup-cidr-block-from-a-data-lookup/60669/1 "2023-12-06T23:16:43Z")

</div>

I need help figuring out how to loop through and put each subnet cidr into the cidr\_blocks part of an ingress rule for a security group. I know there are 5 subnets and thus 5 cidrs that I should be adding here but I cant figure out the for\_each way of doing this. Can someone point me in the right direction please? I cant seem to find a current good example to shed any light on this. Thanks!

```auto
data "aws_subnet" "cidr" { #Yes, poorly named but this is what I was given to fix.
  for_each = toset(data.aws_subnets.public.ids)
  id = each.value
}

data "aws_subnets" "public" {
  filter {
    name = "vpc-id"
    values = [var.vpc_id]
  }

  filter {
    name = "tag:Name"
    values = ["${var.environment}-public-*"]
  }

  tags = {
    Type = "Public"
  }
}

resource "aws_security_group" "security_group" {
  name = var.instance_role
  description = "${var.instance_role} ${var.environment}"
  vpc_id = var.vpc_id
  lifecycle {
    create_before_destroy = true
  }

  ingress {
    description = ""
    from_port = 80
    to_port = 80
    protocol = "tcp"
    cidr_blocks = [data.aws_subnet.cidr.*.cidr_block]
  }
}

```

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [December 7, 2023, 1:21am UTC](https://discuss.hashicorp.com/t/how-do-you-put-multiple-subnet-cidrs-into-a-securitygroup-cidr-block-from-a-data-lookup/60669/2 "2023-12-07T01:21:23Z")

</div>

Hi @seth.floyd,

I think you’re on the right track but you’re using the `.*` operator, which is intended for lists, with a map value. That means the result isn’t what you’re intending.

Here’s one way to write that `cidr_blocks` expression to get the result I think you want:

```auto
    cidr_blocks = [for subnet in data.aws_subnet.cidr : subnet.cidr_block]

```

Alternatively, you can make the `.*` and `[*]` operators work by applying them to the result of [the `values` function](https://developer.hashicorp.com/terraform/language/functions/values), which produces a list of the values from a map while discarding the keys:

```auto
    cidr_blocks = values(data.aws_subnet.cidr)[*].cidr_block

```

I used `[*]` instead of `.*` because it’s the more modern operator, although in this particular case it doesn’t actually matter and both would behave the same.

I tend to prefer the first approach I wrote, using a `for` expression, because I tend to think it’s easier to read for a future maintainer. However, the second approach is also relatively common among those who choose to prioritize brevity. They should both achieve the same result.

---

<div class="post-metadata">

**Author:** ![seth.floyd](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/seth.floyd/32/24217_2.png) [@seth.floyd](https://discuss.hashicorp.com/u/seth.floyd)\
**Post date:** [December 7, 2023, 2:28pm UTC](https://discuss.hashicorp.com/t/how-do-you-put-multiple-subnet-cidrs-into-a-securitygroup-cidr-block-from-a-data-lookup/60669/3 "2023-12-07T14:28:21Z")

</div>

Appreciate it…that got me going in the right direction. 👍
