I currently try to get this AWS Lambda Getting started tutorial running: https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-deployment-pkg.html#with-s3-example-deployment-pkg-python
However, I always receive an error:
{
"errorMessage": "Unable to import module 'CreateThumbnail': cannot import name '_imaging' from 'PIL' (/var/task/PIL/__init__.py)",
"errorType": "Runtime.ImportModuleError"
}
Log output
START RequestId: fefba1d1-443c-4617-a5ad-c3aac19e5591 Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'CreateThumbnail': cannot import name '_imaging' from 'PIL' (/var/task/PIL/__init__.py)
END RequestId: fefba1d1-443c-4617-a5ad-c3aac19e5591
REPORT RequestId: fefba1d1-443c-4617-a5ad-c3aac19e5591 Duration: 1.52 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 71 MB
I went that far to build my .zip from a lambci/docker-lambda
image. But it didn't resolve my problem.
Here what's inside my .zip. Do you have any ideas, why I still get this error?
Using python 3.6 instead of 3.7 would just give me a different error. It seems AWS lambda is missing some components due to the way it was built.
5-minutes solution that worked for me:
go to https://github.com/keithrozario/Klayers/tree/master/deployments/python3.8/arns (note this is python 3.8)
select the file for the region your lambda runs into
get the ARN for the latest Pillow version
on your Lambda in the AWS console, click "Layers (0)"
add a new layer:
Save all and it should just work! However you will have to make sure that redeploying the lambda keeps the layer somehow.
Full credits to this life saving blog post https://medium.com/@derekurizar/aws-lambda-python-pil-cannot-import-name-imaging-11b2377d31c4
I was still facing the error despite trying all these steps. Finally I was able to find my issue :)
For me the Python version in AWS Lamda runtime was python 3.7.7. I installed Pillow using python 3.6. There seems to be some issue with compatibility of PIL with differnt versions of python.
So to resolve the issue either change AWS lambda runtime to python3.6.( This is what I tried and did work)
Alternatively one could also try installing pillow for python 3.7.7 and copying this to lambda.
As there are issues with cython the preferrable way would be to install packages in EC2 with amazon linux or using a AWS AMI docker image.
To answer @hax comment, I ended up with this solution here: https://github.com/marcmetz/Create-AWS-Lambda-Function-with-Docker
The problem I faced was a different Python installation on my Mac than required on AWS. In order to use my solution install Docker, cd
in the repository and add your packages to requirements.txt
. Then simply run the following command:
docker build -t fbprophet . && \
docker run --rm -v $PWD:/export \
fbprophet cp upload-to-s3.zip /export
That will (1) create a Docker container based on this, (2) Installs all your packages and dependencies defined in the requirements.txt and then (3) gives you back a .zip file in the repository folder. That .zip file can be used for your Lambda Function.
Make sure to also adjust lambda_function.py
to whatever function you want there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With