Skip to main content

Drift

Infrastructure drift is the gap between what Terraform's state and configuration expect and what actually exists in the cloud. It happens when resources are changed outside Terraform, such as in the console or CLI.

What Is Drift?

The state file is a snapshot from the last apply, so a console edit makes it stale. Terraform only notices drift when you run a plan, which refreshes state from the provider APIs.

Bash
terraform plan -refresh-only # show drift only
terraform plan -refresh-only -detailed-exitcode # CI check: exit 2 means drift

How to Respond

  1. Revert it with a normal terraform apply.
  2. Adopt it by updating the code, then apply.
  3. Record it in state only with terraform apply -refresh-only, and update the code too or the next apply reverts it.
Common Mistake

Treating drift as a one-off cleanup. If it keeps coming back, restrict console write access and backport every emergency fix to code.

Frequently Asked Questions

How does Terraform detect drift?

Only when you run plan or apply. It reads real resources through the provider APIs and compares them with state and configuration. There is no background watcher.

What is the difference between apply and apply -refresh-only for drift?

A normal apply changes infrastructure back to your code. A refresh-only apply updates only state to match reality and changes nothing in the cloud.

Found a mistake, an outdated step or a broken link?