Sub topic 15
Detecting Drift and Sharing Values Between Stacks
This chapter assumes you can write a template with Parameters, Resources and Outputs, and that you can deploy a stack from the AWS CLI. It covers two topics. The first is how to find out when real resources no longer match the template. The second is how one stack can use values from another.
Detecting drift from the template
Drift is any difference between a stack's template and the live resources. It usually happens when someone edits a resource in the console or with the CLI instead of changing the template. CloudFormation does not watch for this. It only compares when you ask it to.
Drift detection compares the properties you declared in the template with the properties the resource has now. Each resource gets one of these statuses:
| Status | Meaning |
|---|---|
| IN_SYNC | The resource matches the template. |
| MODIFIED | One or more declared properties changed. |
| DELETED | The resource no longer exists. |
| NOT_CHECKED | The resource type does not support drift detection. |
The stack gets an overall status of IN_SYNC or DRIFTED. Detection is asynchronous, so you start it, wait for it to finish, and then read the results.
# Start detection and keep the returned ID
DETECTION_ID=$(aws cloudformation detect-stack-drift \
--stack-name demo \
--query StackDriftDetectionId \
--output text)
# Check whether detection has finished
aws cloudformation describe-stack-drift-detection-status \
--stack-detection-id "$DETECTION_ID"
# List only the resources that drifted, with expected and actual values
aws cloudformation describe-stack-resource-drifts \
--stack-name demo \
--stack-resource-drift-status-filters MODIFIED DELETEDFor a MODIFIED resource, the output lists each property with its expected value, its actual value, and the kind of difference.
Drift detection only sees properties that you set in the template. If you never declared a property, a change to its default value is not reported. Detection also only reports. It does not fix anything. To resolve drift, either change the resource back to match the template, or update the template to match the resource and then deploy. Be careful with the second option, because an update can overwrite or replace a resource that was changed by hand.
Sharing values between stacks
A stack often needs a value that another stack created, such as a bucket ARN or a VPC ID. A cross-stack reference does this with an export and an import.
The exporting stack adds an Export block with a name to an output. In the earlier template, the output BucketArn is exported as ${AWS::StackName}-BucketArn. For the stack named demo, the export name is demo-BucketArn. The importing stack reads it with Fn::ImportValue.
AWSTemplateFormatVersion: "2010-09-09"
Description: Consumer stack that imports the bucket ARN exported by the stack named demo
Resources:
ReaderPolicy:
Type: AWS::IAM::ManagedPolicy
Properties:
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action: s3:GetObject
Resource: !Join
- ""
- - !ImportValue demo-BucketArn
- "/*"Keep these rules in mind:
- Export names must be unique within an account and Region. Imports only work in the same Region.
- You cannot delete a stack while another stack imports one of its exports.
- You cannot change the value of an export that is in use. An update that would change it fails.
- Imports are resolved by name, so the importing stack has a hard dependency on the exporting stack.
To find out who uses an export, run aws cloudformation list-imports --export-name demo-BucketArn. To list every export in the Region, run aws cloudformation list-exports.
Exports suit stable, rarely changed values such as network IDs. If a value changes often, pass it as a parameter instead.
Exercise
A stack named net exports net-VpcId. A second stack imports it. You try to delete net and the operation fails. What are two ways to resolve this, and which command shows you the blocking stack?
Show answer
Run aws cloudformation list-imports --export-name net-VpcId to see which stack imports the export. Then either delete the importing stack, or update it to stop using Fn::ImportValue (for example by passing the VPC ID as a parameter). After that, delete net.
Exercise
Detection reports a bucket as IN_SYNC, yet someone changed a bucket setting in the console. Give one likely reason.
Show answer
The changed setting is not declared in the template, so drift detection does not compare it. It only checks properties that you specified.