If you need to move resources between Terraform projects but they aren't configured in such a way you can directly run tf state move
to move them between modules you can follow this process for migrating resources between terraform projects.
Imagine you need to move some resources from Terraform project a
into project b
without disturbing the remote resources themselves.
The basic process for moving state would work like the following:
- In Terraform project
a
figure out what pieces of state you'd like to move (terraform state list
along withgrep
can be useful here) - Create a local file in project
a
calledtransfer.tfstate
- Run
terraform state mv -state-out=transfer.tfstate RESOURCE_NAME RESOURCE_NAME
for each resource to move - Move
transfer.tfstate
into projectb
- Run
terraform state pull > ./destination.tfstate
to pull the project's remote state into a file - Run
terraform state mv -state=transfer.tfstate -state-out=destination.tfstate RESOURCE_NAME RESOURCE_NAME
for each resource to move - Run
terraform state push ./destination.tfstate
to overwrite the remote state with what is in the local file
If the transfer was successful running terraform plan
in either project should show a noop of changes.
If you have a lot of resources to move see the attached helper scripts. These help automate steps 3 and 6 if you need to run for multiple resources.
Just replace the resources
array in the bash script with an array of resources to export or import and run the script to automate the mv
commands.