You are new to serverless on AWS, and you feel there python packages are missing, right ? Except for numpy and scipi, you are right : there are no packages by default on AWS lambda functions. Here is how you can install them.

1 - Install your package on your computer

let’s say you want to install numpy. We begin on our local computer, creating a working fodler and typing :

mkdir working_dir
cd working_dir
docker run --rm -v $(pwd):/foo -w /foo lambci/lambda:build-python3.7 pip install numpy -t python

Note : lambci/lambda:build-python3.7 can be changed to your better suited python version if required. It’s a way for us tu replicate AWS servers so that the installing process is similar to what we will push to AWS later.

2 - Store it online

Now that our package is installed into the /python directory, we can push it to AWS storage plateform called S3.

Zip the Folder

First, let’s zip our folder :

zip -r my_custom_numpy_layer python/

Push it to S3

Then, let’s go to AWS S3 interface and create a bucket in our favorite region (us-east-1 for instance). Now we can upload our my_custom_numpy_layer.zip file to this bucket ! Copy the Link Url

This step may not be required for small packages, but bugger ones have to be uploaded to S3. If you package is small, you can skip this S3 complication.

3 - Layer & Lambda Function

Create your Layer

We will now create our my_custom_numpy_layer on Lambda interface. Simply follow the instruction, see bellow:

create AWS layer

Don’t forget to mention the runtimes you want (python 3.7 in this example)

Add your layer

Now, just go to your Lambda function. In the “code” tab, you will be able to add a layer at the bottom of the webpage. It will look like this :
create AWS layer

Since you previously created the layer, you will see it appear in your custom layers !!! Easy.

Use your paackage

Here we go : in the code editor, you can now import numpy !!!

create AWS layer

Note :

Reference

Main source of inspiration : theashworld
Layer is not enough : use a Lamdba docker container