0

I downloaded this lambda package from github so I can run my lambda function on AWS. I didn't use all the folders, I only took the ones I needed.

So my .zip folder contains: lambda_function.py, Pillow, numpy, OpenCV and psycopg2.

I uploaded that .zip file to my lambda function. However upon testing it returns this error:

Unable to import module 'lambda_function': No module named 'PIL'

Not sure why I'm getting this error as PIL is imported in my code (the code is provided by AWS):

lambda_function.py

from __future__ import print_function
import boto3
import os
import sys
import uuid
from PIL import Image
import PIL.Image

s3_client = boto3.client('s3')

def resize_image(image_path, resized_path):
    with Image.open(image_path) as image:
        image.thumbnail(tuple(x / 2 for x in image.size))
        image.save(resized_path)

def handler(event, context):
    for record in event['Records']:
        bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key'] 
        download_path = '/tmp/{}{}'.format(uuid.uuid4(), key)
        upload_path = '/tmp/resized-{}'.format(key)

        s3_client.download_file(bucket, key, download_path)
        resize_image(download_path, upload_path)
        s3_client.upload_file(upload_path, '{}resized'.format(bucket), key)

Any idea why I'm getting this error?

Edit: The solution to the duplicate suggests creating an EC2 instance, which is not what I'm doing.

Zorgan
  • 8,227
  • 23
  • 106
  • 207
  • 2
    Possible duplicate of [AWS lambda Unable to import module 'lambda\_function': No module named PIL](https://stackoverflow.com/questions/50734416/aws-lambda-unable-to-import-module-lambda-function-no-module-named-pil) – iacob Jun 14 '18 at 09:55
  • you have to add compiled libraries, you can make a custom package either using docker or using EC2 instance you can follow this : https://stackoverflow.com/questions/50711722/how-to-upload-python-code-with-libraries-to-aws-lambda-from-windows-local-machin/50741325#50741325 – Mausam Sharma Jun 14 '18 at 13:32

1 Answers1

0

Because AWS lambda runs on the amazonlinux OS, you need to upload the packages installed on that OS. This repository will help. https://github.com/hidekuma/lambda-layers-for-python-runtime

Hidekuma
  • 1
  • 1