Pants v2: Fast, consistent builds for Python and more

Welcome to the Pants v2 documentation hub!

Pants v2 is a fast, scalable build system for growing codebases. It's currently focused on Python, with support for other languages coming soon.

Here you'll find guides to help you get started with Pants v2, comprehensive documentation on how to configure, run and customize Pants v2, and information on how to get help from the Pants community.

Get Started

Python support

Create a Lambda with Python code.

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.

📘

Example repository

See the Python example repository for an example of creating an AWS Lambda from Python code.

📘

FYI: how Pants does this

Under-the-hood, Pants uses the Lambdex project. First, Pants will convert your code into a Pex file. Then, Pants will use Lambdex to convert the Pex into a zip file understood by AWS.

Step 1: Activate the Python AWS Lambda backend

Add this to your pants.toml:

[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

Step 2: Define a python_awslambda target

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_library target with the handler file included in the sources field. Add a python_awslambda target and define the runtime and 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 module.dotted.name:handler_func.

For example:

# The default `sources` field will include our handler file.
python_library(name="lib")

python_awslambda(
    name="lambda",
    dependencies=[":lib"],
    runtime="python3.8",
    handler="project:example_handler",
)
def example_handler(event, context):
    print("Hello AWS!")

You can optionally set the output_path field to change the generated zip file's path.

Step 3: Run package

Now run ./pants package on your python_awslambda target to create a zipped file.

For example:

$ ./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 ./pants package.

Step 4: Upload to AWS

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 lambdex_handler.handler.

Updated 10 days ago


Python support


Create a Lambda with Python code.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.