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년 전18970회 조회
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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠