Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mapping multiple Security Groups into ELB

I’m trying to attach multiple security groups containing Cloudfront CIDRs to my AWS ALB.

locals {
  chunks = chunklist(data.aws_ip_ranges.cloudfront.cidr_blocks, 60)
  chunks_map = { for i in range(length(local.chunks)): i => local.chunks[i] }
}

resource "aws_security_group" "sg" {
  for_each = local.chunks_map
  name = "{each.key}"

  egress {
    ....
  }
}

resource "aws_elb" "load" {
  name = "test"
  security_groups = aws_security_group.sg.id // This is wrong

My error that I’m receiving is
Because aws_security_group.sg has for_each se, its attributes must be access on specific instances

Using for_each again doesn’t make sense because i don’t want to create multiple resources, I just want to ensure that all security groups created are attached to the load balancer. Any ideas?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since you’ve used for_each there will be more than instance of aws_security_group.sg. To get id from all of them you can use splat operator:

security_groups = values(aws_security_group.sg)[*].id
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading