terraform state mv
in Delicate Circumstances
Sometimes you have a preexisting resource that you'd like to replace with another version. That's why the terraform state mv
command exists. The Terraform documentation will give you an outline of the different circumstances in which you can use it (including when moving resource between state files).
Steps
- Add the new instance of the resource (or the module it lives in).
- Run
terraform state mv
with the appropriate arguments. - Delete the initial instance.
- Run
terraform plan
to ensure you don't have a remaining resource.
Complications
Sometimes you will be wary about running state move because you aren't sure that your move makes sense (i.e. in the case of a clean plan but dirty apply). In that case, you can safeguard by first pulling state locally and then moving. If your plan goes sideways, you can revert to the earlier state via state push.