Invoke AWS Lambda and return response to API Gateway asyncronously

时光怂恿深爱的人放手 提交于 2019-12-19 03:44:14

问题


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?


回答1:


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




回答2:


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. The second Lambda function will then take as long as long as it needs to complete.




回答3:


Check the answer here on how to set up an Async Invoke to the Lambda function. This will return 200 immediately to the client, but the Lambda will process on it's own asynchronously.

https://stackoverflow.com/a/40982649/5679071



来源:https://stackoverflow.com/questions/41102337/invoke-aws-lambda-and-return-response-to-api-gateway-asyncronously

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!