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!
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]
}
}