-1

At the moment Terraform v1.1.7 is used to create AWS ASG's and a LB.

The LB TG's are attached to the ASG's via the following external attachment resource:

resource "aws_autoscaling_attachment" "gateway_lb_attachment" {
  for_each = toset(local.asg_names)
  autoscaling_group_name = each.value
  lb_target_group_arn   = aws_lb_target_group.gateway_tg.arn
}

Inside the ASG resource the following lifecycle rule exists:

  lifecycle {
    create_before_destroy = true
    ignore_changes = [ load_balancers, target_group_arns ]
  }

And inside the Launch Template there is:

  lifecycle {
    create_before_destroy = true
  }

When the LB is destroyed, it triggers an ASG destroy. From the above config, my understanding is the ASG should remain in place.

Is there something missing?

Marko E
  • 13,362
  • 2
  • 19
  • 28
Theo Sweeny
  • 1,033
  • 14
  • 26

1 Answers1

0

After playing with various configurations I found a winner.

The earlier version which was failing where the ASG names were being complied within the module as a local E.G.

locals {

    asg_names = compact([for i in tolist([
      var.remote_state_lookup.outputs.asg_app_group_name,
      var.remote_state_lookup.outputs.asg_cicd_group_name,
      var.remote_state_lookup.outputs.asg_es_group_name
    ]) : i == "null" ? "" : i])
}

By moving this to the instantiator of the module and using module outputs rather than the remote state datasource, addressed the issue!

E.G.

Inside the Load Balancer module change the local to var:

variable "asg_names" {
  type    = list(string)
  default = null
}

Now set it's value in the module instantiator:

  asg_names = compact([for i in tolist([
    try(module.eks_app_nodes[0].asg_group_name, "null"),
    try(module.eks_cicd_nodes[0].asg_group_name, "null"),
    try(module.eks_es_nodes[0].asg_group_name, "null")
  ]) : i == "null" ? "" : i])
Theo Sweeny
  • 1,033
  • 14
  • 26