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.
Answer from Cecaro on Stack OverflowHow do I import a Python lambda layer? - Stack Overflow
Best way to build a python lambda layer?
amazon web services - How to use AWS Lambda layer using Python? - Stack Overflow
CDK PyPi Python Lambda Layer
Videos
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)
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,
I've seen that a few libraries like numpy and pandas don't work in Lambda when installed using pip. I have had success using the .whl package files for these libraries to create the Lambda layer. Refer to the steps below:
NOTE: These steps set up the libraries specific to the Python 3.7 runtime. If using any other version, you would need to download the
.whlfiles corresponding to that Python version.
Create an EC2 instance using Amazon Linux AMI and SSH into this instance. We should create our layer in Amazon Linux AMI as the Lambda Python 3.7 runtime runs on this operating system (doc).
Make sure this instance has Python3 and "pip" tool installed.
Download the numpy
.whlfile for thecp37Python version and themanylinux1_x86_64OS by executing the below command:
$ wget https://files.pythonhosted.org/packages/d6/c6/58e517e8b1fb192725cfa23c01c2e60e4e6699314ee9684a1c5f5c9b27e1/numpy-1.18.5-cp37-cp37m-manylinux1_x86_64.whl
- Skip to the next step if you're not using pandas. Download the pandas
.whlfile for thecp37Python version and themanylinux1_x86_64OS by executing the below command:
$ wget https://files.pythonhosted.org/packages/a4/5f/1b6e0efab4bfb738478919d40b0e3e1a06e3d9996da45eb62a77e9a090d9/pandas-1.0.4-cp37-cp37m-manylinux1_x86_64.whl
- Next, we will create a directory named "python" and unzip these files into that directory:
$ mkdir python
$ unzip pandas-1.0.4-cp37-cp37m-manylinux1_x86_64.whl -d python/
$ unzip numpy-1.18.5-cp37-cp37m-manylinux1_x86_64.whl -d python/
- We also need to download "pytz" library to successfully import numpy and pandas libraries:
$ pip3 install -t python/ pytz
- Next, we would remove the โ*.dist-infoโ files from our package directory to reduce the size of the resulting layer.
$ cd python
$ sudo rm -rf *.dist-info
This will install all the required libraries that we need to run pandas and numpy.
Zip the current "python" directory and upload it to your S3 bucket. Ensure that the libraries are present in the hierarchy as given here.
$ cd ..
$ zip -r lambda-layer.zip python/
$ aws s3 cp lambda-layer.zip s3://YOURBUCKETNAME
- The "lambda-layer.zip" file can then be used to create a new layer from the Lambda console.
Base on aws lamda layer doc, https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html your zip package for the layer must have this structure.
my_layer.zip
| python/numpy
| python/numpy-***.dist-info
So what you have to do is create a folder python, and put the content of site-packages inside it, then zip up that python folder. I tried this out with a simple package and it seem to work fine.
Also keep in mind, some package require c/c++ compilation, and for that to work you must install and package on a machine with similar architecture to lambda. Usually you would need to do this on an EC2 where you install and package where it have similar architecture to the lambda.