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(lambda-python): add optional poetry bundling exclusion list parameter #23670

Merged

Conversation

ryanandonian
Copy link
Contributor

@ryanandonian ryanandonian commented Jan 12, 2023

A summary of this change is: change from use of cp to rsync --exclude='x' in the bundling commands for poetry based lambdas.

The intention of this PR is to enable the bundling code for Poetry projects to exclude certain files and/or folders from the bundled assets. Currently, if developing a python lambda using either virtualenv itself or a toolchain that leverages virtual environments (re: poetry, specifically with virtualenv.in-project = true, which is strongly recommended for leveraging python tools in VSCode), the bundling code will copy the entire folder passed in. This leads to copying the entire .venv directory into the bundled assets, even though the directory is ignored. Ultimately this leads to inflating the assets by the size of unzipped dependencies (numpy, for instance, is 50Mb by itself).

I verified this concept works in another project I maintain which leverages @aws-cdk/aws-lambda-python-alpha by manually editing the bundling file (I mentioned it in more detail in the linked issue #22585), but this temporary approach requires manually editing files from node_modules, so it is not a proper fix.

fixes #22585

[Post-merge edit] This feature was extended for all Python bundle tools (pipenv, virtualenv, etc), not just Poetry. I forgot to edit the title before merge. Sorry for the confusion.


All Submissions:

Adding new Construct Runtime Dependencies:

* [ ] This PR adds new construct runtime dependencies following the process described here

New Features

  • Have you added the new feature to an integration test?
    • Did you use yarn integ to deploy the infrastructure and generate the snapshot (i.e. yarn integ without --dry-run)?

Note: I was unable to implement an integration test. My plan was to verify an asset was ignored from the existing poetry integration test sample directory packages/@aws-cdk/aws-lambda-python/test/lambda-handler-poetry by passing in ['.ignorefile'] and confirming that asset was excluded, but I was unable to get the test working due to SSM parameters missing. I wasn't sure if I could bootstrap this and get it working.

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

@gitpod-io
Copy link

gitpod-io bot commented Jan 12, 2023

@github-actions github-actions bot added the beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK label Jan 12, 2023
@aws-cdk-automation aws-cdk-automation requested a review from a team January 12, 2023 23:37
@github-actions github-actions bot added bug This issue is a bug. effort/small Small work item – less than a day of effort p2 labels Jan 12, 2023
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.

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

2 similar comments
@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 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.

@ryanandonian ryanandonian force-pushed the feat/aws-lambda-python-exclude-parameter-poetry branch from c22f341 to 642b4ac Compare January 30, 2023 18:45
@ryanandonian ryanandonian reopened this Jan 30, 2023
@ryanandonian
Copy link
Contributor Author

Accidentally messed up my fork with a sync. It should be back to a normal state.

@ryanandonian ryanandonian changed the title feat(aws-lambda-python): add optional poetry bundling exclusion list parameter feat(lambda-python): add optional poetry bundling exclusion list parameter Jan 30, 2023
@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.

@ryanandonian ryanandonian force-pushed the feat/aws-lambda-python-exclude-parameter-poetry branch from 6edf153 to 98a6fbb Compare February 7, 2023 19:23
add snapshot test verifying functionality
@aws-cdk-automation aws-cdk-automation dismissed their stale review February 7, 2023 23:41

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

@TheRealAmazonKendra
Copy link
Contributor

Accidentally messed up my fork with a sync. It should be back to a normal state.

Story of my life, lol.

*
* @default - Empty list
*/
readonly poetryAssetExcludes?: string[];
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason this should be scoped to poetry only and not just be called assetExcludes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Honestly, I was just trying to minimize the blast radius of this PR. But there is no real reason why this should be limited to poetry only. I can make the change to support all the packagers.

Seems like there might be existing issues that this PR could help out with too #23313

Copy link
Contributor

Choose a reason for hiding this comment

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

You could definitely add support for that if you wanted, but this PR is also fine in its current scope 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just pushed some new changes moving it to just assetExcludes. I will definitely need help verifying and cleaning up these integration tests, that seemed to have had a bit more of an impact adding this to the other tests 👀

comcalvi
comcalvi previously approved these changes Feb 14, 2023
Copy link
Contributor

@comcalvi comcalvi left a comment

Choose a reason for hiding this comment

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

lgtm

@mergify
Copy link
Contributor

mergify bot commented Feb 14, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

1 similar comment
@mergify
Copy link
Contributor

mergify bot commented Feb 15, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot dismissed comcalvi’s stale review February 18, 2023 06:36

Pull request has been modified.

@mergify
Copy link
Contributor

mergify bot commented Feb 18, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

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

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

@mergify mergify bot merged commit 53beeae into aws:main Feb 18, 2023
@mergify
Copy link
Contributor

mergify bot commented Feb 18, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@ryanandonian ryanandonian deleted the feat/aws-lambda-python-exclude-parameter-poetry branch February 21, 2023 16:17
@cwaldbieser
Copy link

2 quick questions-- will this also allow exclusions for those of us using pipenv rather than poetry? Or for those who just want to create the virtualenv in the assets folder for other logistical reasons?
And are there some docs on how to use the bundling option? I think I get the idea from looking at the code, but a short example would help.

@ryanandonian
Copy link
Contributor Author

Hey @cwaldbieser - it originally was slated to be poetry-only (hence the title), but after feedback it was raised up to be for all Python build systems. I unfortunately forgot to change the title to reflect those changes. I'm not sure if there are mechanical implications in this repository to changing that title post-merge, it might complicate something that I'm unaware of so I can't safely change it. But these changes should let you utilize it regardless of packager.

There should be a simple usage example in the Packaging > Excluding source files section of this specific module's README.

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. effort/small Small work item – less than a day of effort p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-lambda-python-alpha): bundling fails with .venv dir in "entry" dir
5 participants