Home > Blockchain >  Mapping multiple Security Groups into ELB
Mapping multiple Security Groups into ELB

Time:03-31

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?

CodePudding user response:

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
  • Related