Building AWS Lambda layers for Python #18841
-
Pants has good-looking support for building Python bundles for AWS Lambda. However, I'm wondering if there's an easy way to also build lambda layers. They're used for sharing code between lambdas and they're basically zip files of code with specific structure. Building them is similar but not quite the same as building lambda bundles. The use case I have in mind is that we have a bunch of lambdas that use the same set of dependencies, which we have bundled into a layer. This makes the bundle sizes smaller and deployments faster. Currently we're building the layer with AWS CDK's PythonLayerVersion construct, which we also use to deploy them, but it slows down our CDK runs. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
I'm not a pants maintainer, but I'm on a similar journey! At present, I'm just building layers separately, by zipping up site-packages, but I'm interested in revamping Pants' support for lambda, and writing a plugin for the serverless framework that uses pants to package both layers and functions. The current
To run the pexified code, you need to append the I've hit a couple of issues with lambda pex files resolving dependencies from layers. I've managed to get my scikit-learn layer to run by setting the env vars Let me know if you get any problems, cos I'm slowly building a mental picture of how a lambda target should work. |
Beta Was this translation helpful? Give feedback.
-
In 2.18, there's now native support for a NB. this is currently a very early dev release ( There's some other improvements to Lambdas happening in Pants 2.17 and 2.18 (#19076), so I'd recommend explicitly doing it in three steps, and running
There'll be a bunch more dev/alpha/RC releases for 2.18, and 2.16 (and 2.17) will hopefully be out soon, so experimenting will be smoother soon. |
Beta Was this translation helpful? Give feedback.
Actually, I clean forgot that I already wrote a prototype plugin for building lambda layers with Pants: https://github.com/bobthemighty/pants-lambda-layer
I haven't actually used this in production, because for my use cases I need to build a bunch of libraries from source, but if you're wanting to ship a layer full of ordinary Python modules, it might fit your needs. Feel free to open an issue on that repo if it doesn't work.