terraform,

Migrating Terraform state the other way around: remote to local

Jul 07, 2022 · 1 min read · Post a comment

Migrating Terraform state to a remote location such as a cloud object storage is a no-brainer since it solves the shared access to state challenge when working with multiple members and teams.

What about the other way around? Not so common scenario, though, except maybe when moving the tfstate to another remote location. Here are the steps involved.

Prerequisites

  • Terraform
  • Cloud object storage

Solution

Step 1. Run terraform state pull to download and output the raw state to the standard output (stdout).

Additionally, if you want to save the output to a file, use one of the following:

terraform state pull > terraform.tfstate
terraform state pull | tee terraform.tfstate

Step 2. Remove the TF backend related code usually saved under a backend.tf file. For instance:

terraform {
  backend "gcs" {
    bucket = "devcoops-tfstate"
  }
}

Step 3. Run terraform init -migrate-state. You might be prompted to type yes and hit Enter.

Step 4. Once you deploy and configure your new remote state location, update the backend configuration appropriately (backend.tf).

Step 5. Finally, run terraform init once again. You’ll be prompted to type yes and hit Enter.

Conclusion

Tried everything and nothing works? Let me know. On a side note, follow our official channel on Telegram.