Skip to content

feat: add drift detection to cdk as cdk drift #442

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 35 commits into
base: main
Choose a base branch
from

Conversation

Leo10Gama
Copy link
Member

@Leo10Gama Leo10Gama commented May 1, 2025

Fixes # N/A

Added an additional command, cdk drift, which invokes drift detection. Prints an output of the drift results listing out any resources in the stack that may have drifted.

Tested via yarn build && yarn test and adding in a few integ tests.

Running the command locally shows the following. With one resource drifted:
image

With no resources drifted: (image outdated)
image


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

@aws-cdk-automation aws-cdk-automation requested a review from a team May 1, 2025 18:24
@github-actions github-actions bot added the p2 label May 1, 2025
@rix0rrr
Copy link
Contributor

rix0rrr commented May 2, 2025

Thanks! This is a great feature to have!

I understand that we're detecting some kind of "diff" from a user point of view, but I'm very put-off by drift detection being part of cdk diff. That's just not what CDK diff historically did, and it's confusing to me. I wonder if it will be confusing to more people?

┌────────────┐            ╔═════════════╗            ┌────────────┐
│            │    diff    ║             ║    drift   │            │
│  Template  │◀──────────▶║    Stack    ║◀──────────▶│  Reality   │
│            │            ║             ║            │            │
└────────────┘            ╚═════════════╝            └────────────┘
                                                                   
                   ^                                               
               cdk diff                                            
            does this today                                        

Can we make this its own subcommand to begin with? cdk drift ?

Tested via yarn build && yarn test, which both compile.

Surely there's more? 🥹 An integ test would be nice.

Copy link
Contributor

@mrgrain mrgrain left a comment

Choose a reason for hiding this comment

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

Also needs integ tests and added to toolkit-lib (see Rico's comments).

If we are adding this as a new action/command, the action code should live in toolkit-lib and CLI just be calling it.

@GavinZZ
Copy link
Contributor

GavinZZ commented May 2, 2025

Thanks! This is a great feature to have!

I understand that we're detecting some kind of "diff" from a user point of view, but I'm very put-off by drift detection being part of cdk diff. That's just not what CDK diff historically did, and it's confusing to me. I wonder if it will be confusing to more people?

┌────────────┐            ╔═════════════╗            ┌────────────┐
│            │    diff    ║             ║    drift   │            │
│  Template  │◀──────────▶║    Stack    ║◀──────────▶│  Reality   │
│            │            ║             ║            │            │
└────────────┘            ╚═════════════╝            └────────────┘
                                                                   
                   ^                                               
               cdk diff                                            
            does this today                                        

Can we make this its own subcommand to begin with? cdk drift ?

@rix0rrr Let's discuss this further during Tuesday's meeting. While I'm open to making this a separate command, I'd like to explain my original reasoning for including it in cdk diff:

I envisioned cdk diff as a broader capability for detecting any differences affecting a deployed stack, whether those differences:

  • Come from changes in local CDK code
  • Result from manual modifications to deployed resources through console or CLI (Drifts)

However, I recognize your point about the historical context of cdk diff and the potential for user confusion. This would be a good topic for us to explore during the meeting, weighing the benefits of command consolidation against clarity and user expectations.

Leonardo Gama added 3 commits May 2, 2025 13:48
@rix0rrr
Copy link
Contributor

rix0rrr commented May 5, 2025

I envisioned cdk diff as a broader capability for detecting any differences affecting a deployed stack, whether those differences:

I see that, and I like the idea of a general cdk whatsup command.

There is some precedent for having certain features both as their own commands as well as options to other commands.

So I would propose to have both:

$ cdk drift
$ cdk diff --drift 

@mrgrain
Copy link
Contributor

mrgrain commented May 5, 2025

So I would propose to have both:

came here to say this. cdk drift should be the main command, cdk diff --drift is a convenience short cut.

@Leo10Gama
Copy link
Member Author

Setting to draft until AppSec signs off

@Leo10Gama Leo10Gama marked this pull request as draft May 14, 2025 17:42
auto-merge was automatically disabled May 14, 2025 17:42

Pull request was converted to draft

@Leo10Gama Leo10Gama changed the title feat: add drift detection to cdk diff feat: add drift detection to cdk as cdk drift May 15, 2025
Leonardo Gama added 2 commits May 15, 2025 13:22
Serves to protect potentially sensitive information in the output
from being intercepted.
@codecov-commenter
Copy link

codecov-commenter commented May 15, 2025

Codecov Report

Attention: Patch coverage is 82.44275% with 23 lines in your changes missing coverage. Please review.

Project coverage is 79.04%. Comparing base (7e23e64) to head (c10140c).

Files with missing lines Patch % Lines
packages/aws-cdk/lib/cli/cdk-toolkit.ts 87.60% 15 Missing ⚠️
packages/aws-cdk/lib/cli/cli.ts 11.11% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #442      +/-   ##
==========================================
- Coverage   79.75%   79.04%   -0.72%     
==========================================
  Files          46       46              
  Lines        6994     7124     +130     
  Branches      788      784       -4     
==========================================
+ Hits         5578     5631      +53     
- Misses       1394     1475      +81     
+ Partials       22       18       -4     
Flag Coverage Δ
suite.unit 79.04% <82.44%> (-0.72%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 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.

@Leo10Gama Leo10Gama temporarily deployed to integ-approval May 15, 2025 22:00 — with GitHub Actions Inactive
/**
* Detect infrastructure drift for the given stack(s)
*/
public async drift(options: DriftCommandOptions): Promise<number> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we'd want this code to live in @aws-cdk/toolkit-lib, and then call it from the CLI.

Right, @mrgrain ?

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants