Terraform Field Guide · Chapter 9 of 12
Drift and reviewing plans
Terraform only knows what it last saw. When someone changes production in the console, the code and reality quietly disagree, and the next unrelated apply settles the argument without asking. Learn where drift comes from, how to find it on a schedule, how to decide what to do with it, and how to read any plan like a reviewer.
- 45 min
- Advanced
- Hands-on
- Free
By Joshua Sequeira, founding engineer running Terraform in production. Updated .
By the end you can
0 of 5
Live lab in this chapter
A real-feeling terminal and a live cluster map, right in your browser. Every command block below has a ▶ Run in lab button.
What drift is
Drift is a difference between what Terraform recorded and what actually exists. It does not show up until someone runs a plan, and then it shows up in whatever plan happens to run next, mixed in with someone else's change.
It comes from a handful of places:
- Console hotfixes. A port opened at 2am during an incident and never written down.
- Other tools. A script, a second Terraform project, or a person running the AWS CLI against the same resources.
- The platform itself. An autoscaler changing a desired count, AWS applying a default, a managed service upgrading a minor version.
- Provider upgrades. A new provider version reading an attribute differently, so a plan shows a change nobody made.
Detecting it
Every normal plan refreshes first, so drift appears in a section of its own, above your changes:
terraform plan
Note: Objects have changed outside of Terraform
Terraform detected the following changes made outside of Terraform since the
last "terraform apply" which may have affected this plan:
# aws_security_group.web has changed
~ resource "aws_security_group" "web" {
~ ingress = [
+ { from_port = 22, to_port = 22, cidr_blocks = ["0.0.0.0/0"] },
]
}Objects have changed outside of Terraform- This block is drift: what happened, not what will happen. Your changes follow below it.
Two flags make drift easier to work with:
| Command | Use it to |
|---|---|
terraform plan -refresh-only | See only drift, with none of your code changes mixed in. Applying it updates state to match reality, without touching any resource. |
terraform plan -detailed-exitcode | Script it. Exit code 0 means no changes, 1 means an error, 2 means there are changes. |
A daily drift check
Drift you find on a schedule is a small task. Drift you find in the middle of someone else's urgent change is an incident. A scheduled job that plans every environment and fails on exit code 2 turns the first into the default:
name: drift-check
on:
schedule:
- cron: "0 6 * * 1-5" # weekdays, 06:00 UTC
workflow_dispatch: {}
permissions:
id-token: write
contents: read
jobs:
drift:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
env: [dev, staging, prod]
environment: ${{ matrix.env }}
defaults:
run:
working-directory: envs/${{ matrix.env }}
steps:
- uses: actions/checkout@v4
- uses: hashicorp/setup-terraform@v3
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ vars.PLAN_ROLE_ARN }}
aws-region: eu-west-1
- run: terraform init -input=false
- run: terraform plan -input=false -lock=false -detailed-exitcode- Every weekday morning, so someone sees it with their coffee, not at 2am.
- One drifted environment should not stop the others from being checked.
- A read-only role, per environment. Checking for drift should never need permission to change anything.
- A read-only check does not need the lock, and will not block a real apply that is running.
- Exit 2 fails the job, and a failed scheduled run notifies you. Wire it to Slack if you prefer.
Revert, adopt or ignore
Finding drift is the easy half. Every drifted attribute needs a decision, and there are only three:
| Decision | When | How |
|---|---|---|
| Revert | The change was a mistake, or is no longer needed | Apply. Terraform puts it back to what the code says. |
| Adopt | The change was right and should stay | Write it into the code until plan shows no changes. Now it is reviewed, and in Git. |
| Ignore | Something else owns the attribute, like an autoscaler | lifecycle { ignore_changes = [desired_count] } on that attribute only. |
Adopting usually means narrowing the change too. The 2am rule opened SSH to the whole internet; the version that goes into code allows only the VPN range, and says why:
resource "aws_security_group" "web" {
vpc_id = aws_vpc.main.id
ingress {
description = "HTTPS from the load balancer"
from_port = 443
to_port = 443
protocol = "tcp"
security_groups = [aws_security_group.alb.id]
}
ingress {
description = "SSH from the office VPN only (incident 2026-11-12)"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["203.0.113.0/24"]
}
}- The reason, in the code. The next person to see it can decide whether it is still needed.
- Narrower than what was added in the console. Adopting is a chance to fix it properly.
Reviewing plans
Every plan, drift or not, deserves the same four questions:
- Does the summary match the intent? "Add one bucket" should say
1 to add. Compare the numbers before reading a single line. - Is anything replaced or destroyed? Search for
-/+,destroyandforces replacement. For anything with data in it, stop. - Is there anything the pull request did not mention? Drift, a provider upgrade, a module bump: each one needs its own decision.
- What does it grant? IAM policies, security groups and bucket policies deserve a line-by-line read, however small the diff.
Try it. Six pull requests, each with its plan. Approve or stop:
Interactive · Plan review
Six pull requests. Would you approve the apply?
PR
Hands-on: cause drift, then find it
Use the chapter 2 folder (random_pet and local_file), applied. Now change the world behind Terraform's back:
rm inventory.txt
terraform plan -refresh-only
Note: Objects have changed outside of Terraform
Terraform detected the following changes made outside of Terraform since the
last "terraform apply" which may have affected this plan:
# local_file.inventory has been deleted
- resource "local_file" "inventory" {
- content = <<-EOT
web server: proud-gecko
EOT -> null
...
- filename = "./inventory.txt" -> null
}
This is a refresh-only plan, so Terraform will not take any actions to undo
these. If you were expecting these changes then you can apply this plan to
record the updated values in the Terraform state without changing any remote
objects.has been deleted- Drift, reported on its own.
refresh-only plan- Applying this would make Terraform forget the file, not recreate it.
terraform plan -detailed-exitcode > /dev/null; echo "exit code: $?"
terraform apply -auto-approve
terraform plan -detailed-exitcode > /dev/null; echo "exit code: $?"
exit code: 2
...
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
exit code: 0exit code: 2- Changes present: the file is missing and the plan would recreate it. This is what a scheduled job alerts on.
exit code: 0- After the apply (a revert), code and reality agree again.
Mini mission
Write your team's drift runbook
Write five lines someone could follow when the drift check fails at 06:00: who looks at it, how they find out who made the change, and how they choose between revert, adopt and ignore. Then run the scheduled workflow above against your own chapter 7 layout, or just run its last command by hand in each environment folder.
Reveal one safe solution
- Whoever is on call owns the failed drift check that day.
- Run
terraform plan -refresh-onlyin that environment and note each drifted attribute. - Find who changed it: AWS CloudTrail shows the API call, the user or role, and the time.
- Ask them. Still needed: adopt it in a pull request. Not needed: revert with an apply. Owned by another system:
ignore_changeson that attribute only. - If it was a console fix during an incident, add "write it into Terraform" to that incident's follow-ups, so it does not happen silently next time.
CloudTrail is the step most people forget. Drift is rarely malicious; it is usually someone fixing something in a hurry, and they will know whether it should stay.
Check yourself
Quiz
0 of 5 answered