且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

调用AWS Lambda并异步将响应返回到API网关

更新时间:2023-01-18 12:32:13

使用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