Issue and Impact
During a Terraform run, an attempt was made to create a resource that already existed in the infrastructure. This results in a drift between the actual infrastructure and the Terraform state file, leading to unnecessary planned changes.
Cause
Upon investigation, it can be identified that the particular resource could be missing from the latest Terraform state file.
-
A previous Terraform run could have failed unexpectedly with a
409 Conflict
error. -
During that run, the resource in question was marked for deletion and then recreation.
-
The intermediate state where the resource could be "deleted"or uploaded to the workspace.
-
However, the final state file (after the resource was recreated) was never uploaded due to the failed run.
-
As a result, the resource no longer appears in the current state, though it still exists in the actual infrastructure.
- To verify the state file resources, you can download the state file from HCP workspace and try to find out the particular resource .
Solution
-
The missing resource can be re-imported into the workspace using Terraform’s import functionality.
-
A subsequent
terraform plan
confirms that there is no changes, indicating the state is aligned with the real infrastructure. -
The import block can then be removed from the code to maintain consistency across multiple workspaces.
More helpful information
Here are some useful resources to better understand state handling and recovery in Terraform: