-
-
Notifications
You must be signed in to change notification settings - Fork 644
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for packaging python AWS Lambda layers (#19123)
This fixes #18880 by making pants able to create a AWS Lambda package with the layout expected by a "Layer" (https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html). For Python, this the same as a normal Lambda, except within a `python/`: `python/cowsay/__init__.py` will be importable via `import cowsay`. The big win here is easily separating requirements from sources: ```python # path/to/BUILD python_sources() python_awslambda( name="lambda", entry_point="./foo.py:handler", include_requirements=False ) python_aws_lambda_layer( name="layer", dependencies=["./foo.py"], include_requirements=True, include_sources=False, ) ``` Packaging that will result in `path.to/lambda.zip` that contains only first-party sources, and `path.to/layer.zip` that contains only third-party requirements. This results in faster builds, less cache usage, and smaller deploy packages when only changing first-party sources. This side-steps some of the slowness in #19076. For the example in that PR: | metric | Lambdex | zip, no layers | using zip + layers (this PR) | |-------------------------------|----------|----------------|--------------------------------| | init time on cold start | 2.3-2.5s | 1.3-1.4s | not yet tested | | compressed size | 24.6MB | 23.8MB | 28KB (lambda), 23.8MB (layer) | | uncompressed size | 117.8MB | 115.8MB | 72KB (lambda), 115.7MB (layer) | | PEX-construction build time | ~5s | ~5s | ~1.5s (lambda), ~5s (layer) | | PEX-postprocessing build time | 0.14s | 4.8s | 1s (lambda), ~5s (layer) | That is, the first-party-only lambda package ~1000×; smaller, and is ~2× faster to build than even the Lambdex version. This uses a separate target, `python_aws_lambda_layer`. The target has its inputs configured using the `dependencies=[...]` field. For instance, the example above is saying create a lambda using all of the third-party requirements required (transitively) by `./foo.py`, and none of the first-party sources. (The initial implementation bdbc1cb just added a `layout="layer"` option to the existing `python_awslambda` target, but, per the discussion in this PR, that was deemed unnecessarily confusing, e.g. it'd keep the `handler` field around, which is meaningless for a layer.) Follow-up not handled here: - for 2.18: - documentation - renaming the `python_awslambda` target to `python_aws_lambda_function` to be clearer (NB. I proposing also taking the chance to add an underscore in the name, which I've done with the `python_aws_lambda_layer` target. Let me know if there's a strong reason for the `awslambda` name without an underscore. I note the GCF target is `python_google_cloud_function`) - not necessarily for 2.18: - potentially, adding "sizzle" like the ability to have `python_aws_lambda_function` specify which layers it will be used with, and thus automatically exclude the contents of the layer from the function (e.g. the example above could hypothetically replace `include_requirements=False` with `layers=[":layer"]`)
- Loading branch information
Showing
7 changed files
with
287 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.