Custom provider, specify resource dependancy or destroy order

I’m working on a custom provider (still learning a lot :slight_smile: ) and although I’ve a lot working already, I stumble upon the following;

I have a resource (type_a) that is being used in another resource (type_b).
Creation is working perfectly the Terraform file looks like.

resource type_a a1 {
  name = "test_a"
}

resource type_b b1 {
  name = "test_b"
  type_a_id = type_a.a1.id
}

The backend API doesn’t let me delete resource type_a when it is still being in use by other resources (type_b).

When I use Terraform destroy, the process fails, because resource test_a can not be deleted as long as test_b is still there. So I need a way to make sure resources of test_b are deleted before resources of type test_b. Is there a way to accomplish this, by setting dependencies or prioritizing the deletion order?

It has been a moment since I’ve had to reason about this, but I believe the lifecycle.create_before_destroy block is how this is usually addressed. That can only be controlled by users by setting it in the resource config, however; providers can’t enable that behavior on resources themselves. There’s an open issue tracking that enhancement.

Hi @rob.maas,
as per my understanding the order of destroy operations should be part of the terraform dependency graph - by default.

There might be a typo though as I’d expect type_a_id = type_a.a1.id.

Does the terraform state output reflect the expected dependencies?

If I look in the terraform state file then the expected dependencies are shown.
In this example: resource type_b1 shows under dependencies type_a.a1

p.s. it was indeed a type, I fixed it in the first post.

Which SDK version / terraform version are you using?

I sort of found the issue :slight_smile:

If I do a plan, apply, destroy it works, but if I change resource type_a before doing the destroy things break. I still have to figure out why, but I tend to think it is because the update of type_a changes the ID of the resource, but isn’t seen as a replace (=delete/create).

The versions I’m using:

  • sdk/v2 v2.4.0
  • terraform v0.14.3

According to the page I added below, when setting or changing the ID, Terraform assumes that the resource is destroyed. However even if I start the update function with d.SetID("") it is not being seen as a replacement. What am I missing here ?

I’ve found a solution and everything works now.
I missed the option “ForceNew”, this resolved all the problems.

Thanks for the help, greatly appreciated !