AWS API Gateway:由于配置错误导致执行失败:响应中的JSON无效

Ale*_*ngu 6 lambda amazon-web-services node.js aws-api-gateway

我有一个API网关设置与自定义授权器调用Lambda函数.出于测试目的,我从这里复制了示例:http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-网关代理集成-λ-功能的NodeJS

我得到了与文档中相同的答案,但是当我测试授权程序时,我得到了这个堆栈跟踪:

    Endpoint request body after transformations: {"type":"TOKEN","authorizationToken":"test","methodArn":"arn:aws:execute-api:ap-southeast-2:893445519708:uyue0zqh15/null/GET/"}
    Authorizer result body before parsing: {"statusCode":200,"headers":{"x-custom-header":"my custom header value"},"body":"{\"message\":\"Hello World!\",\"input\":{\"type\":\"TOKEN\",\"authorizationToken\":\"test\",\"methodArn\":\"arn:aws:execute-api:ap-southeast-2:893445519708:uyue0zqh15/null/GET/\"}}"}
    Execution failed due to configuration error: Invalid JSON in response: {"statusCode":200,"headers":{"x-custom-header":"my custom header value"},"body":"{\"message\":\"Hello World!\",\"input\":{\"type\":\"TOKEN\",\"authorizationToken\":\"test\",\"methodArn\":\"arn:aws:execute-api:ap-southeast-2:893445519708:uyue0zqh15/null/GET/\"}}"}
    AuthorizerConfigurationException
Run Code Online (Sandbox Code Playgroud)

为什么授权人不喜欢JSON响应?

Jac*_*AWS 13

授权者响应格式与集成代理响应格式不同.我可以看到这令人困惑!

自定义授权程序输出应符合以下格式:

{
  "principalId": "yyyyyyyy", // The principal user identification associated with the token sent by the client.
  "policyDocument": {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Action": "execute-api:Invoke",
        "Effect": "Allow|Deny",
        "Resource": "arn:aws:execute-api:<regionId>:<accountId>:<appId>/<stage>/<httpVerb>/[<resource>/<httpVerb>/[...]]"
      }
    ]
  },
  "context": {
    "key": "value",
    "numKey": 1,
    "boolKey": true
  }
}
Run Code Online (Sandbox Code Playgroud)

principalIdpolicyDocument是必需的,context是可选的.

更新:

policyDocument不是用户定义的,它是相同的语法上的API网关行动和资源工作的规则IAM政策http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-使用-IAM-政策对调用-api.html

在Lambda Web控制台中,python和node中的授权者也有很好的蓝图,这里有一个Java蓝图:https://github.com/awslabs/aws-apigateway-lambda-authorizer-blueprints


Rob*_*gan 7

我只是遇到了同样的错误,但就我而言,问题context是太复杂了-显然它不能包含数组或对象值键。

此处记录了以下内容:https : //docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html

请注意,您无法将JSON对象或数组设置为上下文映射中任何键的有效值。

(我试图将一个解码的JWT设置为上下文,它具有一个数组值的roles键。我现在改为发送编码的JWT)