Bird
Raised Fist0
Terraformcloud~5 mins

Terraform state mv for refactoring - Cheat Sheet & Quick Revision

Choose your learning style10 modes available

Start learning this pattern below

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
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.

      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

      1. Step 1: Understand the role of terraform state mv

        The command is used to rename or move resource references inside the Terraform state file.
      2. Step 2: Confirm it does not affect actual infrastructure

        It only changes the state file, keeping the real infrastructure intact during code refactoring.
      3. Final Answer:

        To rename or move resources within the Terraform state file without changing actual infrastructure -> Option B
      4. 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

      1. Step 1: Recall the correct command structure

        The correct command starts with terraform state mv followed by the source and destination resource addresses.
      2. 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.
      3. Final Answer:

        terraform state mv aws_instance.old_name aws_instance.new_name -> Option A
      4. 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

      1. Step 1: Understand what terraform state mv does

        It only changes the resource name inside the Terraform state file, not the real resource.
      2. Step 2: Confirm no changes happen to actual AWS resources

        The S3 bucket in AWS remains unchanged; only Terraform's tracking name changes.
      3. Final Answer:

        The resource in the state is renamed to aws_s3_bucket.renamed_bucket without changing the actual bucket -> Option C
      4. 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

      1. Step 1: Analyze the error message

        The error clearly states the resource aws_instance.web is not found in the state file.
      2. Step 2: Understand implications

        This means the resource address is incorrect or the resource was never created or imported into the state.
      3. Final Answer:

        The resource aws_instance.web does not exist in the current Terraform state -> Option D
      4. 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

      1. Step 1: Identify full resource addresses including modules

        Resources inside modules have addresses prefixed by module.module_name.
      2. 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.
      3. Final Answer:

        terraform state mv module.old_module.aws_lambda_function.func module.new_module.aws_lambda_function.func -> Option A
      4. Quick Check:

        Use full module paths in state mv [OK]
      Hint: Include module prefix when moving resources inside modules [OK]
      Common Mistakes:
      • Omitting module prefix in resource addresses
      • Swapping source and destination
      • Using partial resource names