来自 Python 的 AWS 错误:没有名为 lambda_function 的模块

新手上路,请多包涵

我正在创建一个 AWS Lambda python 部署包。我正在使用一个外部依赖请求。我使用 AWS 文档 安装了外部依赖项。下面是我的 Python 代码。

 import requests

print('Loading function')

s3 = boto3.client('s3')

def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))

    # Get the object from the event and show its content type
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')
    try:
        response = s3.get_object(Bucket=bucket, Key=key)
        s3.download_file(bucket,key, '/tmp/data.txt')
        lines = [line.rstrip('\n') for line in open('/tmp/data.txt')]
        for line in lines:
            col=line.split(',')
            print(col[5],col[6])
        print("CONTENT TYPE: " + response['ContentType'])
        return response['ContentType']
    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

创建 Zip 项目目录的内容并上传到 lambda(Zip 目录内容,而不是目录)。当我执行该功能时,出现以下错误。

 START RequestId: 9e64e2c7-d0c3-11e5-b34e-75c7fb49d058 Version: $LATEST
**Unable to import module 'lambda_function': No module named lambda_function**

END RequestId: 9e64e2c7-d0c3-11e5-b34e-75c7fb49d058
REPORT RequestId: 9e64e2c7-d0c3-11e5-b34e-75c7fb49d058  Duration: 19.63 ms  Billed Duration: 100 ms     Memory Size: 128 MB Max Memory Used: 9 MB

原文由 Nithin 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 324
2 个回答

错误是由于 lambda 函数的文件名引起的。在创建 lambda 函数时,它将请求 Lambda 函数处理程序。您必须将其命名为 Python_File_Name.Method_Name 。在这种情况下,我将其命名为 lambda.lambda_handlerlambda.py 是文件名)。

请在下面找到快照。 在此处输入图像描述

原文由 Nithin 发布,翻译遵循 CC BY-SA 4.0 许可协议

如果您要上传 zip 文件。确保您正在压缩目录的内容而不是目录本身。

原文由 2ank3th 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题