Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Terraform state mv for refactoring
📖 Scenario: You have a Terraform project managing cloud resources. You want to rename a resource in your Terraform configuration to improve clarity without destroying and recreating the resource in the cloud.
🎯 Goal: Learn how to use the terraform state mv command to rename a resource in the Terraform state file safely, reflecting the new resource name in your configuration.
📋 What You'll Learn
Create a Terraform configuration with a resource named aws_s3_bucket.old_bucket
Add a variable named new_bucket_name to hold the bucket name
Update the Terraform configuration to use the new resource name aws_s3_bucket.new_bucket
Use terraform state mv command syntax to rename the resource in the state
💡 Why This Matters
🌍 Real World
Renaming resources in Terraform without destroying and recreating them is common when refactoring infrastructure code to improve clarity or organization.
💼 Career
Understanding terraform state mv is essential for cloud engineers and DevOps professionals to safely manage infrastructure changes and avoid downtime.
Progress0 / 4 steps
1
Create initial Terraform resource
Create a Terraform configuration file named main.tf with a resource block for an AWS S3 bucket named old_bucket. Use the resource type aws_s3_bucket and set the bucket name to "my-old-bucket".
Terraform
Hint
Use the resource keyword, resource type aws_s3_bucket, and resource name old_bucket.
2
Add a variable for the new resource name
Add a Terraform variable named new_bucket_name with a default value of "my-old-bucket".
Terraform
Hint
Use the variable block with type = string and default = "my-old-bucket".
3
Update Terraform configuration with new resource name
Update the main.tf file to rename the resource block from old_bucket to new_bucket and set the bucket name to use the variable new_bucket_name.
Terraform
Hint
Change the resource name and use var.new_bucket_name for the bucket attribute.
4
Use terraform state mv command to rename the resource
Write the exact terraform state mv command to rename the resource from aws_s3_bucket.old_bucket to aws_s3_bucket.new_bucket.
Terraform
Hint
The command format is terraform state mv <old_address> <new_address>.
Practice
(1/5)
1. What is the primary purpose of the terraform state mv command?
easy
A. To delete resources from the Terraform state and infrastructure
B. To rename or move resources within the Terraform state file without changing actual infrastructure
C. To create new resources in the Terraform state
D. To backup the Terraform state file to a remote location
Solution
Step 1: Understand the role of terraform state mv
The command is used to rename or move resource references inside the Terraform state file.
Step 2: Confirm it does not affect actual infrastructure
It only changes the state file, keeping the real infrastructure intact during code refactoring.
Final Answer:
To rename or move resources within the Terraform state file without changing actual infrastructure -> Option B
Quick Check:
terraform state mv = rename/move state only [OK]
Hint: Remember: state mv changes state, not real resources [OK]
Common Mistakes:
Thinking it deletes or creates real resources
Confusing it with terraform apply
Assuming it backs up state automatically
2. Which of the following is the correct syntax to move a resource from aws_instance.old_name to aws_instance.new_name in Terraform state?
easy
A. terraform state mv aws_instance.old_name aws_instance.new_name
B. terraform mv state aws_instance.old_name aws_instance.new_name
C. terraform state move aws_instance.old_name aws_instance.new_name
D. terraform move state aws_instance.old_name aws_instance.new_name
Solution
Step 1: Recall the correct command structure
The correct command starts with terraform state mv followed by the source and destination resource addresses.
Step 2: Verify the order and keywords
Only terraform state mv aws_instance.old_name aws_instance.new_name uses the exact syntax: terraform state mv source destination.
Final Answer:
terraform state mv aws_instance.old_name aws_instance.new_name -> Option A
Quick Check:
Correct syntax = terraform state mv [OK]
Hint: Use 'terraform state mv' followed by source and destination [OK]
Common Mistakes:
Swapping 'mv' and 'state' keywords
Using 'move' instead of 'mv'
Incorrect command order
3. Given the Terraform state contains a resource aws_s3_bucket.my_bucket, what will be the result after running terraform state mv aws_s3_bucket.my_bucket aws_s3_bucket.renamed_bucket?
medium
A. Terraform deletes the old bucket and creates a new bucket named renamed_bucket
B. The actual S3 bucket is renamed in AWS to renamed_bucket
C. The resource in the state is renamed to aws_s3_bucket.renamed_bucket without changing the actual bucket
D. The command fails because resource names cannot be changed
Solution
Step 1: Understand what terraform state mv does
It only changes the resource name inside the Terraform state file, not the real resource.
Step 2: Confirm no changes happen to actual AWS resources
The S3 bucket in AWS remains unchanged; only Terraform's tracking name changes.
Final Answer:
The resource in the state is renamed to aws_s3_bucket.renamed_bucket without changing the actual bucket -> Option C
Quick Check:
State rename ≠ real resource rename [OK]
Hint: State mv changes state only, not real cloud resources [OK]
Common Mistakes:
Assuming AWS resources are renamed automatically
Expecting resource recreation
Thinking the command will fail
4. You run terraform state mv aws_instance.web aws_instance.app but get an error: Resource aws_instance.web not found in state. What is the most likely cause?
medium
A. You need to run terraform apply before moving state
B. The syntax of the command is incorrect
C. Terraform state file is corrupted and cannot be read
D. The resource aws_instance.web does not exist in the current Terraform state
Solution
Step 1: Analyze the error message
The error clearly states the resource aws_instance.web is not found in the state file.
Step 2: Understand implications
This means the resource address is incorrect or the resource was never created or imported into the state.
Final Answer:
The resource aws_instance.web does not exist in the current Terraform state -> Option D
Quick Check:
Resource not found = wrong address or missing resource [OK]
Hint: Check resource names exist in state before moving [OK]
Common Mistakes:
Assuming syntax error without checking resource name
Trying to move before resource creation
Ignoring error details
5. You want to refactor your Terraform code by moving a resource from module old_module to new_module. Which command correctly moves the resource aws_lambda_function.func in the state file?
hard
A. terraform state mv module.old_module.aws_lambda_function.func module.new_module.aws_lambda_function.func
B. terraform state mv aws_lambda_function.func module.new_module.aws_lambda_function.func
C. terraform state mv module.old_module.aws_lambda_function.func aws_lambda_function.func
D. terraform state mv module.new_module.aws_lambda_function.func module.old_module.aws_lambda_function.func
Solution
Step 1: Identify full resource addresses including modules
Resources inside modules have addresses prefixed by module.module_name.
Step 2: Use terraform state mv with full source and destination addresses
To move from old_module to new_module, specify both full addresses correctly.
Final Answer:
terraform state mv module.old_module.aws_lambda_function.func module.new_module.aws_lambda_function.func -> Option A
Quick Check:
Use full module paths in state mv [OK]
Hint: Include module prefix when moving resources inside modules [OK]