Drift detection

When reality diverges from the template.

Intermediate10 min · lesson 8 of 12

Drift is when the real configuration of a stack’s resources no longer matches the template — almost always because someone changed something by hand in the console or CLI to fix an incident, and never put it back in code. Drift is dangerous precisely because it is invisible until the next stack update, which may revert the manual fix or fail unexpectedly. Drift detection makes it visible.

terminal
$ aws cloudformation detect-stack-drift --stack-name payments-network
$ aws cloudformation describe-stack-resource-drifts \
--stack-name payments-network --output table
| LogicalResourceId | DriftStatus |
| AppSecurityGroup | MODIFIED | <- a rule was added by hand in the console

Fixing drift

Once detected, you reconcile: either update the template to match the intended real state (adopting the manual change into code), or update the stack to push the template’s state back onto the resource (discarding the manual change). The right choice depends on whether the drift was a good fix that belongs in code or an unauthorized change that should be reverted — but either way the resolution is to make code and reality agree again.

The drift cycle
1manual change
console/CLI edit
2drift
reality ≠ template
3detect
find what diverged
4reconcile
code and reality agree
The cure is prevention: change infrastructure through the stack, and run drift detection on a schedule to catch what slips.
Drift is a process problem, not just a tool
Drift detection tells you what diverged, but the real fix is closing the door: restrict console write access in production so changes go through CloudFormation, run detect-stack-drift on a schedule (via EventBridge/Config), and alert on it. A stack nobody edits by hand does not drift; detection is the backstop for the times discipline slips.