aws lambda execution after callback guaranteed?

亡梦爱人 提交于 2020-01-01 01:56:21

问题


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)
}

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"

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?


回答1:


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

  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.



回答2:


Option 5. Let your lambda function queue a message to SQS and poll the queue from another lambda or ec2 or wherer you want to do the heavy lifting.



来源:https://stackoverflow.com/questions/42983003/aws-lambda-execution-after-callback-guaranteed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!