MORTIMERDEV LABS / AWS / CDK DIFF FREE · BETA
What will this deploy change?
Before it changes it.
Paste the output of cdk diff. The riskiest changes come
first: resources that will be replaced or deleted (and the data that
goes with them), orphaned resources, new permissions and open ports.
Each one says what to check before you deploy.
Explain a diff
RESULT
Check these first
Every resource change
Notes
What is cdk diff?
cdk diff compares your CDK app with what’s deployed and
prints what cdk deploy would change. Each resource line
starts with a symbol and can end with what happens to it:
- Change
- [+] added, [-] removed, [~] changed, [←] imported
- Type
- The CloudFormation resource type
- Path
- Where it is in your CDK code
- Logical ID
- How CloudFormation knows it, made from the path
- Impact
- replace, may be replaced, destroy, orphan or import; blank for a new resource or an in-place update
Before you deploy, check
- Replacements: a new resource is created with a new physical ID. A new bucket, table or database starts empty.
- Deletions and orphans:
destroydeletes it;orphankeeps it in your account, unmanaged. - Renames: a resource removed and another of the same type added usually means a construct moved or was renamed.
- Permissions and ports: wildcards, public principals and rules open to “Everyone”.
Avoiding an unwanted replacement
-
Don’t rename or move constructs that hold data, or keep the old ID
with
overrideLogicalId()on itsnode.defaultChild.cdk refactor(in preview) can move them without replacing them. -
Set
removalPolicy: RemovalPolicy.RETAINon anything you can’t lose. It sets both DeletionPolicy and UpdateReplacePolicy. -
Avoid fixed names (
bucketName,tableName) unless you need them: a resource with a fixed name can’t be replaced unless the name changes too.
In CI
cdk diff doesn’t fail by default. These flags make it fail
(exit code 1) so a pipeline can stop:
cdk diff --fail-on=destructive cdk diff --fail-on=broadening cdk diff --fail
destructive fails on anything replaced, deleted or
orphaned; broadening on wider security permissions;
--fail (the same as any-change) on any
difference. --security-only shows only the security tables,
and only when the change broadens permissions.
Read more: AWS: cdk diffAWS: update behaviours of stack resourcesAWS: UpdateReplacePolicyAWS: preserve resources when refactoringGitHub: aws-cdk CLI README
TEST YOURSELF
How well can you read a cdk diff?
Ten questions on symbols, replacements, removal policies, permissions and CI flags, with an explanation and a link to the reference after each one. Three use real lines from cdk diff output.
CDK DIFF REFERENCE
cdk diff, in plain English.
Symbols and annotations
[+]Added: a new resource, property, parameter or output.
A new resource is created on deploy. Under a changed resource, [+] marks a property being set or a new value.
[-]Removed: a resource, property or value goes away.
On a resource line, [-] means it leaves the stack. Whether it's deleted or kept depends on its removal policy, shown at the end of the line as destroy or orphan.
[~]Changed: the resource or property is modified.
The changed properties are listed underneath, each with its old value ([-]) and new value ([+]). Long values such as JSON arrays are shown as a diff with @@ markers, and unchanged lines marked [ ].
[←]Imported: an existing resource comes under the stack's control.
Shown when a resource would be imported instead of created, for example with cdk diff --import-existing-resources when it already exists in your account. The resource line ends with the word import.
(requires replacement)This property can't be changed in place, so the resource is replaced.
Each property's update behaviour is in the CloudFormation reference for its resource type. One property that requires replacement is enough to replace the whole resource.
(may cause replacement)This property replaces the resource in some cases only.
The update behaviour is conditional: the CloudFormation reference lists the conditions for each property. A change set is more accurate than comparing templates, but can still report it as conditional.
What happens to the resource
replaceThe resource is recreated: a new one with a new physical ID, then the old one is removed.
CloudFormation usually creates the replacement first, points dependent resources at it, then deletes the old one. With UpdateReplacePolicy Retain the old one is kept; with Snapshot it's snapshotted first (supported types only) and then deleted. A new bucket, table or database starts empty. Resources with a fixed (custom) name can't be replaced unless the name changes too.
may be replacedOne or more properties may cause replacement; it depends on the values.
A template-only diff (--method=template, or the fallback when a change set can't be created) can't decide and says may be replaced. The default --method=auto uses a change set, which is more accurate but can still report a replacement as conditional.
destroyThe resource is deleted from your account.
It's leaving the stack and won't be retained: its DeletionPolicy is Delete, Snapshot (a snapshot is taken first, for supported types) or not set. For anything holding data and without a snapshot, that data is gone.
orphanThe resource leaves the stack but is kept in your account.
Its DeletionPolicy is Retain (or RetainExceptOnCreate): CDK's RETAIN, the default for stateful resources like S3 buckets and DynamoDB tables. It keeps existing (and costing money) but is no longer managed. Creating a resource with the same fixed name later fails until you delete it or import it.
(OR move … via refactoring)The change could instead be a move, if you use cdk refactor.
Shown with --include-moves when a resource looks moved or renamed. cdk refactor (in preview, run with --unstable=refactor) updates the logical ID without replacing the resource.
Logical IDsRenaming or moving a construct changes its logical ID, so CloudFormation sees a new resource.
CDK derives each logical ID from the construct's path. A changed path shows up as one resource removed and another of the same type added: a replacement in all but name. Keep the ID with overrideLogicalId() (on node.defaultChild for an L2 construct) or use cdk refactor.
Security changes
IAM Statement ChangesA table of IAM policy statements being added (+) or removed (-): resource, effect, action, principal and condition.
References appear as ${Path.Attribute}, such as ${Reports.Arn}, because the real ARN is only known at deploy time. Look for wildcards: s3:* or * actions, resource *, and principal * (anyone).
IAM Policy ChangesManaged policies being attached to or removed from roles, users and groups.
AWS managed policies like AdministratorAccess grant far more than most code needs.
Security Group ChangesSecurity group rules being added or removed: group, direction (In or Out), protocol and port, and peer.
“Everyone (IPv4)” is 0.0.0.0/0 and “Everyone (IPv6)” is ::/0: anyone on the internet.
(NOTE: There may be security-related changes not in this list…)The tables don't cover every kind of security change.
cdk diff prints this whenever it shows security tables. Check the resource changes as well.
Running cdk diff
cdk synth / cdk diff / cdk deploysynth writes CloudFormation and assets to cdk.out; diff compares that with what's deployed; deploy changes your account.
cdk synth deploys nothing (it only talks to AWS for context lookups not yet cached in cdk.context.json). cdk diff synthesises first. cdk deploy synthesises, publishes assets, then creates and runs a change set.
--methodauto (default) tries a change set for accurate replacement info; change-set requires one; template compares templates only.
A change set needs AWS credentials and uses the deploy role. Template-only diffs are faster but over-report replacements and can only say may be replaced for conditional properties.
--fail-onExit with code 1 in CI: any-change (same as --fail), broadening (security), destructive (replace, delete or orphan) or never.
By default cdk diff doesn't fail. --fail-on=destructive lists the resources that would be replaced, deleted or orphaned, which catches data loss before it ships.
--security-onlyOnly show the security tables, and only when the change broadens permissions.
If nothing broadens security, it prints nothing. When it does print, the IAM and security group tables include any narrowing rows too. Useful for a quick security review of a large diff.
Stack Name (aws://account/region)Each stack's section starts with its name and the environment it deploys to.
“There were no differences” means that stack is unchanged. After the stacks, a line counts the stacks with differences (with --fail-on=destructive, a list of destructive changes follows it).
--strictShow the CDK metadata resource and other changes cdk diff hides by default.
Without it, AWS::CDK::Metadata and the CheckBootstrapVersion rule are filtered out, along with likely-mangled non-ASCII characters.
It reads the diff, not your account.
The explanation is only as good as the diff. A template-only diff
(--method=template) over-reports replacements; the
default method uses a change set, which is more accurate but can still
call a replacement conditional. Always read the full output too.
Nothing leaves this page.
Diffs are explained in your browser and nothing is saved. We count anonymous page views and button use, never what you enter.
Beta feedback helps shape Labs. Tell Andy what would help
Learn Step 4 of 4 in AWS foundations for web developers
Rules last checked , against cdk diff command reference, aws-cdk CLI README (cdk diff), @aws-cdk/cloudformation-diff, CloudFormation update behaviours of stack resources, UpdateReplacePolicy attribute, DeletionPolicy attribute, CDK RemovalPolicy and Preserve deployed resources when refactoring CDK code. Re-checked weekly.
