How to safely replace target groups

So, here’s what I need to do. I need to make a change to a an AWS elb target group that requires recreating it. In order to avoid taking any downtime, I need the changes to be done in the following order:

  1. Create the new target group (aws_lb_target_group)
  2. Add the target group to the list of target groups for an auto scaling group (aws_autoscaling_group)
  3. Change the aws_lb_listener to use the new target group rather than the old one
  4. remove the old target group from the auto scaling group
  5. delete the old target group

by using create_before_destroy on the target group, I can almost get, but I’m not sure how to split up steps 2 and 4, instead of changing the autoscaling group to remove and add the target groups at the same time.

Ideally, I’d also like to wait until the instances from the scale group are marked as healthy in the target group before doing step 3.

This is a specific case that I recently ran into, but there are other similar scenarios I’ve encountered in the past, where avoiding downtime needs finer control over the order of operations than terraform seems to give.

Does anyone have suggestions on how to deal with these kinds of changes?

1 Like