0
0
Terraformcloud~5 mins

Terraform state mv for refactoring - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the terraform state mv command?
It moves resources in the Terraform state file from one address to another without changing the actual infrastructure. This helps when refactoring or renaming resources.
Click to reveal answer
beginner
When should you use terraform state mv?
Use it when you rename or reorganize resources in your Terraform code but want to keep the existing infrastructure intact and avoid recreating resources.
Click to reveal answer
intermediate
What happens if you rename a resource in code but don't update the state with terraform state mv?
Terraform will think the old resource was deleted and a new one needs to be created, which can cause downtime or data loss.
Click to reveal answer
beginner
How do you move a resource named aws_instance.old_name to aws_instance.new_name in Terraform state?
Run terraform state mv aws_instance.old_name aws_instance.new_name to update the state file accordingly.
Click to reveal answer
advanced
Can terraform state mv move resources between different Terraform workspaces or backends?
No, it only moves resources within the same state file. Moving between workspaces or backends requires different approaches like state pull and push.
Click to reveal answer
What does terraform state mv do?
ADeletes resources from the cloud provider
BUpdates Terraform version
CCreates new resources in the cloud
DMoves resources in the state file without changing infrastructure
Why use terraform state mv after renaming a resource in code?
ATo avoid Terraform recreating the resource
BTo delete the old resource
CTo upgrade Terraform
DTo backup the state file
Which command moves aws_s3_bucket.old to aws_s3_bucket.new in state?
Aterraform mv aws_s3_bucket.old aws_s3_bucket.new
Bterraform state mv aws_s3_bucket.old aws_s3_bucket.new
Cterraform state rename aws_s3_bucket.old aws_s3_bucket.new
Dterraform move aws_s3_bucket.old aws_s3_bucket.new
Can terraform state mv move resources between different backends?
AYes, but only for S3 backends
BYes, automatically
CNo, it only works within the same state file
DOnly if you use the -backend flag
What risk exists if you rename a resource in code but don't run terraform state mv?
ATerraform will destroy and recreate the resource
BTerraform will ignore the resource
CTerraform will crash
DTerraform will merge the resources
Explain how and why you would use terraform state mv during a resource refactoring.
Think about what happens if you rename a resource but don't update the state.
You got /4 concepts.
    Describe the limitations of terraform state mv and when it cannot be used.
    Consider different Terraform environments and storage backends.
    You got /3 concepts.