Pants can create a Lambda-compatible zip file from your Python code, allowing you to develop your Lambdas in your repository instead of using the online Cloud9 editor.
FYI: how Pants does this
Add this to your
[GLOBAL] backend_packages.add = [ "pants.backend.awslambda.python", "pants.backend.python", ]
This adds the new
python_awslambda target, which you can confirm by running
./pants help python_awslambda
First, add your lambda function in a Python file like you would normally do with AWS Lambda. Specifically, create a function
def my_handler_name(event, context) with the name you want.
Then, in your BUILD file, make sure that you have a
python_sources target with the handler file included in the
sources field. You can use
./pants tailor :: to automate this.
python_awslambda target and define the
handler fields. The
runtime should be one of the values from https://docs.aws.amazon.com/lambda/latest/dg/lambda-python.html. The
handler has the form
handler_file.py:handler_func, which Pants will convert into a well-formed entry point. Alternatively, you can set
handler to the format
# The default `sources` field will include our handler file. python_sources(name="lib") python_awslambda( name="lambda", runtime="python3.8", # Pants will convert this to `project.lambda_example:example_handler`. handler="lambda_example.py:example_handler", )
def example_handler(event, context): print("Hello AWS!")
Pants will use dependency inference based on the
handler field, which you can confirm by running
./pants dependencies path/to:lambda. You can also manually add to the
You can optionally set the
output_path field to change the generated zip file's path.
filestargets will not be included in the built AWS Lambda because filesystem APIs like
open()would not load them as expected. Instead, use the
resourcestarget. See Assets and archives for further explanation.
./pants package on your
python_awslambda target to create a zipped file.
$ ./pants package project/awslambda_example.py Wrote code bundle to dist/project.zip Runtime: python3.8 Handler: lambdex_handler.handler
Running from macOS and failing to build?
AWS Lambdas must run on Linux, so Pants tells PEX and Pip to build for Linux when resolving your third party dependencies. This means that you can only use pre-built wheels (bdists). If your project requires any source distributions (sdists) that must be built locally, PEX and pip will fail to run.
If this happens, you must either change your dependencies to only use dependencies with pre-built wheels or find a Linux environment to run
You can use any of the various AWS methods to upload your zip file, such as the AWS console or the AWS CLI via
aws lambda create-function and
aws lambda update-function-code.
You must specify the AWS lambda handler as
To deploy a Python lambda function with container images, you can use Pants's Docker support.
FROM public.ecr.aws/lambda/python:3.8 WORKDIR /build RUN yum install unzip -y COPY project/lambda.zip /build RUN unzip /build/lambda.zip -d /app WORKDIR /app CMD ["/app/lambdex_handler.handler"]
python_sources() python_awslambda( name="lambda", runtime="python3.8", handler="main.py:lambda_handler" ) docker_image( name="my_image", dependencies = [":lambda"], )
./pants package project:my_image, for example. Pants will first build your AWS Lambda, and then will build the Docker image and copy it into the AWS Lambda.
Updated 3 months ago