Find and fix drift

DevOps, Hands On15 minutesFree tierSteps checked 14 November 2026By Cloud Hands On

In this task you'll find what changed outside Terraform and put it right.

Drift is when Azure no longer matches the code, usually after a change in the portal. terraform plan shows it: lines with ~ (change) or - (remove) on things you didn't touch in code.

Decide which is right. If the hand change was wrong, apply. If it was right, change the code to match first.

Example. During an outage someone opened RDP on a production NSG. The nightly drift check (terraform plan -detailed-exitcode) flagged it the next morning and apply removed it.

Before you start

You need: A Terraform stack. Time: 15 minutes.

Where to look

  1. terraform plan
  2. terraform apply

Steps

  1. Run terraform plan and read every change you didn't expect.
  2. Decide: revert (apply) or adopt (edit the code).
  3. Apply, then plan again: it should say No changes.
  4. Schedule terraform plan -detailed-exitcode in a pipeline to catch drift early.

Commands

export ARM_SUBSCRIPTION_ID=$(az account show --query id -o tsv)
terraform plan -detailed-exitcode; echo "exit code $?"
terraform apply

Watch out for

Cost
Free.
Security
Drift on security rules (NSGs, firewalls, roles) is an incident, not housekeeping.
Common mistakes
Fixing drift by hand again, so the state still doesn't match.

Clean up

Nothing to clean up.

Words to know

Drift
Differences between code, state and reality.
-detailed-exitcode
Makes plan exit with 2 when there are changes, handy in pipelines.

Tick off each step and track your progress in the guide.

Open this task in DevOps, Hands On

Keep going