Skip to content

fix(toolkit-lib): show termination protection changes in diff - #2174

Open
azatoth wants to merge 3 commits into
aws:mainfrom
azatoth:fix/diff-termination-protection
Open

azatoth wants to merge 3 commits into
aws:mainfrom
azatoth:fix/diff-termination-protection

Conversation

@azatoth

@azatoth azatoth commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes aws/aws-cdk#32043 (filed in aws/aws-cdk before the CLI moved here). The deploy side is in #2173.

cdk diff only compares templates, so a change to a stack's termination protection isn't shown, and the stack is reported as having no differences.

When diffing against a deployed stack, this PR reads the deployed termination protection setting during the same DescribeStacks lookup that already fetches the template, so it adds no API calls. If the setting differs from the app's, the diff shows it as a stack property change:

Stack MyStack
Stack Properties
[~] TerminationProtection: disabled to enabled

Details:

  • The stack counts as changed in "Number of stacks with differences", and the stack header is printed in --quiet mode.
  • It only applies to the root stack, because nested stacks inherit termination protection from the root.
  • Nothing is compared when diffing against --template, when the stack doesn't exist, or when it is in REVIEW_IN_PROGRESS. The deploy code treats that last state as a create, too.
  • Both the CLI's cdk diff (CdkToolkit.diff) and Toolkit.diff() in toolkit-lib pass the value through.
  • The StackDiff payload of CDK_TOOLKIT_I4002 gets an optional terminationProtectionChange field, an IDifference<boolean> from @aws-cdk/cloudformation-diff, so programmatic callers can see the change. No new types are added to the public API. The DiffResult payload (I4001) and the return value of Toolkit.diff() are unchanged, because both map stack names to template diffs and termination protection isn't part of the template.

Behaviour change to review

Because the stack now counts as changed, cdk diff --fail exits non-zero when termination protection is the only difference. That seems right to me (a deploy would change the stack), but it is a visible change in behaviour.

This PR doesn't add the comparison to the approval diff that deploy shows. That diff is only shown when the change set is non-empty, and termination protection is updated before the prompt anyway (see #2173).

Checklist

  • Unit tests added
  • No manual edits to generated files

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

🤖 Generated with Claude Code

@azatoth
azatoth requested a review from a team November 22, 2026 11:12
@github-actions github-actions Bot added the p2 label Nov 22, 2026
auto-merge was automatically disabled November 24, 2026 07:42

Head branch was pushed to by a user without write access

@azatoth
azatoth force-pushed the fix/diff-termination-protection branch from 18e21c6 to 6e500dc Compare November 24, 2026 07:42
@azatoth
azatoth force-pushed the fix/diff-termination-protection branch from 6e500dc to b9bc277 Compare November 24, 2026 07:53
@azatoth
azatoth force-pushed the fix/diff-termination-protection branch from b9bc277 to 40e4f11 Compare November 24, 2026 07:57
@azatoth
azatoth force-pushed the fix/diff-termination-protection branch from 40e4f11 to e4e72c0 Compare November 26, 2026 07:46
@maintainer-for-aws

maintainer-for-aws Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Automated review

A maintainer will still review this — treat the notes below as a starting point.

📌 Commit b3ca0ad

This PR makes cdk diff report when a stack's termination protection setting will change. Termination protection is a stack setting rather than part of the template, so until now changing it showed "There were no differences". The diff now prints a Stack Properties block (for example, [~] TerminationProtection: disabled to enabled) and counts the stack in "Number of stacks with differences". Toolkit-lib's StackDiff (I4002) payload gains an optional terminationProtectionChange field.

No issues found on this change. How it works:

  • Reading the deployed value: it comes from the same CloudFormationStack.lookup that already fetches the template, so no CloudFormation calls are added.
  • When it compares: only for the root stack, and only when the stack exists and isn't in REVIEW_IN_PROGRESS. Nested stacks take the setting from their root.
  • Unset values: a value left unset in the app counts as disabled, which matches how deploy already compares the two.
  • Deploy is unaffected: the deploy approval paths ignore the new field.
  • API surface: no new public types are exported.
  • cdk diff --fail: it now exits non-zero when termination protection is the only difference, which is the behaviour the linked issue asks for.

The tests cover the formatter output (including quiet mode and ordering next to resource changes), reading the deployed value, the I4002 payload, and the CLI exit code.

Resolved since an earlier review (2)
  • 🟡 Recommended · Thread #4706505229 · packages/@aws-cdk/toolkit-lib/lib/api/diff/diff-formatter.ts
  • ⚪ Optional · Thread #4706505240 · packages/@aws-cdk/toolkit-lib/lib/api/cloudformation/nested-stack-helpers.ts

Open findings: 🔴 0 blocking · 🟡 0 recommended · ⚪ 0 optional.


Generated automatically. React 👍 or 👎 to tell us whether this review helped, so we can improve these reviews.

@maintainer-for-aws maintainer-for-aws Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the review summary comment for the overview; the notes below are inline.

@codecov-commenter

codecov-commenter commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.53%. Comparing base (d71c502) to head (b3ca0ad).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2174   +/-   ##
=======================================
  Coverage   92.53%   92.53%           
=======================================
  Files          81       81           
  Lines       13238    13239    +1     
  Branches     1917     1916    -1     
=======================================
+ Hits        12250    12251    +1     
  Misses        950      950           
  Partials       38       38           
Flag Coverage Δ
suite.unit 92.53% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

azatoth added a commit to azatoth/aws-cdk-cli that referenced this pull request Nov 26, 2026
…aws#2173)

Relates to aws/aws-cdk#32043 (filed in
aws/aws-cdk before the CLI moved here). This PR covers deploy, and the
diff side is aws#2174, which will close the issue.

When a deployment changes only the stack's termination protection,
CloudFormation creates an empty change set. The deploy then reports `✅
MyStack (no changes)`, even though `UpdateTerminationProtection` was
called. The only trace of that call was a debug-level log line, so
nothing in the normal output said what changed.

This PR reports the update at info level:

```
MyStack: termination protection enabled
✅  MyStack (no changes)
```

### `noOp` is unchanged

One option was to return `noOp: false` in this case. I decided against
it: `Deployments.prepareStack()` and the deploy loop in
`Toolkit.deploy()` / `CdkToolkit.deploy()` use `noOp` to mean "there is
no change set to execute". With `noOp: false`, an empty prepared change
set is no longer final, and deploy would go through a second, executing
pass. `noOp` still means "CloudFormation had nothing to do", and the
termination protection change gets its own message.

### Out of scope: termination protection is changed before approval

`updateTerminationProtection()` runs in the change-set path even when
`execute: false`. For an executing deploy, the first phase is
`prepareStack()`, so termination protection is already updated before
the approval prompt is shown. Declining the prompt doesn't undo it.
Changing that would also change `--no-execute` behaviour, so I left it
for a separate discussion.

The diff side (showing the change in `cdk diff`) is a separate PR.

### Checklist
- [x] Unit tests added
- [x] No manual edits to generated files

---
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
@azatoth
azatoth force-pushed the fix/diff-termination-protection branch from e4e72c0 to 602044d Compare November 26, 2026 10:47
@azatoth
azatoth force-pushed the fix/diff-termination-protection branch from 602044d to cc5d785 Compare November 26, 2026 15:20
@azatoth
azatoth force-pushed the fix/diff-termination-protection branch from cc5d785 to b5af642 Compare November 30, 2026 08:42
*
* @default undefined - the stack does not exist, or the setting was not read
*/
readonly deployedTerminationProtection?: boolean;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put this in a structure? Something like CloudFormationStackProperties ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added CloudFormationStackProperties and used it for both RootTemplateWithNestedStacks.deployedStackProperties and TemplateInfo.deployedStackProperties.

/**
* A change in the termination protection of a stack
*/
export interface TerminationProtectionChange {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have a more generic ChangedValue<T> type? So that we can just say ChangedValue<bool> ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No ChangedValue<T>, but IDifference<T> from cloudformation-diff fits, so the field is now IDifference<boolean> and the custom type is gone.

azatoth and others added 3 commits December 3, 2026 19:37
`cdk diff` only compared templates, so a change to a stack's termination
protection was never shown and the stack was reported as unchanged.

When diffing against a deployed stack, read the deployed termination
protection setting along with the template, and show a difference with
the app's setting as a stack property change:

    Stack Properties
    [~] TerminationProtection: disabled to enabled

The stack is counted as changed. Nothing is compared when diffing
against a template file, or when the stack has not been deployed yet.

Refs aws/aws-cdk#32043

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Before this commit, the termination protection change only appeared in
the formatted text and in `numStacksWithChanges`, so a programmatic
caller could see a count of changed stacks while every returned diff
was empty.

Add an optional `terminationProtectionChange` field to the `StackDiff`
payload of CDK_TOOLKIT_I4002, and return it from `formatStackDiff()`.

Also use `StackStatus.isReviewInProgress` instead of comparing the
status name.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rence

Read the deployed termination protection into a `CloudFormationStackProperties`
structure, so other stack properties can be added next to it later.

Use the existing `IDifference<boolean>` from cloudformation-diff for the
termination protection change instead of a dedicated type.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@azatoth
azatoth force-pushed the fix/diff-termination-protection branch from 0c4b206 to b3ca0ad Compare December 3, 2026 18:37

This branch is waiting to be deployed

1 inactive (outdated) and 1 waiting deployments
integ-approval — b3ca0ad9 Waiting Dec 3, 2026 by azatoth via prepare #7841
automation — 18e21c6c Deployed Nov 22, 2026 by azatoth via Set AutoQueue on PR #2174 #4031
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

core: Indicate change of stack termination protection

4 participants