Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aws lambda execution after callback guaranteed?

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?

like image 746
bebbi Avatar asked Mar 23 '17 17:03

bebbi


2 Answers

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.
like image 71
Mark B Avatar answered Sep 30 '22 21:09

Mark B


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.

like image 35
Andi Avatar answered Sep 30 '22 21:09

Andi