Terraform: Remove Invalid Resources
Description:
So during my terraform upgrades I came across issues where I would try to update a resource, the terraform plan would fail, and then I ended up having to remove the resource. But sometimes it would still fail even though all references to the resource were commented out in the code. In cases like this sometimes it’s best to just find the resource in your statefile and remove it completely.
NOTE: This should almost never be used as I don’t know the full implications but messing with the statefile is usually not the right answer. You have been warned!
To Resolve:
-
First, download your statefile locally and do a
terraform state list
to get the reference to the object you want to remove completely. -
Next, just add a step in your release pipeline that runs commands to remove them from the state file before
terraform plan
but afterterraform init
.1 2
terraform state rm 'module.mymodule1' terraform state rm 'module.mymodule2'
-
Note that I have only ever done this to get my
terraform plan
to be clean and then readded the resources back if they existed before.
Comments