Issue with Importing Pillow Library for Image Processing in AWS Lambda Environment

0

I attempted to use the Pillow library for image processing tasks, specifically for image resizing and converting images to binary formats. After installing the library and packaging it into a ZIP file for deployment, I encountered the following import error:

Runtime.ImportModuleError: Unable to import module 'handler': cannot import name '_imaging' from 'PIL'

I have tried packaging the Pillow library using multiple environments, including Windows, Linux, and an EC2 instance (AWS Linux), but the issue persists. The error appears to be related to the _imaging module not being properly recognized during import.

asked 3 months ago318 views
2 Answers
1

Hi, Pillow is packaged as a standard Lambda layer.

So,

  1. Go to https://github.com/keithrozario/Klayers/tree/master/deployments
  2. Select your Python version
  3. Open corresponding html file
  4. Find the ARN of the Pillow library and copy
  5. Go to the Lambda console
  6. Open the setup of your Lambda
  7. Choose Add Layer
  8. paste the ARN of the Pillow layer that you copied before

Your Lambda should now run with Pillow

Best,

Didier

profile pictureAWS
EXPERT
answered 3 months ago
  • @Didier Do I need to do this using Linux, or is it okay to use Windows as well?

0

The issue you're experiencing with importing the Pillow library in your AWS Lambda environment is a common problem when working with libraries that have C or C++ components, like Pillow. This occurs because the binary dependencies need to be compatible with the Lambda execution environment.

To resolve this issue, you have a few options:

  1. Use a Lambda Layer: Create a Lambda Layer containing the Pillow library and its dependencies, compiled specifically for the Lambda environment. This allows you to separate the library from your function code and reuse it across multiple functions.

  2. Use a container image: Instead of packaging your function as a ZIP file, you can create a container image that includes the correct version of Pillow and its dependencies. This approach gives you more control over the runtime environment.

  3. Build the deployment package in a compatible environment: Use an Amazon Linux environment (like an EC2 instance) that matches the Lambda execution environment to build your deployment package. This ensures that the compiled binaries are compatible.

  4. Use the AWS SAM CLI with the --use-container option: If you're using AWS SAM for deployment, you can use the --use-container option when running 'sam build'. This downloads a container base image compatible with the Lambda execution environment and builds your function's deployment package in a Docker container using that image.

  5. Manually build the .zip deployment package: Build your function's .zip deployment package yourself using the correct source distribution binary for Pillow. Save the .zip file in the directory you specify as the CodeUri in your AWS SAM template or use it for direct upload to Lambda.

When working with image processing libraries like Pillow in Lambda, it's crucial to ensure that you're using the correct source distribution binary that's compatible with the Lambda execution environment. This often means compiling the library in an environment that closely matches Lambda's, or using pre-built binaries specifically designed for use with Lambda.

Remember to always test your function thoroughly after making these changes to ensure that the Pillow library is working correctly in the Lambda environment.
Sources
Create a serverless file-processing app - AWS Lambda
Troubleshoot deployment issues in Lambda - AWS Lambda

profile picture
answered 3 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions