Controlling order of resource/module replacement

Hello,

I’m struggling with trying to control the replacement order/cadence for my project:

I have two EC2 instances that I need to be replaced on change (such as AMI change, etc) one at a time in order to avoid downtime. From my understanding and observation, Terraform will only destroy everything first before it creates anything.

I’d like to destroy + create Instance1 before destroying + creating Instance2. I have tried using depends_on but this will only implement the order of the destruction or creation but both instances will still be destroyed before they are then both created in whichever order.

Options I am aware of but are not currently viable:

  • For my project, I am unable to utilize terraform apply -replace to target a resource or any other customization of terraform apply
  • changing to using AutoScalingGroups
  • create_before_destroy (instances have network interfaces so this isn’t very feasible)

Any tips or help would be greatly appreciated! Thanks very much in advance for your time and consideration.

Terraform isn’t an orchestration tool.

To do orchestration involving Terraform you either need to be creating resources which do it (e.g. creating ASGs instead of EC2 directly) or having a tool that calls Terraform in different ways (e.g. passing in different variable values across multiple applies).

As you seem to have discounted both options I don’t believe there is anything else to suggest.