Background
I have been struggling for the past few days to deploy a Lambda that uses Pillow, and I am deploying using Python 3.6. It may be noteworthy a
Basically, you have to compile the libraries (eg, PIL) either using Docker or, even better, an EC2 instance.
Launch an Docker container like this: docker run --rm -it -v "%cd%:/code" lambci/lambda:build-python3.6 sh
Inside there, navigate to the /code dir (cd /code
), create a virtualenv (virtualenv env
), activate it (source env/bin/activate
) and finally, install your library (pip install pillow
).
Once you have installed your library, you can exit the container. The secret here is to move your package library to the root folder (where your main .py file is). For example, move the folder env/lib/python3.6/site-packages/PIL
to the root.
Then, zip your PIL folder together with your .py file and you're set!
I just did it and documented it in full here: https://learn-serverless.org/post/deploying-pillow-aws-lambda/
You can use a precompiled version of the PIL available here: https://github.com/Miserlou/lambda-packages
Just extract PIL folder to the deployment package and it should work.
For anyone else also new to aws python and running into this issue, you can use the layers feature, and there are existing layers here you can link to and this worked for me.
https://github.com/keithrozario/Klayers
Pillow specifically on us-east-1:
arn:aws:lambda:us-east-1:770693421928:layer:Klayers-python38-Pillow:2