What is the least painful way to migrate state of resources from one project (i.e., move a module invocation) to another, particularly when using remote state storage? While refactoring is relatively straightforward within the same state file (i.e., take this resource and move it to a submodule or vice-versa), I don't see an alternative to JSON surgery for refactoring into different state files, particularly if we use remote (S3) state (i.e., take this submodule and move it to another project).
To migrate with the Terraform CLI, add the cloud block to your configuration, specify one or more Terraform Cloud workspaces for the state files, and run terraform init . If the workspaces you choose do not yet exist, Terraform Cloud creates them automatically in the specified organization.
»Command: state mv.
The least painful way I’ve found is to pull both remote states local, move the modules/resources between the two, then push back up. Also remember, if you’re moving a module, don’t move the individual resources; move the whole module.
For example:
cd dirA terraform state pull > ../dirA.tfstate cd ../dirB terraform state pull > ../dirB.tfstate terraform state mv -state=../dirA.tfstate -state-out=../dirB.tfstate module.foo module.foo terraform state push ../dirB.tfstate # verify state was moved terraform state list | grep foo cd ../dirA terraform state push ../dirA.tfstate
Unfortunately, the terraform state mv command
doesn’t support specifying two remote backends, so this is the easiest way I’ve found to move state between multiple remotes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With