Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Respond then continue working with AWS Lambda/API Gateway?

Tags:

I currently have a Web hook that's calling AWS API Gateway -> AWS Lambda function proxy. I'd like to make the web hook more responsive and return an early reply while continuing processing in the Lambda.

I went ahead and did this early reply from the Lambda (Node v6.10) but it didn't appear to have improved responsiveness. Is API Gateway somehow waiting for the Lambda to finish executing despite having the response from the callback already?

The other idea is to post an SNS notification from Lambda and have a second Lambda listen and continue processing but would rather avoid that complication if there's a simpler way.

like image 576
ss2k Avatar asked May 27 '17 17:05

ss2k


People also ask

Can AWS Lambda run continuously?

Lambda has a hard timeout of 15 minutes. Therefore it cannot run continuously.

How do I return a response from Lambda to API gateway?

Yes, simply create two Lambda functions. The first Lambda function will be called by the API Gateway and will simply invoke the second Lambda function and then immediately return successfully so that the API Gateway can respond with an HTTP 200 to the client.

Can Lambda continue after returning response?

No, this isn't possible.


1 Answers

API Gateway currently only supports synchronous invocation (aka InvocationType: RequestResponse) of Lambda functions, so yes, it is waiting for the full response from the Lambda.

To support your use case, you could use SNS or an another intermediary AWS service like Kinesis, SQS, etc. But you could also do it with Lambda alone. Have the first Lambda function trigger a second Lambda function asynchronously with InvocationType: 'Event', this will achieve the effect you desire.

See this post for more details: https://stackoverflow.com/a/31745774/5705481

like image 128
Lorenzo de Lara Avatar answered Nov 09 '22 22:11

Lorenzo de Lara