bebbi
bebbi

Reputation: 2539

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?

Upvotes: 16

Views: 7673

Answers (2)

Andi
Andi

Reputation: 414

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.

Upvotes: 2

Mark B
Mark B

Reputation: 200562

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.

Upvotes: 26

Related Questions