我正在创建一个 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 许可协议
错误是由于 lambda 函数的文件名引起的。在创建 lambda 函数时,它将请求 Lambda 函数处理程序。您必须将其命名为
Python_File_Name.Method_Name
。在这种情况下,我将其命名为lambda.lambda_handler
(lambda.py
是文件名)。请在下面找到快照。