Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

feat(cli): add --mode options to diff command. deprecates --change-set/--no-changeset #32830

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

msessa
Copy link

@msessa msessa commented Jan 10, 2025

Issue # (if applicable)

Closes #28753

Reason for this change

The current behaviour of implicitly reverting to template-only diff can hide important error messages from the user that can help catching mistakes early before deployment.

This is especially true when a template uses transforms or when using changeset-level cloudformation hooks to enforce compliance rules.

See an example code snippet at the bottom

Description of changes

Added a --mode option to the diff command that replaces (deprecates) --change-set/--no-changeset.

The following modes are supported:

  • auto : Attempts changeset creation and fallback to local mode should any error be encountered. (replaces -change-set)
  • change-set: Attempts changeset creation but doesn't handle errors returned by cloudformation when creating a changeset for an existing stack. Instead those errors are surfaced to the user
  • template-only: Uses template-only diff (replaces --no-change-set)

Description of how you validated changes

  • Added unit test
  • Added integration test
  • Tested against the example code at the bottom

Checklist

Example Code

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';

const app = new cdk.App();

export class TestStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Deploy first without this resource, then add and diff
    new cdk.CfnOutput(this, 'TestOutput', {
      value: cdk.Fn.transform('MyTransform', { Param: "Value"}).toString()
    })
  }
}

new TestStack(app, 'MyTestStack');

app.synth()

Current behaviour on diff:

$ npx cdk diff MyTestStack
Hold on while we create a read-only change set to get a diff with accurate replacement information (use --no-change-set to use a less accurate but faster template-only diff)
Could not create a change set, will base the diff on template differences (run again with -v to see the reason)
Stack MyTestStack
Outputs
[+] Output TestOutput TestOutput: {"Value":{"Fn::Transform":{"Name":"MyTransform","Parameters":{"Param":"Value"}}}}

New behaviour on diff:

$ npx cdk diff --no-fallback MyTestStack
Hold on while we create a read-only change set to get a diff with accurate replacement information (use --no-change-set to use a less accurate but faster template-only diff)
Failed to create ChangeSet cdk-diff-change-set on MyTestStack: FAILED, No transform named 000000000000::MyTransform found.

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

@msessa msessa requested a review from a team as a code owner January 10, 2025 03:05
@aws-cdk-automation aws-cdk-automation requested a review from a team January 10, 2025 03:05
@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort p2 beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK labels Jan 10, 2025
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

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

The pull request linter fails with the following errors:

❌ Features must contain a change to an integration test file and the resulting snapshot.
❌ CLI code has changed. A maintainer must run the code through the testing pipeline (git fetch origin pull/32830/head && git push -f origin FETCH_HEAD:test-main-pipeline), then add the 'pr-linter/cli-integ-tested' label when the pipeline succeeds.

If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.

@aws-cdk-automation aws-cdk-automation added the pr/needs-cli-test-run This PR needs CLI tests run against it. label Jan 10, 2025
@msessa msessa force-pushed the feat/cli-diff-no-fallback branch from 0a38fbc to c0bf4db Compare January 10, 2025 13:51
@msessa
Copy link
Author

msessa commented Jan 10, 2025

Clarification Request: Integration test has been added but the linter still doesn't seems too happy. Anything I can do about that?

@aws-cdk-automation aws-cdk-automation added pr/reviewer-clarification-requested The contributor has requested clarification on feedback, a failing build, or a failing PR Linter run pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. labels Jan 10, 2025
@msessa msessa force-pushed the feat/cli-diff-no-fallback branch from c0bf4db to 8e9f761 Compare January 11, 2025 01:28
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.

Thanks for getting started on this!

In stead of introducing a new boolean option that only sometimes has an effect, we should create a new 3-way switch option to replace the current --change-set and --no-change-set options.

Something like

--mode=auto
--mode=change-set
--mode=template-only

@msessa msessa force-pushed the feat/cli-diff-no-fallback branch from 8e9f761 to e7a536b Compare January 12, 2025 22:34
@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jan 12, 2025
@msessa msessa force-pushed the feat/cli-diff-no-fallback branch from e7a536b to 95be843 Compare January 14, 2025 00:16
@msessa msessa changed the title feat(cli): add --no-fallback options to disable template-only diffs feat(cli): add --mode options to diff command. deprecates --change-set/--no-changeset Jan 14, 2025
@msessa
Copy link
Author

msessa commented Jan 14, 2025

@mrgrain I have addressed your feedback. Let me know what you reckon

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: ea60fe6
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@samson-keung samson-keung added the cli Issues related to the CDK CLI label Jan 23, 2025
@aws-cdk-automation
Copy link
Collaborator

This PR cannot be merged because it has conflicts. Please resolve them. The PR will be considered stale and closed if it remains in an unmergeable state.

@aws-cdk-automation
Copy link
Collaborator

This PR has been in the CHANGES REQUESTED state for 3 weeks, and looks abandoned. To keep this PR from being closed, please continue work on it. If not, it will automatically be closed in a week.

@mrgrain mrgrain self-assigned this Feb 3, 2025
@mrgrain mrgrain added the pr-linter/do-not-close The PR linter will not close this PR while this label is present label Feb 3, 2025
@mrgrain
Copy link
Contributor

mrgrain commented Feb 3, 2025

@msessa FYI, we've parked this to prioritize some other work in the CLI area. I'll be getting back it end of February. Apologies for the delay.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. cli Issues related to the CDK CLI effort/medium Medium work item – several days of effort p2 pr/needs-cli-test-run This PR needs CLI tests run against it. pr/reviewer-clarification-requested The contributor has requested clarification on feedback, a failing build, or a failing PR Linter run pr-linter/do-not-close The PR linter will not close this PR while this label is present
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-cdk): cdk diff always fallback to no change-set diff
4 participants