Drift detection and refresh-only plans

Intermediate · 4 min

Someone changed a resource in the console. How plan reports it, what -refresh-only does with state, and how to catch drift on a schedule with exit codes.

Before thisInside the state fileImporting existing infrastructure

Talking tech with Techielass · 4 min

Drift detection and refresh-only plans

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Talking tech with TechielassReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Drift is a difference between state and the real object that Terraform did not make. Every normal plan refreshes state first and reports drift as 'Objects have changed outside of Terraform'.
  • A normal plan then goes further and proposes to undo the drift, because your code is the desired state. `-refresh-only` instead records the real values in state and proposes no change to infrastructure.
  • `terraform refresh` is deprecated. The docs call it an alias for `apply -refresh-only -auto-approve`, which writes state with no review. Use `plan -refresh-only` or `apply -refresh-only` instead.
  • Accept drift by updating state (and your code to match) or reject it by running a normal apply. Neither is right every time, and a refresh-only plan lets you choose per change.
  • `plan -detailed-exitcode` returns 0 for no changes, 1 for an error and 2 for changes. A scheduled job can use it to raise an alert when reality and code part ways.

Friday’s console fix

During an incident, someone opens the cloud console and adds a tag, loosens a security group or resizes an instance. It works, the incident ends, and nobody writes it down. Six weeks later an unrelated pull request shows a plan that changes a security group. Terraform is not misbehaving. It found something that does not match your code and is proposing to fix it. That mismatch is called drift, and the way Terraform handles it explains most surprises of this kind.

Where drift shows up

Before every normal plan or apply, Terraform refreshes: it asks each provider for the current attributes of every object in state. Then it compares your configuration against those refreshed values. When the refresh found changes you did not make, the plan prints them first, in a section the Terraform tutorial shows as “Objects have changed outside of Terraform”, and then proposes the actions needed to bring everything back in line with the code.

To see the distinction, I used my toy planner again. It is a Python stand-in, not Terraform, and its wording is mine. The scenario: the code declares a bucket with no tags, and someone added team=ops in the console.

Drift: someone added tag team=ops in the console. Config says no tag.
plan:
  ~ update  aws_s3_bucket.uploads: {'tags': {'team': 'ops'}} -> {'tags': {}}
plan -refresh-only:
  # aws_s3_bucket.uploads changed outside Terraform: {'tags': {}} -> {'tags': {'team': 'ops'}} (state would be updated, no remote change)

The normal plan treats your code as the truth and proposes removing the tag. The refresh-only plan treats the real object as the truth and proposes to update state. Which one is correct depends on who was right.

Refresh-only mode

terraform plan -refresh-only shows what state would absorb, and terraform apply -refresh-only writes it, after you confirm. The plan docs describe the mode for situations like an incident, where changes were made outside the usual workflow and you need to reconcile Terraform’s records. No real object is changed. Only state is.

That is only half the job. If the tag change is one you want to keep, state now has it, but the code does not. The next normal plan will still propose to remove it, so you add the tag to the resource block too. If the change was a mistake, skip the refresh-only apply and run a normal apply, which puts things back.

The older command is terraform refresh. Its docs say it is deprecated and effectively an alias for terraform apply -refresh-only -auto-approve. The auto-approve is the problem. It writes state with no plan to read, and the docs warn that if your credentials point at the wrong account, it could record that all your resources have vanished. Use the flag form so you see the diff first. Terraform 1.10 also made refresh-only plans applyable when only outputs changed.

Two related options exist. -refresh=false skips the refresh, which is faster and also blind to drift, and the docs say it can give an incomplete or incorrect plan. -minimal-refresh, new in Terraform 1.17, refreshes only the resources the plan proposes to change, with the same warning.

Drift is not always a bug

Some changes outside Terraform are normal. An autoscaler changes desired_capacity. A cloud service adds a default tag. Another tool writes a field your resource also declares. A refresh-only plan will report all of these. For a field you deliberately let something else own, the answer is ignore_changes in a lifecycle block, covered in the lifecycle rules lesson. That tells Terraform not to fight about it.

Catching drift on a schedule

Drift found on a Friday deploy is a bad time to learn about it. The cheap fix is a scheduled pipeline job that runs a plan and fails on changes:

terraform plan -detailed-exitcode -input=false

The docs define the exit codes: 0 means success with an empty diff, 1 is an error, and 2 is success with a non-empty diff. A job that treats 2 as “send an alert” is a working drift detector. Run it against production daily, and have its output go somewhere a person reads. The command is from the plan docs and I did not run it, as there is no Terraform on this machine.

Managed platforms such as HCP Terraform and tools such as Spacelift sell scheduled drift detection with reconciliation, and the CI lessons cover wiring up the plain version. A plain job does most of the job for no cost.

The better fix

Alerts don’t stop console edits. The durable answer is to make the console read-only for people in production, so the incident fix goes through a pull request that takes minutes, and to give the emergency path a name, like a break-glass role with an audit log. Drift then becomes an exception that gets noticed, instead of the default.

About the video

The embed is Techielass’s “Terraform -refresh-only tutorial | Fixing State drift the right way”, published May 2025, 4 minutes. Its description says it covers what state is, how drift happens and the -refresh-only flag. It is short and, as far as I can tell from the description, Azure flavoured. It does not mention scheduled detection or exit codes, which this lesson adds. A longer Spacelift video on drift exists, but it is mostly a pitch for their product, so I left it out. English was judged from the title, description and the English auto-caption track.

Where this fits

Import, move and refresh keep state matching reality. The next group is about what Terraform does when it has to change or replace real things, and how to stop it hurting you: lifecycle rules.

Check yourself

  1. 01A teammate added a tag to a bucket in the console. What does a normal `terraform plan` do with it?

    It refreshes state, notes the change outside Terraform, and then plans to remove the tag, since your configuration does not declare it.

  2. 02What is the difference between `plan` and `plan -refresh-only`?

    `plan` compares code to the refreshed state and proposes changes to infrastructure. `plan -refresh-only` only proposes updating state to match reality. It touches no real objects.

  3. 03Why is `terraform refresh` discouraged?

    It updates and saves state straight away with no plan to review. If credentials or a region are wrong, it can record that resources are gone and leave state wrong.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark