AWS Lambda runs in the background even after sending a response to API Gateway

I have looked all over the net but did not find a solution on how to make this functionality successful. Help Wanted.

My requirement: I want this if I run an aws lambda function written in node.js and use the aws-serverless-express module, should quickly send a response back to the API gateway, but still shouldn't exit and still run in the backend and we could see the cloud observation logs. It must be asynchronous.

Snippet of code:

    app.get('/check', function(req, res){
     method.invoke(req)
     res.status(200).send('success')
   })

      

I did and tested like this, but the lambda function stops and returns a response to the api gateway, it doesn't even run the method.invoke () function in the backend.

Please correct me if there is anything I understand or do wrong. I checked from this link: Call AWS Lambda and return response to API Gateway asynchronously

This is the only way to solve this problem. Creating two lambda functions.

+9


source to share


3 answers


This can be done using AWS Lambda Step Functions connected to the API Gateway that have branches running in parallel with two lambda functions, where one returns a response to the API gateway and the other executes asynchronously.



+3


source


Besides the Step functions, you can simply call another Lambda function using the built-in SDK in Lambda.



I'm not an Express or NodeJS expert, but I also think there must be a way to send an HTTP response and continue executing the code.

+1


source


Step function seems to be the best solution here. See @ Ashan's answer. Alternatively, you can use the new invoke method in lambda nodejs sdk. Please note that invokeAsync is now deprecated. You can set InvocationType to Event. See example below. which is taken from here

var params = {
  ClientContext: "MyApp", 
  FunctionName: "MyFunction", 
  InvocationType: "Event", 
  LogType: "Tail", 
  Payload: <Binary String>, 
  Qualifier: "1"
 };
 lambda.invoke(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
   /*
   data = {
    FunctionError: "", 
    LogResult: "", 
    Payload: <Binary String>, 
    StatusCode: 123
   }
   */
 }); 

      

One use case: the first function will return an immediate response, and it will run another lambda function that will perform tasks and may eventually call a webhook.

0


source







All Articles