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

fix(lambda): fix so that the commandHook function is called only once during bundling. #32776

Conversation

ayame113
Copy link

@ayame113 ayame113 commented Jan 7, 2025

This is my first contribution to aws-cdk! So please let me know if I'm doing something wrong.

Issue # (if applicable)

Closes #31973.

Reason for this change

When using local bundling, the commandHook function was being called twice.
This PR fixes it so that it is only called once.

Description of changes

When the variable shouldBuildImage is true, a createBundlingCommand for the Docker build will be created as before.
When the variable shouldBuildImage is false, a createBundlingCommand for the local build will be created when bundler.local.tryBundle() is called. (Previously, both a createBundlingCommand for the Docker build and for the local build were created.)

After making this change, some tests failed because they were expecting the createBundlingCommand to be called immediately, so I added forcedDockerBundling: true to some of the tests.

Note: I was going to send a patch to aws-cdk-lib, but I found a similar code in aws-lambda-go-alpha, so I fixed it at the same time. Is that okay?

Description of how you validated changes

I've added a unit test.

Checklist


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

@ayame113 ayame113 changed the title chore(aws-cdk-lib): Fix so that the commandHook` function is called only once during bundling. chore(aws-cdk-lib): Fix so that the commandHook function is called only once during bundling. Jan 7, 2025
@github-actions github-actions bot added beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. effort/medium Medium work item – several days of effort p2 labels Jan 7, 2025
@aws-cdk-automation aws-cdk-automation requested a review from a team January 7, 2025 11:07
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 has failed. See the aws-cdk-automation comment below for failure reasons. 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.

@ayame113 ayame113 changed the title chore(aws-cdk-lib): Fix so that the commandHook function is called only once during bundling. chore(aws-cdk-lib): fix so that the commandHook function is called only once during bundling. Jan 7, 2025
@aws-cdk-automation aws-cdk-automation dismissed their stale review January 7, 2025 11:14

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@ayame113
Copy link
Author

ayame113 commented Jan 7, 2025

@Mergifyio update

Copy link
Contributor

mergify bot commented Jan 7, 2025

update

✅ Branch has been successfully updated

@ayame113 ayame113 changed the title chore(aws-cdk-lib): fix so that the commandHook function is called only once during bundling. fix(aws-cdk-lib): fix so that the commandHook function is called only once during bundling. Jan 7, 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 has failed. See the aws-cdk-automation comment below for failure reasons. 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.

@ayame113 ayame113 changed the title fix(aws-cdk-lib): fix so that the commandHook function is called only once during bundling. chore(aws-cdk-lib): fix so that the commandHook function is called only once during bundling. Jan 7, 2025
@aws-cdk-automation aws-cdk-automation dismissed their stale review January 7, 2025 11:32

✅ Updated pull request passes all PRLinter validations. Dismissing previous PRLinter review.

@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Jan 7, 2025
Copy link
Contributor

@lpizzinidev lpizzinidev left a comment

Choose a reason for hiding this comment

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

Thanks 👍
A couple of comments:

  1. I'd classify the PR as a fix(lambda) instead of a chore (that will require an integration test update as well to verify the change)
  2. Do we need to fix the behavior for Python runtimes as well?

@ayame113 ayame113 changed the title chore(aws-cdk-lib): fix so that the commandHook function is called only once during bundling. fix(lambda): fix so that the commandHook function is called only once during bundling. Jan 9, 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:

❌ Fixes must contain a change to an integration test file and the resulting snapshot.

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.

✅ A exemption request has been requested. Please wait for a maintainer's review.

@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 9, 2025
@ayame113
Copy link
Author

ayame113 commented Jan 9, 2025

@lpizzinidev

Thank you for the review! 👍

  1. I'd classify the PR as a fix(lambda) instead of a chore (that will require an integration test update as well to verify the change)

I fixed the PR title. Thanks.

I have a question about integration testing.
What kind of integration testing is necessary?

a. Deploy the Lambda stack and confirm that the commandHook is called only once.
b. Deploy the Lambda stack and confirm that no errors occur.

In the case of a, I'm wondering how to test it. Can I use jest for integration testing?
In the case of b, can I be exempted from adding a test because it is covered by existing tests?

  1. Do we need to fix the behavior for Python runtimes as well?

I think this logic is unnecessary for the Python runtime because there is no distinction between Docker bundling and Local bundling.

@lpizzinidev
Copy link
Contributor

@ayame113 Thanks for the follow up 👍

What kind of integration testing is necessary?

Looking at the docs, following

  1. Changing functionality that affects asset bundling

I guess an update/re-run to this Node test and this GO test should be enough.

@ayame113
Copy link
Author

ayame113 commented Jan 13, 2025

@lpizzinidev

I guess an update/re-run to this Node test and this GO test should be enough.

Thanks for the clarification! 👍
I reran those two tests and confirmed there were no snapshot changes that needed to be committed.
(This is expected, since the bundling results do not change whether the commandHooks are called twice or once.)

Please let me know if there are any other changes that should be added.

Verifying integration test snapshots...

   Waiting for 1 more (integ.function)
  UNCHANGED  integ.function 419.943s

Snapshot Results: 

Tests:    1 passed, 1 total

@lpizzinidev
Copy link
Contributor

@ayame113
Thanks for running the tests 👍
I think it would still be useful to update an integration test to make the build ✅ and have this PR merged earlier.

a. Deploy the Lambda stack and confirm that the commandHook is called only once.

Can we use an invokeFunction assertion to verify that the Lambda is bundling once? (sample code)

@ayame113
Copy link
Author

@lpizzinidev

I think it would still be useful to update an integration test to make the build ✅ and have this PR merged earlier.

Understood. Thanks for pointing that out!

Can we use an invokeFunction assertion to verify that the Lambda is bundling once? (sample code)

I think it's difficult.
In the previous behavior, the commandHook functions (beforeBundling, afterBundling) are called twice, but the command is actually executed once.
It would be nice if I could change the result of the Lambda invocation depending on the number of times commandHook was called and assert it, but I don't think there is a way to do that.

The test I can think of is the following.
Record the number of times beforeBundling and afterBundling are called, and if they are called more than twice, generate an error.
How about adding this inside an existing test?

@@ -15,11 +15,30 @@ class TestStack extends Stack {
   constructor(scope: Construct, id: string, props?: StackProps) {
     super(scope, id, props);
 
+    let afterBundlingCallCount = 0;
+    let beforeBundlingCallCount = 0;
+
     this.lambdaFunction = new lambda.GoFunction(this, 'go-handler-docker', {
       entry: path.join(__dirname, 'lambda-handler-vendor', 'cmd', 'api'),
       bundling: {
         forcedDockerBundling: true,
         goBuildFlags: ['-mod=readonly', '-ldflags "-s -w"'],
+        commandHooks: {
+          afterBundling() {
+            if (1 < afterBundlingCallCount) {
+              throw new Error('afterBundling should called only once');
+            }
+            afterBundlingCallCount++;
+            return [];
+          },
+          beforeBundling() {
+            if (1 < beforeBundlingCallCount) {
+              throw new Error('afterBundling should called only once');
+            }
+            beforeBundlingCallCount++;
+            return [];
+          },
+        },
       },
     });
   }

@lpizzinidev
Copy link
Contributor

@ayame113 Thanks a lot for looking into this!
Please adjust the integration tests as you suggest.
After looking into it further, I don't see a way to test it with assertions either.
I'm sure maintainers will provide more feedback if necessary.
I'll approve when the code is updated 🙏

@ayame113
Copy link
Author

Please adjust the integration tests as you suggest.
After looking into it further, I don't see a way to test it with assertions either.
I'm sure maintainers will provide more feedback if necessary.
I'll approve when the code is updated 🙏

@lpizzinidev
Thanks for looking into it! I've updated the integration tests according to your suggestions.

Copy link
Contributor

@lpizzinidev lpizzinidev left a comment

Choose a reason for hiding this comment

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

👍

@ayame113
Copy link
Author

Hi! Can I get a review from the maintainer for this PR?

@pahud
(I mentioned you because you commented on the referenced issue.)

@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.

Copy link

codecov bot commented Feb 1, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.39%. Comparing base (ac3ffa5) to head (d36dede).
Report is 262 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #32776      +/-   ##
==========================================
- Coverage   81.52%   81.39%   -0.14%     
==========================================
  Files         222      225       +3     
  Lines       13715    13714       -1     
  Branches     2417     2411       -6     
==========================================
- Hits        11181    11162      -19     
- Misses       2254     2277      +23     
+ Partials      280      275       -5     
Flag Coverage Δ
suite.unit 81.39% <ø> (-0.14%) ⬇️

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

Components Coverage Δ
packages/aws-cdk 80.70% <ø> (-0.27%) ⬇️
packages/aws-cdk-lib/core 82.10% <ø> (+<0.01%) ⬆️

@ayame113
Copy link
Author

ayame113 commented Feb 1, 2025

The pull request linter fails with the following errors:

❌ Fixes must contain a change to an integration test file and the resulting snapshot.
❌ CodeCov is indicating a drop in code coverage

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.

I have already addressed this change request.

Exemption Request

Is there anything else I should change? Or can I get it reviewed by the maintainers?

@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-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback. pr/needs-maintainer-review This PR needs a review from a Core Team Member labels Feb 1, 2025
@ayame113
Copy link
Author

ayame113 commented Feb 1, 2025

@Mergifyio update

Copy link
Contributor

mergify bot commented Feb 1, 2025

update

❌ Mergify doesn't have permission to update

For security reasons, Mergify can't update this pull request. Try updating locally.
GitHub response: refusing to allow a GitHub App to create or update workflow .github/workflows/README.md without workflows permission

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

@GavinZZ
Copy link
Contributor

GavinZZ commented Feb 21, 2025

Closing this PR in regards to the discussion #31973. Feel free to draft up a new PR. Thank you!

@GavinZZ GavinZZ closed this Feb 21, 2025
Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK bug This issue is a bug. effort/medium Medium work item – several days of effort p2 pr/needs-maintainer-review This PR needs a review from a Core Team Member pr/reviewer-clarification-requested The contributor has requested clarification on feedback, a failing build, or a failing PR Linter run pr-linter/exemption-requested The contributor has requested an exemption to the PR Linter feedback.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-lambda-nodejs): commandHooks is called twice when local bundling
5 participants