How to use state rm on a deposed resource

I got into a weird situation where I need to remove a deposed resource from the state file - I don’t want terraform to destroy it. I don’t know how to address it for a terraform state rm command. It seems I have to manually edit the state file?

How I got into this situation is the subject of another post I should make…

Hey Mark, you should definitely write another post on how you got into the situation! :wink:

1 Like

Hi Mark!

One idea of is to protect the resource from deletion on the cloud provider side, but I am guessing that might not be possible. So far, most attempts (and safest) to solve this have been to edit the state file manually. The deposed information will not show up as part of terraform state. You will have to go into the state file and remove the attributes under the deposed instance.

Curious to know if you figured it out!

I had an aws_ecs_cluster (inside of a module) set up named “foo” and then used the same module later to set up another cluster. It also named the aws_ecs_cluster as “foo” but other pieces failed because of pre-existing names. Note that these two module instances were in different workspaces/state files. Now I know that the aws_ecs_cluster resource does not care if there is already an existing cluster of the same name - it will happily report that it has created the cluster and record that in the state file.

So, now I have the same aws_ecs_cluster resource in two different state files. I do some work on my module so that it names the cluster based on the environment that I want it for so that now my second instance of this module will name the aws_ecs_cluster as “foo-staging”. A plan now shows, however, that the original is deposed and will be destroyed because I have renamed the resource. I don’t want it to destroy that resource because it is actually the same resource that my original state file is tracking. So, I had to go in an edit the state file to just remove that deposed resource.

Yes, I removed the resource manually from the state file. It would be nice to have the terraform state be able to cover this. It would also be nice to be able to destroy a deposed resource directly, too.


1 Like