To move a resource in state from one name to another without recreating it, which command should you use?

Prepare for the HashiCorp Terraform Associate Exam. Utilize flashcards and multiple-choice questions with detailed hints and explanations. Boost your confidence and be ready for success!

Multiple Choice

To move a resource in state from one name to another without recreating it, which command should you use?

Explanation:
Renaming a resource’s address while preserving the existing real resource is done by updating Terraform’s state rather than applying changes. Terraform’s state file is the source of truth that maps your configuration addresses to the actual resources in your cloud provider. When you rename a resource in your configuration, you want to move that resource’s entry in the state to the new address, so Terraform continues to manage the same resource without recreating it. Use the state mv command to move the state entry from the old address to the new one. In your example, you’d move aws_security_group.htp to aws_security_group.http, which tells Terraform to associate the existing security group with the new configuration address. After performing this move, you can run terraform plan to confirm that no changes are needed. Why the other options don’t fit: apply would attempt to align the real infrastructure with the new configuration, potentially creating or destroying resources rather than just renaming the address. refresh would update the state to reflect actual resources but doesn’t rename addresses in the state. The version without state in the command isn’t the correct way to modify the state; the proper form is terraform state mv.

Renaming a resource’s address while preserving the existing real resource is done by updating Terraform’s state rather than applying changes. Terraform’s state file is the source of truth that maps your configuration addresses to the actual resources in your cloud provider. When you rename a resource in your configuration, you want to move that resource’s entry in the state to the new address, so Terraform continues to manage the same resource without recreating it.

Use the state mv command to move the state entry from the old address to the new one. In your example, you’d move aws_security_group.htp to aws_security_group.http, which tells Terraform to associate the existing security group with the new configuration address. After performing this move, you can run terraform plan to confirm that no changes are needed.

Why the other options don’t fit: apply would attempt to align the real infrastructure with the new configuration, potentially creating or destroying resources rather than just renaming the address. refresh would update the state to reflect actual resources but doesn’t rename addresses in the state. The version without state in the command isn’t the correct way to modify the state; the proper form is terraform state mv.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy