Move Terraform remote state between tf projects

Deepthi Nalla
2 min readJul 7, 2020

If you are using Terraform for managing Infrastructure as code, you might run into a situations where you will need to cherry pick a few terraform resources from one remote state file and move them to another remote state file. This blog post will help you to do just that.

Below are the series of steps that you can follow to move resources from one remote state file to another terraform state file.

Lets make a few assumptions

  1. There are two tf projects namelysource-tf-project and dest-tf-project
  2. Both projects use remote backends to manage state
  3. Both projects have multiple resources in their remote state

Goal is to move a gcs bucket named as bucket_in_src created in source-tf-project to bucket_in_dest in dest-tf-project

  1. Pull the remote state file of dest-tf-projecttf project to local machine
pushd dest-tf-project
terraform init
terraform state pull > terraform.tfstate
popd

2. Then move the resource

pushd source-tf-project
terraform init
terraform state mv --state-out=dest-tf-project/terraform.tfstate google_storage_bucket.bucket_in_src google_storage_bucket.bucket_in_dest
popd

In the above, google_storage_bucket.bucket_in_src is being moved from the tf state ofsource-tf-project to google_storage_bucket.bucket_in_destin dest-tf-project ‘s state.

It is possible to move the resources and use the same name as well. So, the below is also correct as long as the destination resource is already defined in the tf files in dest-tf-project

pushd source-tf-project
terraform init
terraform state mv --state-out=dest-tf-project/terraform.tfstate google_storage_bucket.bucket_in_src google_storage_bucket.bucket_in_src
popd

3. Check if the dest-tf-project state has the resource that was moved from soruce-tf-project

pushd dest-tf-project
terraform state list
popd

4. Finally push the remote state file of dest-tf-project that we pulled to local in the step #1

pushd dest-tf-project
terraform state push terraform.tfstate
popd

That’s all. Happy terraform state moving :)

--

--