[英]How can I return a response to an AWS lambda function for a longer running process before lambda times out?
I have a s3 bucket trigger set up to call a lambda function when a new file is saved to the bucket.我有一个 s3 桶触发器设置为在将新文件保存到桶中时调用 lambda function。 That lambda function then parses the event data and sends it as a post request to an api running on on ec2 instance.
lambda function 然后解析事件数据并将其作为发布请求发送到在 ec2 实例上运行的 api。 That post request, when received starts a file conversion pipeline process that can take a minute or so to run.
该发布请求在收到后会启动一个文件转换管道过程,该过程可能需要一分钟左右的时间才能运行。 The lambda seems to time-out every 3 seconds, so it keeps trying and then saying 'timed out' because it never receives a response from the api call.
lambda 似乎每 3 秒超时一次,因此它一直在尝试然后说“超时”,因为它从未收到来自 api 呼叫的响应。 The actual conversion process kicks off and completes just fine though so thats not an issue but I didn't think this was good practice.
实际的转换过程开始并完成得很好,所以这不是问题,但我认为这不是好的做法。 Is there a fix to this or some other way I should be configuring this?
是否有解决此问题或我应该配置此问题的其他方式?
My lambda function is我的 lambda function 是
import json
import requests
def lambda_handler(event, context):
# Get the bucket name and object key from the event
bucket_name = event['Records'][0]['s3']['bucket']['name']
object_key = event['Records'][0]['s3']['object']['key']
# Encode the data as JSON
data = {
'bucket_name': bucket_name,
'object_key': object_key
}
json_data = json.dumps(data)
# Make the POST request
url = "http://<domain>/api/new/file"
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=json_data)
# Return the response status code
print(f'response code: {response}')
# return response.status_code
print(data)
return data
The api on the ec2 instance is: ec2 实例上的 api 是:
from flask import Flask, request
app = Flask(__name__)
@app.route('/api/new/file', methods=['POST'])
def handle_request():
content_type = request.headers.get('Content-Type')
if (content_type == 'application/json'):
data = request.json
file_name = os.path.basename(data['object_key'])
try:
<my_long_function>(file_name)
except Exception as e:
print(e)
return "False"
return "True"
else:
return 'Content-Type not supported!'
That's because lambda by default runs for 3 secs.这是因为 lambda 默认运行 3 秒。 All you have to do is increase the timeout value, which can be up to 15 mins == 900 secs Lambda > Configuration > General Configuration > Timeout
您所要做的就是增加超时值,最长可达 15 分钟 == 900 秒 Lambda > Configuration > General Configuration > Timeout
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.