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-alpha): cache python lambda dependencies usig lambda layer #30157

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

Conversation

orshemtov
Copy link

@orshemtov orshemtov commented May 11, 2024

Issue # (if applicable)

Closes #23829

Reason for this change

cdk synth takes very long to synthesize a PythonFunction construct, because the dependencies are getting installed regardless if there was any changes made to them.

The dependencies, whether they are specified in a requirements.txt file, a poetry.lock file or a pipenv.lock file, get installed as part of the CMD in the bundling phase, meaning that we don't use docker's cache or any other caching mechanism and re-install the dependencies from the internet every time.

Trying to compute a custom assetHash based on the dependencies file won't work, because of where the call for install is currently placed (in the CMD of the bundling phase container)

This causes deployment times to rise significantly, in some cases from minutes to hours.

Description of changes

The PythonFunction construct in function.ts will introduce a new prop: layer which is defined as:

/**
   * Whether or not to create a layer for the function's dependencies.
   * @default - No layer is created.
   */
  readonly layer?: boolean;

If layer is true, we create the layer before the constructor of PythonFunction is called, and attach the layer to PythonFunction in the constructor's layers prop.

To control whether dependencies are installed during the Bundling phase, a new prop is introduced to BundlingOptions and is defined as:

  /**
   * Whether or not to install the dependencies
   * @default true
   */
  readonly installDependencies?: boolean;

Since bundling is used for the PythonLayerVersion and PythonFunction props, this prop defaults to true, to not alter any behavior of PythonLayerVersion.

When initializing a PythonFunction, if layer is true, and we are creating a layer, the installDependencies is set to true for the layer creation, and false for the constructor of PythonFunction, so dependencies will only be installed in the layer.

To compute the assetHash for the layer, I added a dependenciesHash(entry) method inside the Packaging class, with this we can compute the hash of the lock file, and in the created layer we can use AssetHashType.CUSTOM with this hash.

bundling: {
          ...props.bundling,
          installDependencies: true,
          // assetExcludes: ["TODO: exclude everything except the dependencies file"]
          assetHashType: AssetHashType.CUSTOM,
          assetHash: Packaging.dependenciesHash(entry),
        },

Description of how you validated changes

This is my first PR here, and in open source in general, I am working on adding and validating the changes with unit tests, I would appreciate some help with that, if possible.

Checklist

TODOs

  • assetExcludes for the created layer, exclude all files besides the lock file
  • Add tests
  • Edit README.md

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

@github-actions github-actions bot added beginning-contributor [Pilot] contributed between 0-2 PRs to the CDK feature-request A feature should be added or improved. p1 labels May 11, 2024
@aws-cdk-automation aws-cdk-automation requested a review from a team May 11, 2024 13:16
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.

@orshemtov orshemtov changed the title feat(aws-lambda-python-alpha): Cache python lambda dependencies usig lambda layer feat(lambda-python-alpha): Cache python lambda dependencies usig lambda layer May 11, 2024
@orshemtov orshemtov changed the title feat(lambda-python-alpha): Cache python lambda dependencies usig lambda layer feat(lambda-python-alpha): cache python lambda dependencies usig lambda layer May 11, 2024
@aws-cdk-automation
Copy link
Collaborator

The pull request linter fails with the following errors:

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

PRs must pass status checks before we can provide a meaningful review.

If you would like to request an exemption from the status checks or clarification on feedback, please leave a comment on this PR containing Exemption Request and/or Clarification Request.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 9891f5b
  • Result: FAILED
  • Build Logs (available for 30 days)

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

@orshemtov
Copy link
Author

orshemtov commented May 22, 2024

Exemption Request

This is a first PR for me in this codebase, i have the general idea of the design and the required code changes, and could use some help with the PR process

@orshemtov orshemtov marked this pull request as ready for review May 22, 2024 19:21
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 feature-request A feature should be added or improved. p1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

(aws-lambda-python): cache Docker layer with dependencies
2 participants