Skip to content

Instantly share code, notes, and snippets.

@ArchTaqi
Created July 22, 2021 14:32
Show Gist options
  • Save ArchTaqi/86efadf861b25f894f6e21a0f3d1c846 to your computer and use it in GitHub Desktop.
Save ArchTaqi/86efadf861b25f894f6e21a0f3d1c846 to your computer and use it in GitHub Desktop.
How to Install Python Packages for AWS Lambda Layers

A guide on how to build Python Packages for AWS Lambda Layers using Docker containers.

Limitations of Lambda Layers

There are a few limitations that you need to be aware of and this includes:

  • You can only use up to 5 layers per Lambda.
  • The size of all your layers unzipped cannot exceed 250mb.
  • Layers are mounted to the /opt directory in the function’s execution environment so be sure to Layer your functions properly if you are going to have more than one.

1. Build

Create an python.lambda.layer.Dockerfile.

FROM amazonlinux:latest 

RUN yum install -y python37 && \
    yum install -y python3-pip && \
    yum install -y zip && \
    yum clean all
RUN python3.7 -m pip install --upgrade pip && \
    python3.7 -m pip install virtualenv
usr> docker build -f "python.lambda.layer.Dockerfile" -t lambdalayer:latest .

2. Run

usr> docker run -it --name lambdalayer lambdalayer:latest bash

3. Install

bash> python3.7 -m venv pandas
bash> source pandas/bin/activate
(pandas) bash> pip install pandas numpy requests -t ./python
(pandas) bash> deactivate
  1. Package
bash> zip -r python.zip ./python/usr> docker cp lambdalayer:python.zip
  1. Upload

Upload the python.zip file to your lambda layers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment