Learn how to safely and efficiently migrate your Terraform state between projects for seamless infrastructure management.
terraform state mv
terraform state pull
and terraform state push
This guide provides a comprehensive overview of migrating your Terraform state, a crucial step in managing your infrastructure as code. We'll explore different migration methods, each suited for specific scenarios, and provide step-by-step instructions for a seamless transition. Whether you're moving resources within the same backend, switching to a remote backend, or making bulk changes, we've got you covered. We'll also share essential tips for a safe and successful migration, including backing up your state, using version control, and testing in a non-production environment.
Choose your migration method:
terraform state mv
: Best for moving individual resources within the same backend.terraform state pull
and terraform state push
: Suitable for migrating between different backends or making bulk changes.For terraform state mv
:
module.example.aws_instance.web
).terraform state mv [options] <SOURCE_ADDRESS> <DESTINATION_ADDRESS>
<SOURCE_ADDRESS>
and <DESTINATION_ADDRESS>
with the resource's current and desired locations.For terraform state pull
and terraform state push
:
terraform state pull > current.tfstate
current.tfstate
(e.g., to change resource names or paths).terraform state push current.tfstate
For backend configuration changes:
terraform
block in your configuration with the new backend details.terraform init
: Terraform will prompt you to migrate the state.Verification:
terraform plan
: Ensure no unexpected changes are detected.terraform show
or your backend's tools to confirm the migration's success.Additional Tips:
This document provides code examples for migrating Terraform state using methods like terraform state mv for moving resources within a state file, terraform state pull and push for migrating to a remote backend, and directly changing backend configuration. It includes scenarios for AWS and Consul backends, emphasizing the importance of backing up state files, verifying migration success with terraform plan and show, and testing in non-production environments.
This document provides code examples for different Terraform state migration methods.
Important: Always back up your state file before performing any migration!
Scenario: Moving an AWS instance to a different module within the same state file.
# Original resource location
resource "aws_instance" "web" {
# ... instance configuration ...
}
# New resource location
module "web_server" {
source = "./modules/web"
}
Command:
terraform state mv aws_instance.web module.web_server.aws_instance.web
Scenario: Migrating state from local to a remote backend (AWS S3).
Step 1: Pull the current state:
terraform state pull > current.tfstate
Step 2: Configure the new backend (in your Terraform configuration):
terraform {
backend "s3" {
bucket = "your-terraform-state-bucket"
key = "your-project/terraform.tfstate"
region = "your-aws-region"
}
}
Step 3: Push the state to the new backend:
terraform init # Initialize the new backend
terraform state push current.tfstate
Scenario: Switching to a remote backend (Consul) for the first time.
Step 1: Update the backend configuration:
terraform {
backend "consul" {
address = "your-consul-address:8500"
path = "your-project/terraform.tfstate"
}
}
Step 2: Initialize Terraform and migrate the state:
terraform init
Terraform will detect the backend change and prompt you to migrate the state. Follow the on-screen instructions.
After any migration, verify the following:
terraform plan
: Ensure no unexpected changes are detected.terraform show
or your backend's tools to confirm the migration's success.Example:
terraform plan
terraform show -json > state.json # Inspect the state file
Remember:
terraform state mv
, ensure you use the complete and accurate resource addresses for both the source and destination. Typos can lead to unintended consequences.current.tfstate
file directly is possible for terraform state pull
and terraform state push
, proceed with extreme caution. Only modify the file if you have a strong understanding of its structure and the potential impact of changes.terraform state list
, terraform state show
, and terraform state diff
commands can help diagnose problems.This table summarizes different methods for migrating Terraform state:
| Method | Description
By following the outlined methods and best practices, you can confidently migrate your Terraform state, ensuring a smooth transition for your infrastructure management. Remember to prioritize state backups, leverage version control, and thoroughly test your migration process in a controlled environment. As your infrastructure scales and evolves, mastering Terraform state migration becomes increasingly vital for maintaining a robust and well-managed infrastructure as code setup.