Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke AWS Lambda and return response to API Gateway asyncronously

My use case is such that I'll have an AWS Lambda front ended with API Gateway.

My requirement is that once the Lambda is invoked it should return a 200 OK response back to API Gateway which get forwards this to the caller. And then the Lambda should start its actual processing of the payload.

The reason for this is that the API Gateway caller service expects a response within 10 seconds else it times out. So I want to give the response before I start with the processing.

Is this possible?

like image 272
Arafat Nalkhande Avatar asked Dec 12 '16 13:12

Arafat Nalkhande


1 Answers

With API Gateway's "Lambda Function" integration type, you can't do this with a single Lambda function -- that interface is specifically designed to be synchronous. The workaround, if you want to use the Lambda Function integration type is for the synchronous Lambda function, invoked by the gateway, to invoke a second, asynchronous, Lambda function through the Lambda API.

However, asynchronous invocations are possible without the workaround, using an AWS Service Proxy integration instead of a Lambda Function integration.

If your API makes only synchronous calls to Lambda functions in the back end, you should use the Lambda Function integration type. [...]

If your API makes asynchronous calls to Lambda functions, you must use the AWS Service Proxy integration type described in this section. The instructions apply to requests for synchronous Lambda function invocations as well. For the asynchronous invocation, you must explicitly add the X-Amz-Invocation-Type:Event header to the integration request.

http://docs.aws.amazon.com/apigateway/latest/developerguide/integrating-api-with-aws-services-lambda.html

like image 156
Michael - sqlbot Avatar answered Nov 10 '22 00:11

Michael - sqlbot