“errorMessage”:“无法导入模块'lambda_function'

时间:2018-06-12 09:42:54

标签: python amazon-web-services amazon-s3

在测试我的lambda函数时,我收到以下错误消息:

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

我上传的.zip仅包含以下依赖项:requestsboto3PILPillow-4.0.0.dist-info& lambda函数lambda_function.py - 这是来自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)

我的处理程序是:lambda_function.lambda_handler

知道问题是什么吗?

1 个答案:

答案 0 :(得分:1)

<强>首先

默认情况下,lambda运行带有特定关键字的处理程序: lambda_handler 您也可以在AWS Lambda控制台中更改处理程序条目。

enter image description here

或者您必须更改处理程序的名称:

Route

您可以在使用某个框架时使用自定义处理程序,并明确提及处理程序名称。

例如,无服务器:

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

def lambda_handler(event,context):  
    '''

<强>其次

问题的一部分是您必须检查您导入的模块是否包含在内。你必须看到详细的错误输出。您的方案的即时响应是:无法导入lambda_function。

检查错误日志,您可能会看到如下内容: 无法导入模块'lambda_function':没有名为PIL的模块

您还必须在此处查看有关importing PIL

的此问题