0

I'm migrating some stuff in a terraform repo and replacing Resource A with Resource B, of different types. If the details are important, A is a CloudFormation stack, B is a native terraform resource for the same thing.

I want to tell Terraform to delete resource A before it tries to create resource B, as they set up the same thing and otherwise I get errors from AWS about duplicate things.

Can I be clever with my use of depends_on in order to get it to delete in the order I want?

From the docs:

The depends_on meta-argument instructs Terraform to complete all actions on the dependency object (including Read actions) before performing actions on the object declaring the dependency.

Does it do the same in reverse for a deletion? Can I therefore say that B depends_on A and get it to do all the actions on A (deletion) before creating B? Or is there another way of doing this?

At the moment, I just have to do 2 runs and let the first one fail. But it feels like there /should/ be a way to get this to work in 1...

1 Answer 1

0

If you are replacing the Terraform configuration for an existing resource, then you don't need to delete the resource. You can remove Resource A from state then add Resource B. The following steps are helpful:

  1. Run terraform state rm to remove Resource A from Terraform State
  2. Remove Resource A Terraform configuration code
  3. Add Resource B Terraform configuration code
  4. Run terraform import to add the state for the existing Resource B to state
1
  • Unfortunately in this case I do need to delete the resource, as it's a CloudFormation stack that's creating the actual things, being replaced by native terraform resources for those things. So I'm removing an intermediate step.
    – shearn89
    Commented Apr 12 at 9:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .