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.
See the Python example repository for an example of creating an AWS Lambda from Python code.
FYI: how Pants does this
Add this to your
[GLOBAL] backend_packages2.add = [ "pants.backend.awslambda.python", "pants.backend.python", ]
This adds the new
python_awslambda target and the
awslambda goal, which you can confirm by running
<<pantscmd>> target-types --details=python_awslambda and
First, add your lambda function in Python like you would normally do with AWS Lambda. Specifically, create a function
def handler_name(event, context).
Then, in your BUILD file, create a
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
python_awslambda( sources=["awslambda_example.py"], runtime="python3.8", handler="project:example_handler", )
def example_handler(event, context): print("Hello AWS!")
<<pantscmd>> awslambda on your
python_awslambda target to create a zipped file.
$ <<pantscmd>> awslambda 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
Updated over 3 years ago