Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform circular dependency between security groups

I have a setup: ALB in front of ECS. I have a module of ecs and one for alb: The egress rule of the ALB of alb_sg:

  egress {
    description     = "Traffic from ALB to ECS"
    from_port       = 80
    to_port         = 80
    protocol        = "tcp"
    security_groups = [var.ecs_sg] //comes from output from ecs stack
  } 

Now in ECS I have something like this for its security group (ecs_sg)

  ingress {
    description = "Allow ALB to ECS"
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    security_groups = [var.alb_sg] //comes from output from alb stack
  }

Now I have some error:

│ Error: Cycle: module.ecs.var.alb_sg (expand), module.ecs.aws_security_group.ecs_sg, module.ecs.output.ecs_sg (expand), module.alb.var.ecs_sg (expand), module.alb.aws_security_group.alb_sg, module.alb.output.alb_sg (expand)

How can this be solved? Thanks

like image 340
DenCowboy Avatar asked Apr 06 '26 18:04

DenCowboy


1 Answers

The solution is to create the SGs and SG rules separately. You can define the SG rules by using the aws_security_group_rule resource.

like image 94
jellycsc Avatar answered Apr 08 '26 20:04

jellycsc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!