I have a Terraform test file, with the following (redacted) content:
run "setup" {
...
}
run "create_provider" {
...
}
run "create_consumer_same_region" {
...
}
run "create_consumer_different_region" {
...
}
- The
setup
run step is a dependency for all the other run steps create_provider
is also used by bothcreate_consumer_same_region
andcreate_consumer_different_region
The test creation works fine and all the tests are succeeding. However, when the destroy phase of the test is executed, the create_provider
run step can’t be destroyed because other resources still depend on the resources this step creates.
The DEBUG logs of Terraform during a test show the following:
$ grep -e "TestFileRunner: starting plan" -e "TestFileRunner: starting destroy" tf-1.11.log
2025-04-25T13:18:12.929+0200 [DEBUG] TestFileRunner: starting plan for tests/defaults.tftest.hcl/setup
2025-04-25T13:18:13.229+0200 [DEBUG] TestFileRunner: starting plan for tests/defaults.tftest.hcl/create_provider
2025-04-25T13:18:21.085+0200 [DEBUG] TestFileRunner: starting plan for tests/defaults.tftest.hcl/create_consumer_same_region
2025-04-25T13:19:10.772+0200 [DEBUG] TestFileRunner: starting plan for tests/defaults.tftest.hcl/create_consumer_different_region
2025-04-25T13:21:45.517+0200 [DEBUG] TestFileRunner: starting destroy plan for tests/defaults.tftest.hcl/create_consumer_different_region
2025-04-25T13:24:10.068+0200 [DEBUG] TestFileRunner: starting destroy plan for tests/defaults.tftest.hcl/create_provider
2025-04-25T13:24:13.569+0200 [DEBUG] TestFileRunner: starting destroy plan for tests/defaults.tftest.hcl/setup
We can see that the plans (and related applies) initially execute in the correct order. However, the destroy for the create_consumer_same_region
run step doesn’t seem to be execute at all. Thus, the destroy phase for create_provider
fails because resources created in create_consumer_same_region
still exist.
If I comment out any of the 2 create_consumer_*
run steps so that only one is executed, all the destroy plans execute correctly and the test suite succeeds. However, it’s when the 2 create_consumer_*
steps are present in the file that only one gets destroyed and the test suite fails by not being able to destroy all the resources created (and there are then leftover resources that must be manually cleaned up).
I’m not sure what could create this behavior. Is there a specific configuration in Terraform that would prevent running the destroy phase in specific scenarios?