Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handing back a response to API Gateway from Lambda

I'm using API Gateway-to-Lambda for a few micro-services but in at least one case the service will take 20-30 seconds to complete so in cases like this I'd like to pass back an immediate response to the client, something like:

 status: 200
 message: {
      progressId: 1234
 }

and then allow the Lambda Function to continue on (and periodically updating the "processId" somewhere that is accessible to a client. The problem is that if you call context.succeed(), context.fail(), or context.done() that apparently stops the lambda function from further execution and yet it's the only way I know to flush the stdout buffer back to the API Gateway.

This has led me to a second approach which I haven't yet try to tackle (and for simplicity sake would love to avoid) which involves API Gateway calling a "Responder" Lambda function that then asynchronously fires off the Microservice and then immediately responds to the API Gateway.

I've tried to illustrate these two options in sketch format below. I'd love to hear how anyone's been able to solve this problem.

two approaches

like image 478
ken Avatar asked Nov 22 '15 22:11

ken


People also ask

Can we call API gateway from Lambda?

A Lambda integration maps a path and HTTP method combination to a Lambda function. You can configure API Gateway to pass the body of the HTTP request as-is (custom integration), or to encapsulate the request body in a document that includes all of the request information including headers, resource, path, and method.

How do you handle Lambda errors in API gateway?

Make sure that you also set up the corresponding error code ( 400 ) on the method response. Otherwise, API Gateway throws an invalid configuration error response at runtime. At runtime, API Gateway matches the Lambda error's errorMessage against the pattern of the regular expression on the selectionPattern property.


1 Answers

Currently API Gateway requires that the AWS Lambda integration is synchronous. If you desire asynchronous invocation of your Lambda function, you have 2 options:

  1. Invoking the Lambda asynchrously, either with an AWS integration calling InvokeAsync on Lambda, or using an intermediate service such as SNS or Kinesis to trigger the Lambda function.

  2. You're #2 diagram, using a synchronous Lambda invoke to initiate the asynchronous invoke.

like image 182
Bob Kinney Avatar answered Sep 23 '22 17:09

Bob Kinney