So I’ve discovered an issue with our provider (still currently using the v2 SDK) that I’m not sure how to resolve because it’s unclear to me how it is even possible in the first place.
I have a ‘service’ resource that contains two nested blocks ‘A’ and ‘B’ (within our provider these nested blocks are setup as their own resources of sort and so they have their own CRUD lifecycle functions).
When it comes to our provider processing these nested blocks, they are done in the order ‘A’, then ‘B’.
I add the nested block ‘B’ to my service resource config and I successfully run a terraform apply
. This results in the main ‘service’ resource and the nested ‘B’ block resource both being created via our API.
The problem occurs when I add to my config an incorrectly configured nested block ‘A’, while at the same time removing the nested block ‘B’ from the configuration.
What happens (I’m using TF_LOG=TRACE
) when I run terraform apply
I see that our Terraform provider calls our API for the nested block ‘A’ (remember A is processed first by our provider) and the API returns an error (because we configured the nested block with an invalid attribute value). The Terraform provider then reports the error and stops. There is no call to our API for deleting the nested block ‘B’ and so that item still exists as far as our API is concerned.
Inside our Terraform provider it is setup such that ‘A’ is always processed before ‘B’ (hence why in the logs we can see the API for ‘A’ was called first, and because that failed we didn’t see a call to our API for ‘B’).
But if I now run terraform show
, I can see two things:
- Our ‘A’ nested block attribute exists in our state (with invalid value).
- Our ‘B’ nested block attribute is removed from the state.
Neither of which I expected because of the error returned when trying to create ‘A’.
I expected, once our Terraform provider had returned the error from the ‘A’ block’s Create function, because of the error, for no state changes to have been made.
Similarly, because the ‘B’ block’s Delete function was never actually called, I again expected there to be no state changes.
But clearly the state has been updated and this is confusing to me.
Any ideas why this might happen?
Thanks.