002 · Domain 2: Terraform State and Backend Operations · 13% of the exam
Task 2.2: Manage state and remediate drift
Finding and fixing drift: refresh-only plans and applies, when to accept a change into state and when to overwrite it, and how backend reinitialization and plan options affect what Terraform compares.
Study it
Drift: refresh-only plans and deciding which side wins
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 2
During an incident, an engineer raised an autoscaling group's maximum size from 6 to 10 in the cloud console and also added a temporary debug tag. The team decides that 10 is the new standard and that the tag must go. Which two actions achieve both outcomes? (Choose TWO.)
AChange the maximum size in the configuration to 10
BRun terraform apply -refresh-only, which records the new maximum size and the added tag and keeps both
CRun a normal plan and apply, which proposes removing the tag added outside Terraform
DAdd lifecycle { ignore_changes = [tags] } to the autoscaling group
ERun terraform plan -refresh=false to revert the tag without touching the size
Show the answer and why
AChange the maximum size in the configuration to 10
Correct
With the configuration matching the new size, a normal plan no longer proposes changing the size back.
BRun terraform apply -refresh-only, which records the new maximum size and the added tag and keeps both
Incorrect
A refresh-only apply writes the console changes into state and never changes the group, so the debug tag stays in place.
CRun a normal plan and apply, which proposes removing the tag added outside Terraform
Correct
A normal plan refreshes the object and proposes updating it to match the configuration, which removes the tag Terraform does not declare.
DAdd lifecycle { ignore_changes = [tags] } to the autoscaling group
Incorrect
That keeps the debug tag forever, the opposite of what the team wants.
ERun terraform plan -refresh=false to revert the tag without touching the size
Incorrect
The plan then compares the configuration with the stored state only, which holds neither the tag nor the new size, so it proposes nothing for either.
Decide per attribute: write the values you keep into the configuration, and let a normal plan and apply revert the rest. Refresh-only mode reconciles state with reality but never changes the remote objects.
An on-call runbook says to run terraform refresh whenever someone changes infrastructure outside Terraform. A reviewer asks for terraform apply -refresh-only instead. Which risk is the reviewer avoiding?
Aterraform refresh auto-approves; with wrong credentials it can drop every object from state unprompted
Bterraform refresh changes the real infrastructure to match the configuration while it refreshes state
Cterraform refresh ignores state locking and lets two runs write the state at the same time
Dterraform refresh rewrites the configuration files to match remote objects, which loses edits
Show the answer and why
Aterraform refresh auto-approves; with wrong credentials it can drop every object from state unprompted
Correct
refresh is an alias for apply -refresh-only -auto-approve, and HashiCorp warns that misconfigured credentials can make Terraform remove all tracked objects without confirmation.
Bterraform refresh changes the real infrastructure to match the configuration while it refreshes state
Incorrect
refresh never modifies remote objects; it only updates state to match them.
Cterraform refresh ignores state locking and lets two runs write the state at the same time
Incorrect
refresh accepts the same locking options as apply and locks state like any other operation that writes it.
Dterraform refresh rewrites the configuration files to match remote objects, which loses edits
Incorrect
No refresh command edits configuration files; it works on state only.
The refresh command is deprecated. apply -refresh-only shows the detected changes and asks for confirmation before writing them to state.
After a manual change to a security group, an engineer runs terraform plan -refresh-only. Besides resource attributes in state, what else can this plan update when it is applied?
ARoot module output values
BThe remote objects, which it changes back to match the configuration
CThe configuration files, which it rewrites to match reality
DThe dependency lock file's provider selections
Show the answer and why
ARoot module output values
Correct
Refresh-only mode updates the state and any root module output values to match changes made outside Terraform.
BThe remote objects, which it changes back to match the configuration
Incorrect
Refresh-only mode never changes remote objects.
CThe configuration files, which it rewrites to match reality
Incorrect
Terraform does not edit configuration files in any planning mode.
DThe dependency lock file's provider selections
Incorrect
Provider selections change only during init.
A refresh-only plan lists the detected drift and any changes to outputs; applying it records both without touching infrastructure.
During an incident an engineer created an extra CloudWatch alarm by hand. The team wants Terraform to manage that alarm from now on without recreating it. A colleague proposes terraform apply -refresh-only. What should the team do instead?
ARun apply -refresh-only, which adds the new alarm to state
BWrite a resource block for the alarm and import it to that address
CRun plan -refresh=false, so that the next apply leaves the alarm alone
DAdd the alarm's ID to the depends_on list of a related resource
Show the answer and why
ARun apply -refresh-only, which adds the new alarm to state
Incorrect
Refresh-only mode updates objects already tracked in state; it does not add an object that has no address in the configuration.
BWrite a resource block for the alarm and import it to that address
Correct
An object created outside Terraform comes under management once the configuration declares it and an import binds the real object to that address.
CRun plan -refresh=false, so that the next apply leaves the alarm alone
Incorrect
Skipping refresh ignores external changes for that plan; it does not bring the alarm under management.
DAdd the alarm's ID to the depends_on list of a related resource
Incorrect
depends_on takes references to objects in the configuration and only orders operations.
Refresh-only mode reconciles state with changes to objects Terraform already tracks. A new object needs a resource block and an import, preferably an import block so the import shows up in a reviewed plan.
An operator changes a remote object outside Terraform, but the change does not invalidate the configuration: Terraform would only need to update state. How does HashiCorp classify this kind of drift?
AConfiguration drift, which drift detection reports as a failed check
BPolicy drift, which only a policy check can find
CProvider drift, fixed by pinning the provider version
DState drift, which drift detection does not report
Show the answer and why
AConfiguration drift, which drift detection reports as a failed check
Incorrect
Configuration drift is an external change that invalidates the configuration, which this change does not do.
BPolicy drift, which only a policy check can find
Incorrect
HashiCorp's drift terms are configuration drift and state drift.
CProvider drift, fixed by pinning the provider version
Incorrect
That is not one of HashiCorp's drift categories.
DState drift, which drift detection does not report
Correct
State drift is an external change that does not invalidate the configuration; refresh-only mode handles it, and drift detection does not detect it.
Drift detection in HCP Terraform looks for configuration drift. Reconcile state drift with a refresh-only plan and apply.