How to automatically destroy a resource while destroying other?

I need to create two resources A and B. B depends on A for a key it provides when A is created. Creating it all is fine, no issues whatsoever.

Now, when I say terraform destroy -target B, I want A to get destroyed as well automatically. I.e. I do not want to say destroy -target A B. (I don’t want to remember their relationship.)

Is there a way to do that?

B depends on A, so if you destroy A then B gets destroyed.
Amd you can destroy and re-create B without A being affected. This is how the graph works.

I find nothing in life_cycle covering what you wish to do, and I think that making A depend on B would be a bad idea.

Sorry, but I don’t see a way of doing that. Yes, you can introduce C which both A and B depends on. So, if you destroy C then A and B gets destroyed as well. But that doesn’t change what happens if you destroy B.

1 Like