为什么API网关(测试)不等待异步lambda



我正在使用Node.js(14.x(在AWS上编写异步lambda。此lambda由API网关调用(REST API,POST方法,启用CORS(。

为了允许API网关调用异步lambda,我在其集成请求中添加了以下HTTP头:Name=X-Amz-Invocation-Type和Mapped from(value(='Event',如这里所指定的。

当我运行测试时,我得到以下输出:

Execution log for request BLAH
Fri Mar 26 07:30:34 UTC 2021 : Starting execution for request: BLAH
Fri Mar 26 07:30:34 UTC 2021 : HTTP Method: POST, Resource Path: BLAH
Fri Mar 26 07:30:34 UTC 2021 : Method request path: {}
Fri Mar 26 07:30:34 UTC 2021 : Method request query string: {}
Fri Mar 26 07:30:34 UTC 2021 : Method request headers: {}
Fri Mar 26 07:30:34 UTC 2021 : Method request body before transformations: {
"id": "BLAH",
"recaptcha": "BLAH"
}
Fri Mar 26 07:30:34 UTC 2021 : Request validation succeeded for content type application/json
Fri Mar 26 07:30:34 UTC 2021 : Endpoint request URI: https://lambda.BLAH.amazonaws.com/2015-03-31/functions/arn:aws:lambda:BLAH:function:BLAH/invocations
Fri Mar 26 07:30:34 UTC 2021 : Endpoint request headers: {X-Amz-Date=BLAH, x-amzn-apigateway-api-id=BLAH, Accept=application/json, User-Agent=AmazonAPIGateway_BLAH, Host=lambda.BLAH.amazonaws.com, X-Amz-Content-Sha256=BLAH, X-Amzn-Trace-Id=Root=BLAH, x-amzn-lambda-integration-tag=BLAH, Authorization=*****BLAH*****, X-Amz-Source-Arn=arn:aws:execute-api:BLAH/test-invoke-stage/POST/BLAH, X-Amz-Invocation-Type=Event, X-Amz-Security-Token=BLAH [TRUNCATED]
Fri Mar 26 07:30:34 UTC 2021 : Endpoint request body after transformations: {
"id": "BLAH",
"recaptcha": "BLAH"
}
Fri Mar 26 07:30:34 UTC 2021 : Sending request to https://lambda.BLAH.amazonaws.com/2015-03-31/functions/arn:aws:lambda:BLAH:function:BLAH/invocations
Fri Mar 26 07:30:34 UTC 2021 : Received response. Status: 202, Integration latency: 34 ms
Fri Mar 26 07:30:34 UTC 2021 : Endpoint response headers: {Date=Fri, 26 Mar 2021 07:30:34 GMT, Content-Length=0, Connection=keep-alive, x-amzn-RequestId=BLAH, x-amzn-Remapped-Content-Length=0, X-Amzn-Trace-Id=root=BLAH;sampled=0}
Fri Mar 26 07:30:34 UTC 2021 : Endpoint response body before transformations: 
Fri Mar 26 07:30:34 UTC 2021 : Method response body after transformations: 
Fri Mar 26 07:30:34 UTC 2021 : Method response headers: {X-Amzn-Trace-Id=Root=BLAH;Sampled=0, Access-Control-Allow-Origin=*, Content-Type=application/json}
Fri Mar 26 07:30:34 UTC 2021 : Successfully completed execution

注意,响应给出Status: 202,并且没有响应主体无论我在lambda中放入了什么代码,似乎都是这样以下是我尝试过的几个基本示例:

// Load the AWS SDK for Node.js and set the region.
var AWS = require('aws-sdk');
AWS.config.update({region: 'BLAH'});
// Main function, responds to AWS API Gateway.
exports.handler = async function (event, context) {
var response = {
httpStatus : 200,
message : "Success"
};
return JSON.stringify(response);
};

// Load the AWS SDK for Node.js and set the region.
var AWS = require('aws-sdk');
AWS.config.update({region: 'BLAH'});
// Main function, responds to AWS API Gateway.
exports.handler = async function (event, context) {
var promise = new Promise(function(resolve, reject) {
var response = {
httpStatus : 200,
message : "Success"
};
resolve(JSON.stringify(response));
});
return promise;
};

我确保部署API和Lambda如何获得非202状态(即让API网关等待异步lambda(

这是按预期工作的。AWS文档中关于";异步调用";说:

异步调用函数时,不需要等待函数代码的响应。

如果您想要一个有结果的响应,异步调用将不起作用。

最新更新