By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(terraform state mv, rm, show, list)
Terraform state is the single source of truth for your infrastructure. It maps real-world resources (AWS EC2 instances, S3 buckets, etc.) to your Terraform configuration. If the state file is wrong, Terraform thinks your infrastructure is in a different state than it actually is—and chaos ensues.
Why this matters in production:- You rename a resource in your .tf files, but Terraform tries to destroy and recreate it (downtime, data loss).- You refactor a module, and Terraform loses track of existing resources (orphaned infrastructure, cost leaks).- You inherit a legacy codebase with mismatched state, and you need to fix it without nuking everything.
.tf
Real-world scenario:You’re migrating an EC2 instance from one VPC to another. Instead of destroying and recreating it (which would cause downtime), you use terraform state mv to update the state file to reflect the new VPC ID. This keeps the instance running while Terraform now tracks it correctly.
terraform state mv
terraform state
terraform.tfstate
terraform state list
terraform state show <resource>
terraform state mv <source> <destination>
terraform state rm <resource>
-dry-run
mv
>= 1.0.0
You have an EC2 instance named web_server, but you want to rename it to app_server without destroying it.
web_server
app_server
# main.tf provider "aws" { region = "us-east-1" } resource "aws_instance" "web_server" { ami = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 instance_type = "t2.micro" tags = { Name = "web_server" } }
Run:
terraform init terraform apply -auto-approve
Verify:
terraform state list # Should show "aws_instance.web_server" aws ec2 describe-instances --filters "Name=tag:Name,Values=web_server" # Should return the instance
Edit main.tf:
main.tf
- resource "aws_instance" "web_server" { + resource "aws_instance" "app_server" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" tags = { - Name = "web_server" + Name = "app_server" } }
Without terraform state mv, Terraform would:1. Destroy web_server.2. Create a new app_server.
Instead, we move the state:
terraform state mv aws_instance.web_server aws_instance.app_server
terraform state list # Should now show "aws_instance.app_server" aws ec2 describe-instances --filters "Name=tag:Name,Values=app_server" # Should return the same instance
terraform apply -auto-approve
Expected output:
aws_instance.app_server: Refreshing state... [id=i-1234567890abcdef0] No changes. Your infrastructure matches the configuration.
✅ Success! The instance was not recreated.
terraform state rm
terraform destroy
terraform plan
aws_instance.web
module.web.aws_instance.server
terraform state mv -dry-run
terraform import
rm
terraform apply
❌ Trap: "Terraform updates the resource in place" (wrong—it treats it as a new resource).
"How do you safely rename a resource without destroying it?"
terraform state mv old_name new_name
❌ Trap: "Edit terraform.tfstate manually" (never do this).
"You need to remove a resource from Terraform state but keep it in AWS. What command do you use?"
terraform state rm aws_instance.example
destroy
import
You have a Terraform-managed S3 bucket named my-bucket-123. You want to: 1. Rename it to my-bucket-prod in code.2. Update the state without recreating the bucket.
my-bucket-123
my-bucket-prod
Solution:
# 1. Rename in code (main.tf) resource "aws_s3_bucket" "my_bucket" { bucket = "my-bucket-prod" # Changed from "my-bucket-123" } # 2. Update state terraform state mv aws_s3_bucket.my_bucket aws_s3_bucket.my_bucket # 3. Verify terraform plan # Should show no changes
Why it works:- terraform state mv updates the state file to match the new name.- Terraform now tracks the same bucket under the new name.
terraform state show aws_instance.web
terraform state mv <source> <dest>
terraform state mv aws_instance.web aws_instance.app
terraform state rm aws_s3_bucket.old
terraform state mv -dry-run aws_instance.web aws_instance.app
terraform import <resource> <id>
terraform import aws_instance.web i-1234567890
Terraform state manipulation is like surgery for your infrastructure—precise, high-stakes, and irreversible if done wrong. Master these commands, and you’ll save hours of downtime when refactoring. Ignore them, and you’ll accidentally delete production resources.
Now go fix that state file! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.