Some recipes for AWS Lambda and Docker Python artefacts (including cross-building from macOS) #18756
huonw
started this conversation in
Development
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Questions often come up about building AWS Lambda and docker artefacts effectively on Slack. I've answered a few based on my experience using pants to deploy AWS Lambda (via zips) and Fargate for a while.
This discussion post collects what is currently working for us to deploy dozens of lambdas and a handful of Fargate containers, to be a reference that I can link to. None of this is perfect, but it's been good enough, and, once we worked through the initial set-up, pants packaging has never been the cause of a deploy failure.
We develop on ARM64 macOS, and deploy to x86-64 Linux. We run pants natively, and try to do as much as work natively too as reasonable because that is noticeably more efficient than emulating the OS, and very much more efficient than emulating the architecture.
We also implemented all this well before Pants 2.15 and its experimental environments feature (https://blog.pantsbuild.org/environments-simpler-multi-platform-workflows/): that can likely be used too, but I cannot report on that.
Basic setup
(UPDATE: With newer Pants, this isn't required. They use a new-enough PEX by default.)
Old requirements for Pants 2.14
We're currently using Pants 2.14 and PEX v2.1.128. Upgrading the latter is required to pick up a few bug fixes that were affecting our Lambda and docker builds. Our
pants.toml
looks something like:Python AWS Lambda zip artefacts
UPDATE: for Pants 2.18, this can be simplified fairly significantly, but we haven't applied it locally yet. The instructions in https://www.pantsbuild.org/docs/awslambda-python now see most of the benefits here.
Most important: use the
complete_platforms=
parameter, NOTruntime=
python_awslambda
target: https://www.pantsbuild.org/docs/awslambda-pythonruntime
parameter, to tell PEX about the environment:3rdparty/platforms/aws_lambda_python_3_9.json
with aBUILD
file:ap-southeast-2
, as of 8 months ago, so your environment may differ): lambda-complete-platform.zippex_binary
targets #15454, to reduce the size of the deployed package. You may need to adjust them, based on what packages you use.python_awslambda
target, and generates the same sort of output (a zip indist/
): (As above, note that theruntime=
parameter is not used.)dist/some.code/some_name.zip
path to pass tolambda.Code.fromAsset
:pex_binary
targets #15454Docker image artefacts from PEX
complete_platforms
to be able to build PEX files natively on macOS:python:3.9-bullseye
containers, and thus have addeddocker_python_3_9_bullseye_arm64.json
anddocker_python_3_9_bullseye_amd64.json
files to3rdparty/platforms
; here's theBUILD
file including how we generate them:pex_binary
targets #15454pants.toml
configuration is:COPY
s, without runningPEX_TOOLS=1 ...
themselves (integrates better with the pants cache), and/or Support for usedocker buildx
/ BuildKit features #15199 (integrates better with the Docker layer cache)pants.ci.toml
)pex_binary
targets #15454docker_image
dependency inference to cover source files (i.e.file
,shell_source
) #16561Beta Was this translation helpful? Give feedback.
All reactions