上传到 S3 时 Lambda 函数超时 [英] Lambda Function timing out when uploading to S3

查看:30
本文介绍了上传到 S3 时 Lambda 函数超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 python 编写的 lambda 函数,它在 mongodb 中执行一些操作,然后它应该将该函数的 tmp 文件夹中的图片上传到 s3.该函数在上传步骤中保持超时.

I have a lambda function written in python that does some operations in mongodb then it is supposed to upload a picture from the tmp folder of the function onto s3. The function keeps timing out on the upload step.

我将超时时间提高到 2 分钟,并且该函数具有 S3 和 vpc 权限.该函数只是超时.任何人都知道出了什么问题?

I raised the timeout to 2 mins and the function has S3 and vpc permissions. The function simply times out. Anyone has any ideas of what is going wrong??

样本输入

#picturename should be created by the app. a name unique for the dish
{
    "UserId": "56dc63fc1769d032d4d78e2e",  
    "DishId": "56dcc2781769d032d4d78e2f",
    "PictureName" : "katsu-001.png",
    "Data": "base64 image just the bits ignore data:image/jpeg;base64, if you have it"
}

功能

def addPicture(event,context):  

from __future__ import print_function
import pymongo
from pymongo import MongoClient
import bson.code
from bson.objectid import ObjectId
import datetime
import json    
import boto3
import sys
import uuid
from base64 import decodestring

print ('Writing file to disk')
with open('/tmp/' + pictureName,"wb") as f:
    f.write(decodestring(event["Data"]))
    print ('File written to /tmp/' + pictureName)

s3_client = boto3.client('s3')
print ('Starting S3 upload')
bucket = "foundue-dev-filestore"
upload_path = 'pictures/dish/' + dishId.__str__() + '/' + pictureName
print ('Uploading /tmp/' + pictureName + ' ' + bucket + ' ' + upload_path) 
s3_client.upload_file('/tmp/' + pictureName,bucket, upload_path)   
print ('Upload Complete')  
#pics[pictureName] = upload_path
#dish["Pictures"] = pics
#dish["UpdatedOn"] = datetime.datetime.utcnow()
#db.dishes.replace_one({"_id": dishId}, dish)
return

附加到 lambda 的策略

Policies attached to lambda

oneClick_lambda_basic_vpc_execution_1457284829450
oneClick_lambda_s3_exec_role_1457392283800

输出

Loading function 
START RequestId: ed91c290-e582-11e5-95d6-ed4fc6a3321b Version: $LATEST 
Writing file to disk 
File written to /tmp/katsu-002png 
Starting S3 upload 
Uploading /tmp/katsu-002png foundue-dev-filestore pictures/dish/56dcc2781769d032d4d78e2f/katsu-002png 
END RequestId: ed91c290-e582-11e5-95d6-ed4fc6a3321b 
REPORT RequestId: ed91c290-e582-11e5-95d6-ed4fc6a3321b  Duration: 121003.49 ms  Billed Duration: 121000 ms Memory Size: 128 MB  Max Memory Used: 22 MB
2016-03-08T23:12:21.437Z ed91c290-e582-11e5-95d6-ed4fc6a3321b Task timed out after 121.00 seconds

推荐答案

真正的问题是 lambda 使用的是 VPC,但 VPC 没有访问 s3 的端点.所以确保你有那个.

The real problem was that the lambda was using a VPC, but the VPC had no endpoints to access s3. So make sure you have that.

(并允许 lambda 有足够的权限来调用 s3)

(and to allow lambda enough permissions to call s3)

现在它可以在不到一秒的时间内完成这些功能.

Now it does the functions in less than a second.

这篇关于上传到 S3 时 Lambda 函数超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆