bebbi bebbi -4 years ago 214
Node.js Question

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.

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?

Answer Source

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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download