Doing aws lambda after guaranteed return?

The node4 lambda function, called through the GW API, makes a sequence of slow API calls. So that users do not wait for the completion of everything, I plan to make 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 AWS docs : "the callback will wait until the Node.js event loop is empty before freezing the process and returning the results to the caller"

Does this mean that the GW API returns response data before or after the completion of longApiCalls?

If after, is there a way to “get back earlier” before it's all over?

+4
source share
2 answers

In your current configuration, the Gateway API will wait until the Lambda function completes before sending a response. Your options:

  • Change the type of integration of the API gateway endpoint to AWS and enable the API API gateway asynchronously. This is described here .
  • The Lambda function called by the Gateway API does nothing except asynchronously call another Lambda function, and then returns.
  • Ask the API gateway or Lambda function called by the API gateway to send a message to the SNS topic. Then, in the SNS topic, the Lambda function is launched, which processes long API calls. This will slightly cancel your microservices.
  • API Gateway Lambda, API Gateway, AWS Step, API -. , API Lambda 5 .
+9

5. - SQS ec2 .

0

Source: https://habr.com/ru/post/1673035/


All Articles