004 · Domain 6: Terraform state management · 13% of the exam
Task 6.4: Manage resource drift and Terraform state
Seeing and fixing drift: refresh-only plans and applies, -replace, moved blocks for refactoring, and removed blocks that forget an object without destroying it.
Study it
Drift and refactoring: refresh-only, moved and removed blocks
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
During an incident, engineers changed several resources directly in the cloud console. Before deciding what to do, the team wants to see exactly how those objects now differ from what Terraform recorded, without any proposal to change infrastructure. Which command fits?
Aterraform plan -compact-warnings
Bterraform output -json
Cterraform plan -refresh-only
Dterraform validate
Show the answer and why
Aterraform plan -compact-warnings
Incorrect
That only shortens warnings; the normal plan would still propose reverting the changes.
Bterraform output -json
Incorrect
output prints root output values, not differences in resources.
Cterraform plan -refresh-only
Correct
A refresh-only plan shows what changed in the remote objects outside Terraform and proposes no infrastructure changes.
Dterraform validate
Incorrect
validate never contacts the infrastructure.
Refresh-only mode is for looking at drift on its own, separately from configuration changes.
A developer renames resource "aws_instance" "web" to "app" for clarity, and the plan shows the instance being destroyed and a new one created. Which change to the configuration lets the rename keep the existing instance?
AAdd a moved block from aws_instance.web to aws_instance.app
BAdd an import block that imports the instance's ID to aws_instance.app
CAdd lifecycle { ignore_changes = all } to the renamed block
DRun terraform apply -refresh-only before planning the rename again
Show the answer and why
AAdd a moved block from aws_instance.web to aws_instance.app
Correct
A moved block tells Terraform that the existing object now belongs to the new address, so it updates state instead of replacing the object.
BAdd an import block that imports the instance's ID to aws_instance.app
Incorrect
Import blocks bring in objects that Terraform does not manage yet. This instance is still recorded under aws_instance.web, which has no block any more, so the plan still destroys that address.
CAdd lifecycle { ignore_changes = all } to the renamed block
Incorrect
ignore_changes stops Terraform from updating arguments that changed; it does not connect a new address to an existing object.
DRun terraform apply -refresh-only before planning the rename again
Incorrect
A refresh-only apply updates state from the real objects; the instance stays recorded under the old address, so the rename still plans a replacement.
Addresses identify objects in state; a moved block changes an address without touching the object.
A team renames a resource in its shared configuration. To keep the existing object, an engineer plans to run terraform state mv against the shared state shortly after the change is merged. What risk does the state mv documentation call out for this approach?
Astate mv destroys the object and creates it again at the new address
BA teammate who plans and applies between the merge and the state mv can replace the object
Cstate mv works with local state files, so the shared remote state keeps the old address
Dstate mv holds the state lock until the next apply completes
Show the answer and why
Astate mv destroys the object and creates it again at the new address
Incorrect
state mv changes only addresses in state; it never touches the remote object.
BA teammate who plans and applies between the merge and the state mv can replace the object
Correct
Until state mv runs, the new configuration and the old state disagree, so a plan made in that gap destroys the object at the old address and creates a new one at the new address.
Cstate mv works with local state files, so the shared remote state keeps the old address
Incorrect
The state subcommands work with remote state too; the risk is about timing, not storage.
Dstate mv holds the state lock until the next apply completes
Incorrect
Like other state subcommands, it holds the lock only while it runs.
A CLI refactor changes the configuration and the state in two separate steps. The documentation asks the engineer to coordinate with coworkers so that nobody plans or applies in between.
A team upgrades the Terraform CLI on its build agents from 1.10 to 1.12. Its existing state snapshots were all written by 1.10. What does the Terraform state documentation say about using them with 1.12?
AThe upgrade requires importing every resource again into a fresh state
BNew Terraform versions are generally backward compatible with snapshots written by earlier ones
CThe snapshots must first be converted by pushing them with terraform state push
DThe dependency lock file must be regenerated before 1.12 can read the state
Show the answer and why
AThe upgrade requires importing every resource again into a fresh state
Incorrect
Importing binds objects that Terraform does not track yet; an upgrade keeps the existing bindings.
BNew Terraform versions are generally backward compatible with snapshots written by earlier ones
Correct
The state documentation says new Terraform versions are generally backward compatible with state snapshots produced by earlier versions.
CThe snapshots must first be converted by pushing them with terraform state push
Incorrect
state push uploads a local state file to the configured backend; it does not convert anything.
DThe dependency lock file must be regenerated before 1.12 can read the state
Incorrect
The lock file records provider selections; it plays no part in reading state.
Upgrading the CLI does not mean rebuilding state. Plan with the new version as usual; the existing snapshots stay the source of truth.