CloudFormation Drift

Difficulty: Intermediate

Question

What is CloudFormation drift detection, how do you run it, and how do you fix drift once you find it?

Answer

CloudFormation drift detection compares the actual, live configuration of the resources in a stack against the configuration declared in its template, and reports anything that was changed or deleted outside CloudFormation. Drift usually comes from someone editing a resource in the console or CLI during an incident, and detecting it keeps your infrastructure as code trustworthy.

Why drift is dangerous

Infrastructure as code only works if the template is the source of truth. When someone opens port 22 on a security group by hand at 2 a.m. to debug production, three bad things happen:

  • The template no longer describes reality, so reviews and audits are wrong.
  • The next stack update may silently revert the manual fix, or fail because the resource is not in the state CloudFormation expects.
  • Security holes (open ports, public buckets, wider IAM policies) hide outside version control.

How drift detection works

  1. You start detection on a stack (or on individual resources, or a whole StackSet).
  2. CloudFormation reads the current configuration of each supported resource through the service APIs.
  3. It compares the live actual properties with the expected properties from the template and parameters.
  4. Each resource gets a status, and the stack gets an overall one.
LevelPossible statuses
StackIN_SYNC, DRIFTED, NOT_CHECKED, UNKNOWN
ResourceIN_SYNC, MODIFIED, DELETED, NOT_CHECKED

For a MODIFIED resource you get a property-level diff showing expected vs actual values, with differences labelled ADD, REMOVE or NOT_EQUAL.

Limitations to know

  • Detection is on demand, not continuous. You must trigger it, schedule it, or use the AWS Config managed rule cloudformation-stack-drift-detection-check to run it regularly.
  • Only resource types that support drift detection are checked; others show NOT_CHECKED.
  • It mostly checks properties you explicitly set in the template. A property left at its default and later changed may not be flagged.
  • Resources created by hand that were never in the stack are invisible to drift detection; that is a job for AWS Config or a resource inventory.

Fixing drift

There is no single "undo drift" button for every case, so pick based on whether the manual change was right:

  • The manual change was a mistake: run a stack update with the original template so CloudFormation reapplies the declared values. If CloudFormation cannot reconcile it, revert the change by hand, then re-run detection.
  • The manual change was correct: update the template to match reality, commit it, and deploy. The stack goes back to IN_SYNC.
  • A resource was deleted: recreate it through a stack update, or remove it from the template.
  • A resource was replaced by hand: remove it from the stack with DeletionPolicy: Retain, then bring the new one in with resource import.

Prevention is cheaper than cure: restrict console write access in production with IAM or SCPs, require changes through a CI/CD pipeline, and use stack policies to protect critical resources.

Drift detection and remediation loop. Detect, inspect the diff, then decide whether the template or the resource should change.
  1. Start drift detection: Trigger on a stack, a resource or a StackSet.
  2. Compare live vs template: CloudFormation reads actual properties via service APIs.
  3. Review resource statuses: IN_SYNC, MODIFIED, DELETED or NOT_CHECKED.
  4. Inspect property diffs: See expected vs actual values for each change.
  5. Fix template or resource: Codify the change or revert it with a stack update.
  6. Re-run detection: Confirm the stack is back to IN_SYNC.

Code examples

Detect and inspect drift with the AWS CLI

# 1. Start drift detection (asynchronous)
DRIFT_ID=$(aws cloudformation detect-stack-drift \
  --stack-name payments-prod \
  --query StackDriftDetectionId --output text)

# 2. Poll until DetectionStatus is DETECTION_COMPLETE
aws cloudformation describe-stack-drift-detection-status \
  --stack-drift-detection-id "$DRIFT_ID"

# 3. Show only resources that drifted
aws cloudformation describe-stack-resource-drifts \
  --stack-name payments-prod \
  --stack-resource-drift-status-filters MODIFIED DELETED \
  --query "StackResourceDrifts[].{Id:LogicalResourceId,Status:StackResourceDriftStatus,Diff:PropertyDifferences}"

Detection is asynchronous, so you start it, poll the status, then fetch per-resource differences. Here someone added an SSH rule open to the world outside CloudFormation.

Key points

  • Drift means the live resource no longer matches the template, usually because of manual console or CLI edits
  • Detection is on demand; schedule it or use the AWS Config rule for regular checks
  • Resource statuses are IN_SYNC, MODIFIED, DELETED and NOT_CHECKED; stack status is DRIFTED or IN_SYNC
  • Fix drift by either updating the template to match reality or redeploying to restore the declared state
  • Prevent drift by limiting console write access and pushing all changes through a pipeline

Common follow-up questions

  • Why might drift detection miss a change to a property that has a default value?
  • How would you run drift detection automatically across 50 accounts?
  • What is CloudFormation resource import and when would you use it to resolve drift?
  • How does Terraform handle drift compared to CloudFormation?

Interview tips

  • Explain why drift matters (next update can revert or fail, hidden security holes), not just what it is
  • Mention the limitations: on demand, supported resource types only, explicitly set properties
  • Give a concrete story such as a manually opened security group port during an incident

Concepts covered

Drift Detection, Infrastructure as Code, Stack Resources, Configuration Drift, Remediation

More AWS & Cloud Fundamentals questions