1

Upon testing my lambda function I get the following error message:

{
  "errorMessage": "Unable to import module 'lambda_function'"
}

I uploaded a .zip which only includes the following dependancies: requests, boto3, PIL, Pillow-4.0.0.dist-info & the lambda function lambda_function.py - which is a copied example from AWS:

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)

My handler is: lambda_function.lambda_handler

Any idea what the problem is?

Zorgan
  • 8,227
  • 23
  • 106
  • 207

1 Answers1

2

Firstly:

By default lambda runs the handler with the specific keyword named: lambda_handler you can change the handler entry as well in the AWS Lambda console.

enter image description here

Or you have to change the name of your handler:

def handler(event, context):
    for record in event['Records']:
    ,,,

def lambda_handler(event,context):  
    '''

You can use custom handler while working with some framework and explicitly mention the handler name.

For example with serverless:

your_action:
    handler: path/lambda_function.handler

Secondly:

part of the issue is that you have to check for the modules you have been importing are included. You have to see a detailed error output for that. The immediate response for your scenario would be: unable to import lambda_function.

Checking error log you might see something like this: Unable to import module 'lambda_function': No module named PIL

You also must checkout this question here regarding importing PIL

NoorJafri
  • 1,787
  • 16
  • 27