AWS Custom Authorizer无法正常工作,并且返回的请求超时

时间:2019-04-17 11:23:12

标签: node.js aws-lambda authorization

我正在使用AWS Custom Authorizer。我以https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html中的自定义授权代码为例 部署API后,我发送带有标头“允许”或“拒绝”的请求,并得到正确的响应。第二次请求时,请求超时。

我的Lambda处理程序代码为:

let authenticateHandler = require('./authenticate_handler').handler;

exports.handler = function (event, context, callback) {

console.log('event.path ====> : ', event.path);
  if(event.path) {
      console.log('API Gateway call!!!!!');
    lambdaHandler(event, context, callback);
  } else {
    console.log('Authentication call!!!!!');
    authenticateHandler(event, context, callback);
  }
};

我的lambdahandler是一个请求处理程序,当授权者授予允许请求时,将在该请求处理程序上路由该请求。

在两种情况下从lambda函数生成的日志如下:


2019-04-17T10:58:58.760Z    d1a35bcc-9336-4fe4-a5a2-55b0d86b4064    Token : allow
2019-04-17T10:58:58.761Z    d1a35bcc-9336-4fe4-a5a2-55b0d86b4064    ======>>> { principalId: 'user',
policyDocument: { Version: '2012-10-17', Statement: [ [ { Action: 'execute-api:Invoke',
Effect: 'Allow',
Resource: 'arn:aws:execute-api:us-east-1:xxxxxxxxxxxxx:xxxxxxxxxx/dev/GET/test/authorizer' } ] ] },
context: { stringKey: 'stringval', numberKey: 123, booleanKey: true } } 

---Here request ends and authorizer sends the request to API Gateway.
Route logs are:
2019-04-17T10:58:58.779Z    0ea765f9-2e93-4811-b391-f150a8e2248e    API Gateway call!!!!!
2019-04-17T10:58:58.779Z    0ea765f9-2e93-4811-b391-f150a8e2248e    Event object is: { numberKey: '123',
booleanKey: 'true',
stringKey: 'stringval',
principalId: 'user',
integrationLatency: 441 }
2019-04-17T10:58:58.799Z    0ea765f9-2e93-4811-b391-f150a8e2248e    I am in /test/authorizer allow

And at this time request ends.

现在,我已发送带有“ deny”标头的请求,并查看了该请求:

2019-04-17T11:16:55.998Z    20a0ab60-ac0d-4323-81de-9869537ad7e5    Token : deny
2019-04-17T11:16:55.998Z    20a0ab60-ac0d-4323-81de-9869537ad7e5    ======>>> { principalId: 'user',
policyDocument: { Version: '2012-10-17', Statement: [ [ { Action: 'execute-api:Invoke',
Effect: 'Deny',
Resource: 'arn:aws:execute-api:us-east-1:xxxxxxxxxxx:xxxxxx/dev/GET/test/authorizer' } ] ] },
context: { stringKey: 'stringval', numberKey: 123, booleanKey: true } } 

如果被拒绝,API网关的日志为:

(4f4d3765-6102-11e9-a7ed-39a90f976cc3) Incoming identity: **ny
(4f4d3765-6102-11e9-a7ed-39a90f976cc3) Endpoint request body after transformations:
{
    "type": "TOKEN",
    "methodArn": "arn:aws:execute-api:us-east-1:xxxxxxxxxxx:xxxxxxxxx/dev/GET/test/authorizer",
    "authorizationToken": "deny"
}
(4f4d3765-6102-11e9-a7ed-39a90f976cc3) Sending request to https://lambda.us-east-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-east-1:xxxxxxxxxxxx:function:AuthrorizerTest/invocations
(4f4d3765-6102-11e9-a7ed-39a90f976cc3) Execution failed due to a timeout error
(4f4d3765-6102-11e9-a7ed-39a90f976cc3) Execution failed due to configuration error: Authorizer error

我无法确定此授权者错误是什么。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我在以下链接中找到了答案:

Why does AWS Lambda function always time out?

context.callbackWaitsForEmptyEventLoop = false;

为我工作。