AWS Lambda : How to use Pillow library?
Asked Answered
S

2

9

I'm trying to create an AWS lambda function in order to create thumbnail of my uploaded images. My script is running well locally, I followed this tutorial to deploy my function but I have a problem with the Pillow library, indeed when I'm testing my function I can see this following log :

enter image description here

I found this post with the same issue but in my case I can't execute command line on the machine.

Sequela answered 23/6, 2016 at 9:36 Comment(2)
Solution: look here github.com/Miserlou/lambda-packages/tree/master/lambda_packages - they have precompiled PIL packagesMarigolde
Hey, I posted a solution here that does not require Docker. You just create a layer, the trick being that you have the correct version of Python locally, which you can install if needed. https://mcmap.net/q/242213/-getting-pil-pillow-4-2-1-to-upload-properly-to-aws-lambda-py3-6Jap
C
6

You must include the libjpeg.so in your lambda package, but it will also require some tweaking with the patchelf utility. Assuming that you prepare the lambda package via "pip install module-name -t" (rather than via virtualenv), do the following:

cd into/your/local/lambda/package/dir
cp -L $(ldd PIL/_imaging.so|grep libjpeg|awk '{print $3}') PIL/
patchelf --set-rpath PIL PIL/_imaging.so
# zip, deploy and test the package

This script works for Pillow version 3.2.0.

Regarding patchelf: under Ubuntu it can be 'apt install'ed, but under other Linuxes it may need to be built from source.

Cyanamide answered 24/6, 2016 at 15:2 Comment(1)
It is strange, but it works only if I add the following line sys.path.append("./PIL")Dictation
K
0

The problem here is that Pillow uses native libraries that must be built for the exact correct environment.

I solved this by installing my requirements in a Docker container that replicates very closely the AWS Lambda environment, lambci/lambda. I used the build-python3.8 version.

I installed my requirements there and zipped up the whole contents of /var/lang/lib/python3.8/site-packages/ directory along with my lambda function file.

I tried this with a standard Amazon Linux Docker image and it didn't work. Only the lambci/lambda image worked for me.

Katelynnkaterina answered 18/9, 2020 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.