OpenAI AWS Lambda Layer (Open Source)
Best way to build a python lambda layer?
How do I import a Python lambda layer? - Stack Overflow
Open Source Lambda Layers Repository
Videos
There are lots of different articles online describing ways to build dependency layers for python lambda functions. Is the definitive way to build a lambda layer with packages that will work to do the following:
spin up a (linux) EC2
build a virtualenv and source it
pip install the packages you want
download the /pythonx.x/ directory and zip it
upload it as a lambda layer
This seems to be the most common recommendation, but I wanted to know what the actual best practice is for doing this. Thanks,
So I've recently ran into this issue, and I believe I found a cleaner way to import your layers.
First for the structure of the zip file which you upload:
- You do not need an
__init__.pyfile - Put all the scripts which you want to import into a folder name
python - Zip up that python folder (choose any name you want) and upload it to your layer
- Once uploaded, and the layer has been configured in your lambda function, you can simply use it with
import {filename}
So if your script in the python folder was called something like custom_helper.py, import it in your lambda with import custom_helper.
I am not sure if this is the clean way to do it, but it seems simple enough to start.
Your zip file should have the following structure:
python/lib/python3.7/site-packages
That is, it needs a folder named Python, and within that a folder named lib, and within that a folder named python3.7, and within that a folder named site-packages. Anything inside that folder will be available for import.
(If you're using another version of Python, that version should be in the path instead of 3.7)