【发布时间】:2016-05-22 07:45:05
【问题描述】:
我正在创建一个 AWS Lambda python 部署包。我正在使用一个外部依赖请求。我使用 AWS 文档 http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html 安装了外部依赖项。下面是我的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(压缩目录内容,而不是目录)。当我执行该功能时,我收到下面提到的错误。
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
请帮我调试错误。
【问题讨论】:
-
这是你的完整代码吗?由于错误,似乎在某个地方想要
import lambda_function,但没有找到。也许你想要from future import lambda_function?或者只是 pip install lambda_function 在 cmd 行上。 -
@Berci 我在 AWS 平台上运行这个 python 代码。我不能使用点子。我的代码中的任何地方都在使用 lambda_function。如果我在 AWS 控制台中复制粘贴相同的代码,它将起作用
-
查看this thread 上的最后一条评论——也许适用于你?
-
我的猜测是您的函数中的“处理程序”选项不正确。检查您的文件名为“lambda_function.py”,处理程序方法是否为“lambda_handler”
-
我花了几个小时,终于知道你必须压缩目录的内容(包括 lambda_function.py )而不是目录。
标签: python amazon-web-services aws-lambda