Destroying resources gracefully in-order

Hello All :wave:

This is more of a general Terraform question and perhaps related to the Terraform SDK and the implementation of custom Providers.

For some background, I have written recently a new Terraform provider to create and manage a set of resources that are dependent on the existence of a set of virtual machines (in this case provided by the vsphere provider).

One of the only pieces of the puzzle I’m not able to get working correctly is destroying those resources or scaling them down gracefully. Terraform seems to destroy the underlying virtual machine resources (via vsphere provider) before applying the resource Update/Delete operations on the provider I wrote.

Despite there being an implied dependency between the two; that is I’m using as input to my provider data from the vsphere resources, Terraform still destroys things in the wrong order.

I’ve also tried explicitly setting a depends_on meta argument but that doesn’t yield the effect I want either, in fact it’s the same.

What am I missing here? Is there some way I can coerce Terraform that it must destroy/update one resource before another?

Kind regards

James

According to the docs:

Just like with apply , Terraform determines the order to destroy your resources. In this case, Terraform identified a single instance with no other dependencies, so it destroyed the instance. In more complicated cases with multiple resources, Terraform will destroy them in a suitable order to respect dependencies.

But I don’t really observe this behavior.

Similar issue

What version of Terraform are you using? v0.15.4 added some functionality around additional explanations in the output, iirc; not sure that’ll help you here, however.

I also noted that someone suggested exploring dependencies with the graph command; does that provide any insights in your case?

Overall, this sounds odd. I assumed depends_on would suit you perfectly.

Using Terraform v1.0.1

I’m exploring the terraform graph output now with terraform-grpah-beautfier to understand the dependencies at play here – I had to restructure out Terraform first though.

However note that depends_on which is an explicit control mechanisms here didn’t have much effort.

I’ll keep plugging away at this.

One thought though… As the creation of resource B depends on the creation of a list of resources A, and A is used as input into B I’m not even sure what I’m asking for is possible? Because the input to B would have to change before applying changes to B, and then finally destroying a subset of A.

Does this make sense? Put another way:

terraform apply ...:

  • Create A1, A2, A3
  • Create B1 from inputs [A1, A2, A3]

terraform destroy ...:

  • Drain A1
  • Drain A2
  • Drain A3
  • Destroy A1, A2, A3
  • Update B1

This is what I can’t seem to get out of Terraform :smiley:

1 Like