Getting PIL/Pillow 4.2.1 to upload properly to AWS Lambda Py3.6

后端 未结 3 690
温柔的废话
温柔的废话 2021-02-19 04:03

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

相关标签:
3条回答
  • 2021-02-19 04:22

    Basically, you have to compile the libraries (eg, PIL) either using Docker or, even better, an EC2 instance.

    1. Launch an Docker container like this: docker run --rm -it -v "%cd%:/code" lambci/lambda:build-python3.6 sh

    2. 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).

    3. 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/

    0 讨论(0)
  • 2021-02-19 04:42

    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.

    0 讨论(0)
  • 2021-02-19 04:47

    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

    0 讨论(0)
提交回复
热议问题