调用AWS Lambda并异步将响应返回到API网关 [英] Invoke AWS Lambda and return response to API Gateway asyncronously

查看:180
本文介绍了调用AWS Lambda并异步将响应返回到API网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用例是,我将拥有一个使用API​​ Gateway的AWS Lambda前端.

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

我的要求是,一旦调用Lambda,它应将200 OK响应返回给API网关,API网关会将其转发给调用方. 然后Lambda应该开始对有效负载进行实际处理.

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.

这样做的原因是API网关调用者服务期望在10秒内响应,否则将超时.所以我想在开始处理之前给出响应.

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.

这可能吗?

推荐答案

使用API​​ Gateway的"Lambda函数"集成类型,您不能使用单个Lambda函数来完成此操作-该接口是专门为同步而设计的.如果要使用Lambda函数集成类型,则解决方法是针对由网关调用的同步Lambda函数,以通过Lambda API调用第二个异步Lambda函数.

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.

但是,使用 AWS服务代理集成而不是 Lambda Function 集成,可以在没有解决方法的情况下实现异步调用.

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

如果您的API仅在后端对Lambda函数进行同步调用,则应使用Lambda Function集成类型. [...]

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

如果您的API对Lambda函数进行异步调用,则必须使用本节中描述的AWS Service Proxy集成类型.该说明也适用于同步Lambda函数调用的请求.对于异步调用,必须将X-Amz-Invocation-Type:Event标头显式添加到集成请求中.

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

这篇关于调用AWS Lambda并异步将响应返回到API网关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆