Terraform Associate 004 ยท Maintaining infrastructure
Moving and removing state entries
terraform state mv and rm, moved blocks for refactoring, removed block, effects on real infrastructure (7b).
Key points
- terraform state mv SOURCE DESTINATION renames an entry in state so a rename does not become destroy-and-create. terraform state rm ADDRESS makes Terraform forget an object; the real object keeps running.
- Declarative alternatives go through plan and review: a moved block (from, to) for renames and a removed block (from, with lifecycle destroy = false) to stop managing without destroying.
Source: https://developer.hashicorp.com/terraform/cli/commands/state/mv
Practice questions
What happens to the real object when you run terraform state rm on it?
- It keeps existing but Terraform no longer manages it
- It is destroyed immediately
- It is renamed in the cloud account
- It is replaced on the next apply
Show answer
Answer: A. It keeps existing but Terraform no longer manages it
state rm only deletes Terraform's record. Destruction would need a destroy, and nothing renames or replaces the object.
Which statements about the removed block are true?
- from is required and holds the address to remove
- lifecycle with destroy = false leaves the real object in place
- Without destroy = false, the real object is destroyed too
- It needs a to argument naming the new address
- Its provisioners must use when = create
Show answer
Answer: A. from is required and holds the address to remove, B. lifecycle with destroy = false leaves the real object in place, C. Without destroy = false, the real object is destroyed too
from is required, destroy = false is how you hand off management, and by default the object is destroyed. There is no to argument, and removed-block provisioners must use when = destroy.
terraform state rm destroys the real infrastructure object it removes from state.
Show answer
Answer: False
False. It forgets the object only; it keeps running unmanaged. If the config still declares it, the next plan will want to create another one.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
