I’m working on a custom provider (still learning a lot ) 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.
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).
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 ?