Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform: How to migrate state between projects?

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).

like image 602
Nathan Avatar asked May 17 '18 20:05

Nathan


People also ask

How do you move terraform state?

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.

Which command is used to move state into a backend storage?

»Command: state mv.


1 Answers

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.

like image 128
RubberDuck Avatar answered Sep 18 '22 19:09

RubberDuck