0

I am trying to run aws lambda function with Pillow package but getting an import error. This error is like a standard error (talked here and here). What I understood from those threads is: to resolve the problem I should run the package installation in a docker container and then use that as a zip package from lambda function.

Following is my docker file:

FROM lambci/lambda:python3.6

USER root

ENV APP_DIR /var/task

WORKDIR $APP_DIR

COPY requirements.txt .
COPY bin ./bin
COPY lib ./lib

RUN mkdir -p $APP_DIR/lib
RUN pip3 install -r requirements.txt -t /var/task/lib

(i am using lambci/lambda:python3.6 to emulate python lambda environment locally and locally the lambda function runs fine).

My question is: once I run the docker-build command, I can see the image being build but I can't seem to figure out a way to zip the packages and dependencies (in lib and bin) installed and use that zip file to upload on s3.

Following is my docker-compose file:

version: '3'

services:
  lambda:
    build: .
    environment:
      - PYTHONPATH=/var/task/src:/var/task/lib
      - PATH=/var/task/bin
    volumes:
      - ./src/:/var/task/src/
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
monte
  • 1,482
  • 1
  • 10
  • 26
  • Start a container from the image. The files will be available in it. You can also copy them to your host by mounting a volume. – Klaus D. Feb 26 '21 at 07:19

1 Answers1

1

In my experience, the easiest way to add dependencies to a lambda function is through lambda layers. The layer can be created using docker described in the AWS blog.

Thus you can add pillow to your function as follows:

  1. Create empty folder, e.g. mylayer. The layer if fully independent from your project, so it can be created in any location, e.g. in /tmp.

  2. Go to the folder and create requirements.txt file with the content of pillow:

echo pillow > ./requirements.txt
  1. Run the following docker command:

The command will create layer for python3.6:

docker run -v "$PWD":/var/task "lambci/lambda:build-python3.6" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.6/site-packages/; exit"
  1. Archive the layer as zip:
zip -9 -r mylayer.zip python 
  1. Create lambda layer based on mylayer.zip in the AWS Console. Don't forget to specify Compatible runtime to python3.6.

  2. Add the the layer created in step 5 to your function.

Marcin
  • 215,873
  • 14
  • 235
  • 294