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

0

Following a tutorial I'm attempting to create a lambda function that listens for an image being uploaded to an S3 bucket and then generates a thumbnail and places it in another S3 bucket.

I've done the following actions:
-The lambda function is written in python and uses a python imaging library. The python file and library are zipped and uploaded as the function code.
-Defined an environment variable: key: DEST_BUCKET value: ds-animalpics-thumb
-Added a trigger event (called objectupload) to the originating S3 bucket which notifies the lambda function when a PUT operation occurs.

I get the following error when I attempt to test the lambda function. What I am I missing?

{
"errorMessage": "Unable to import module 'lambda_function': No module named 'lambda_function'",
"errorType": "Runtime.ImportModuleError"
}

The python file and execution role are below. Please let me know if there is anything else helpful to upload.

Python file called lambda_handler.py:

import os
import tempfile

import boto3
from PIL import Image

s3 = boto3.client('s3')
DEST_BUCKET = os.environ['DEST_BUCKET']
SIZE = 128, 128

def lambda_handler(event, context):

    for record in event['Records']:
        source_bucket = record['s3']['bucket']['name']
        key = record['s3']['object']['key']
        thumb = 'thumb-' + key
        with tempfile.TemporaryDirectory() as tmpdir:
            download_path = os.path.join(tmpdir, key)
            upload_path = os.path.join(tmpdir, thumb)
            s3.download_file(source_bucket, key, download_path)
            generate_thumbnail(download_path, upload_path)
            s3.upload_file(upload_path, DEST_BUCKET, thumb)

        print('Thumbnail image saved at {}/{}'.format(DEST_BUCKET, thumb))

def generate_thumbnail(source_path, dest_path):
    print('Generating thumbnail from:', source_path)
    with Image.open(source_path) as image:
        image.thumbnail(SIZE)
        image.save(dest_path)

Execution role:

{
  "Version": "2012-10-17",
  "Statement": [{
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:*:*:*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:GetObject"
      ],
      "Resource": "arn:aws:s3:::ds-animalpics-upload/*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "s3:PutObject"
      ],
      "Resource": "arn:aws:s3:::ds-animalpics-thumbs/*"
    }
  ]
}
已提問 4 年前檢視次數 18964 次
2 個答案
1

Your python file is called "lambda_handler.py" and the function is called "lambda_handler", but the error mentions "lambda_function". check that the "handler" value in your lambda configuration is correct.

Ellison
已回答 4 年前
0

It will be where the files are sat within the directory. If you do some testing by outputting the file directory structures you can see where the items are sat and then adjust them accordingly with the paths being called.

Not done it in Python personally but have had similar issues in NodeJS and PHP within Lambda.

已回答 4 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南