Sub topic 16
Detecting drift from the template
This chapter assumes you can create and update stacks from the CLI and read the demo stack template from earlier chapters. You also need permission to call cloudformation:DetectStackDrift and to read the resources in the stack.
What drift is
A stack records what the template declared. The real resources can change afterward through the console, the CLI, or another tool. This difference is drift. CloudFormation does not watch for it. It compares the live configuration to the expected one only when you ask it to.
Drift matters for three reasons:
- Later updates may behave unexpectedly, because CloudFormation reasons from what it believes the resource looks like.
- A deleted resource can make an update or delete fail.
- The template stops being a trustworthy description of what is running.
Drift statuses
Detection gives a status for the stack and for each resource.
| Level | Status | Meaning |
|---|---|---|
| Stack | `IN_SYNC` | Every checked resource matches the template. |
| Stack | `DRIFTED` | At least one resource differs. |
| Stack | `NOT_CHECKED` | Detection has not run. |
| Resource | `IN_SYNC` | Properties match. |
| Resource | `MODIFIED` | One or more properties differ. |
| Resource | `DELETED` | The resource no longer exists. |
| Resource | `NOT_CHECKED` | The resource type does not support drift detection. |
For MODIFIED resources, each property difference has a type: ADD (a value exists that the template does not specify), REMOVE (the template specifies a value that is missing), or NOT_EQUAL (the values differ).
Running detection
Detection is asynchronous. You start it, poll for completion, then read the results.
# Start detection and keep the detection ID
DETECTION_ID=$(aws cloudformation detect-stack-drift \
--stack-name demo \
--query StackDriftDetectionId \
--output text)
# Poll until DetectionStatus is DETECTION_COMPLETE
aws cloudformation describe-stack-drift-detection-status \
--stack-drift-detection-id "$DETECTION_ID"
# List only the resources that differ
aws cloudformation describe-stack-resource-drifts \
--stack-name demo \
--stack-resource-drift-status-filters MODIFIED DELETEDThe status call returns DetectionStatus (DETECTION_IN_PROGRESS, DETECTION_COMPLETE, or DETECTION_FAILED) and, once complete, StackDriftStatus. A failed detection usually points to missing read permissions or a stack in a state that cannot be checked, such as CREATE_IN_PROGRESS.
To check one resource, call detect-stack-resource-drift with --logical-resource-id. It returns the result directly instead of a detection ID.
A worked example
Take the demo stack from the template chapter with EnvName=dev. The template sets bucket versioning to Suspended. Now change the bucket outside CloudFormation:
BUCKET=$(aws cloudformation describe-stack-resource \
--stack-name demo \
--logical-resource-id AppBucket \
--query StackResourceDetail.PhysicalResourceId \
--output text)
aws s3api put-bucket-versioning \
--bucket "$BUCKET" \
--versioning-configuration Status=EnabledRun detection again. AppBucket should show MODIFIED, with a property difference at /VersioningConfiguration/Status. The expected value is Suspended, the actual value is Enabled, and the type is NOT_EQUAL.
What detection does and does not check
- Only resource types that support drift are checked. Others report
NOT_CHECKED, so a clean stack result does not cover them. Check the supported-types list in the CloudFormation documentation. - Detection compares the properties your template sets. A property you left out is generally not compared, even if someone changes it. Some resource types also compare default values.
- Changes to values CloudFormation does not model, such as data inside a bucket, are invisible to it.
- For nested stacks, detection started on the parent stack includes the child stacks.
Fixing drift
Detection only reports. You decide what is correct, then reconcile in one of these ways:
- Revert the resource. Undo the manual change so it matches the template again.
- Accept the change. Edit the template to match reality, then update the stack.
- Re-import. If a resource was deleted and recreated outside CloudFormation, import the new one into the stack. A later chapter covers import.
There is a trap in the update route. CloudFormation updates only properties whose template values changed. If you re-deploy an unchanged template, a drifted property is not touched, and the stack still reports drift. To force a property back, change its value in the template (for example to a different value, deploy, then change it back), or fix the resource directly.
Practical habits
- Run detection before a risky update and after any incident where people had console access.
- Schedule detection for important stacks, for example from an EventBridge rule that invokes a function calling
DetectStackDrift. - Restrict write access to managed resources, so drift is rare.
- Treat
DELETEDas urgent. A later stack update or delete that touches the missing resource may fail.
Exercise
You run drift detection on a stack and the stack status is IN_SYNC. A colleague says no one could have changed the DNS records the stack manages, because the result is clean. What two reasons could make this conclusion wrong?
Show answer
First, the DNS record resource type may not support drift detection, in which case its status is NOTCHECKED and it is not compared at all. Second, detection compares only properties your template specifies (plus some defaults), so a change to an unspecified property would not appear. Either way, an INSYNC stack means only that the checked properties match.
Exercise
A bucket property has drifted. You re-deploy the same unchanged template and the stack still reports drift. Why, and how can you fix it?
Show answer
CloudFormation updates only properties whose values changed in the template, so an unchanged template leaves the drifted property alone. Either revert the property manually, or change the value in the template, deploy, and (if needed) change it back and deploy again. If the live value is the one you want, update the template to match it instead.