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
afigure out what pieces of state you'd like to move (terraform state listalong withgrepcan be useful here) - Create a local file in project
acalledtransfer.tfstate - Run
terraform state mv -state-out=transfer.tfstate RESOURCE_NAME RESOURCE_NAMEfor each resource to move - Move
transfer.tfstateinto projectb - Run
terraform state pull > ./destination.tfstateto pull the project's remote state into a file - Run
terraform state mv -state=transfer.tfstate -state-out=destination.tfstate RESOURCE_NAME RESOURCE_NAMEfor each resource to move - Run
terraform state push ./destination.tfstateto 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.