保证回调后执行AWS Lambda吗? [英] aws lambda execution after callback guaranteed?

查看:80
本文介绍了保证回调后执行AWS Lambda吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的通过API GW调用的node4 lambda函数进行了一系列缓慢的API调用. 为了不让用户等到一切完成,我打算让我的代码看起来像这样:

My node4 lambda function called via API GW makes a sequence of slow API calls. In order to not let users wait until everything completes, I'm planning to have my code look like this:

function(event, context, callback) {
  ...
  // Return users API GW call now
  callback(null, data);
  // Do the heavy lifting afterwards.
  longApiCall().then(otherLongApiCalls)
}

但是现在我阅读了 AWS文档: 回调将一直等到Node.js运行时事件循环为空,然后再冻结进程并将结果返回给调用方"

But now I read in the AWS docs: "the callback will wait until the Node.js runtime event loop is empty before freezing the process and returning the results to the caller"

这是否意味着API GW在longApiCalls完成之前或之后返回响应数据?

Does that mean the API GW returns the response data before or after the longApiCalls complete?

如果过了,有什么方法可以建议在一切完成之前提早返回"?

If after, is there a suggested way for how to "return early" before everything is finished?

推荐答案

在当前配置中,API网关将等待Lambda函数执行完毕,然后再发送响应.您的选择是:

In your current configuration API Gateway will wait until the Lambda function has finished executing before sending a response. Your options are:

  1. 将API Gateway端点的集成类型更改为 AWS服务,并让API Gateway异步调用Lambda函数. 此处.
  2. 让API网关调用的Lambda函数什么都做,只能异步调用另一个Lambda函数,然后返回.
  3. 具有API网关或API网关调用的Lambda函数可将消息发送到SNS主题.然后让SNS主题触发一个Lambda函数来处理长API调用.这会使您的微服务稍微松耦合.
  4. 具有API网关或API网关调用的Lambda函数会触发 AWS步骤功能配置为通过一个或多个Lambda函数处理长API调用.如果长时间的API调用会冒一个Lambda函数的执行时间限制为5分钟的风险,我会建议采用这种方法.
  1. Change the API Gateway endpoint's integration type to AWS Service and have API Gateway invoke the Lambda function asynchronously. This is documented here.
  2. Have the Lambda function that API Gateway invokes do nothing but invoke another Lambda function asynchronously and then return.
  3. Have API Gateway, or a Lambda function called by API Gateway, send a message to an SNS topic. Then have the SNS topic trigger a Lambda function that handles the long API calls. This would decouple your microservices a bit.
  4. Have API Gateway, or a Lambda function called by API Gateway, trigger an AWS Step Function that is configured to handle the long API calls via one or multiple Lambda functions. I would suggest this approach if the long API calls run the risk of running over a single Lambda function's execution time limit of 5 minutes.

这篇关于保证回调后执行AWS Lambda吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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