具有Lambda代理集成的AWS API Gateway失败

时间:2019-01-29 23:04:46

标签: json scala amazon-web-services aws-lambda aws-api-gateway

我的问题很简单,真的。

我编写了一个Scala处理程序类,该类返回包含以下JSON的String:

{
   "isBase64Encoded":false,
   "statusCode":404,
   "headers":{
      "Content-Type":"text/html"
   },
   "body":"<p>The requested resource could not be found.</p>"
}

我的课有以下签名:

object Handler extends RequestHandler[APIGatewayProxyRequestEvent, String] {

  override def handleRequest(input: APIGatewayProxyRequestEvent, context: Context): String = ???

}

我正在使用AWS Java SDK中的RequestHandlerAPIGatewayProxyRequestEvent

但是在调用它时出现错误Execution failed due to configuration error: Malformed Lambda proxy response

这是完整的日志:

Execution log for request 99234b33-2419-11e9-8156-d18557467ee6
Tue Jan 29 22:59:57 UTC 2019 : Starting execution for request: 99234b33-2419-11e9-8156-d18557467ee6
Tue Jan 29 22:59:57 UTC 2019 : HTTP Method: GET, Resource Path: /foo
Tue Jan 29 22:59:57 UTC 2019 : Method request path: {proxy=foo}
Tue Jan 29 22:59:57 UTC 2019 : Method request query string: {}
Tue Jan 29 22:59:57 UTC 2019 : Method request headers: {}
Tue Jan 29 22:59:57 UTC 2019 : Method request body before transformations: 
Tue Jan 29 22:59:57 UTC 2019 : Endpoint request URI: https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:794147591978:function:xxxx/invocations
Tue Jan 29 22:59:57 UTC 2019 : Endpoint request headers: {x-amzn-lambda-integration-tag=99234b33-2419-11e9-8156-d18557467ee6, Authorization=***************************************************************************************************************************************************************************************************************************************************************************************************************************e4cddd, X-Amz-Date=20190129T225957Z, x-amzn-apigateway-api-id=0lypbq3och, X-Amz-Source-Arn=arn:aws:execute-api:eu-central-1:794147591978:0lypbq3och/test-invoke-stage/GET/{proxy+}, Accept=application/json, User-Agent=AmazonAPIGateway_0lypbq3och, X-Amz-Security-Token=FQoGZXIvYXdzECAaDNe+xBH2UeEIx4SHoyK9A24kTKoADNrqj2oEsP7wwqlNw/Z3DHCHLqagRvBVke5R7MUg4G4cTFG0KQia0x3x4CqYIjTtQ6oFp2iWuTV8LOuMQz6r4T75Y5wrvl3c2qfkUd7m+Oj2B5Q0TXKtI/DBa0I4ChqAVgamMuPTVbZga0u9gG4d+v7sEzeOrMDrp1AsiBYQMG+S9MbTC/2+UuFe2X7elWnAhSXAZ/LA7w/dQu8+UvPPLN2xVl0W7d/9Nsq0BM1B5OBgJaj5gO9r8zh1Yto96100F3JaIdjLk+btcyy53OnL+SQWaS2isF/2Gl [TRUNCATED]
Tue Jan 29 22:59:57 UTC 2019 : Endpoint request body after transformations: {"resource":"/{proxy+}","path":"/foo","httpMethod":"GET","headers":null,"multiValueHeaders":null,"queryStringParameters":null,"multiValueQueryStringParameters":null,"pathParameters":{"proxy":"foo"},"stageVariables":null,"requestContext":{"path":"/{proxy+}","accountId":"794147591978","resourceId":"m044wv","stage":"test-invoke-stage","domainPrefix":"testPrefix","requestId":"99234b33-2419-11e9-8156-d18557467ee6","identity":{"cognitoIdentityPoolId":null,"cognitoIdentityId":null,"apiKey":"test-invoke-api-key","cognitoAuthenticationType":null,"userArn":"arn:aws:iam::794147591978:root","apiKeyId":"test-invoke-api-key-id","userAgent":"aws-internal/3 aws-sdk-java/1.11.481 Linux/4.9.137-0.1.ac.218.74.329.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.192-b12 java/1.8.0_192","accountId":"794147591978","caller":"794147591978","sourceIp":"test-invoke-source-ip","accessKey":"ASIA3RZXCS4VNQOJVZON","cognitoAuthenticationProvider":null,"user":"794147591978"},"domainName":"testPrefix.tes [TRUNCATED]
Tue Jan 29 22:59:57 UTC 2019 : Sending request to https://lambda.eu-central-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-central-1:794147591978:function:xxx/invocations
Tue Jan 29 22:59:57 UTC 2019 : Received response. Integration latency: 36 ms
Tue Jan 29 22:59:57 UTC 2019 : Endpoint response body before transformations: "{\"isBase64Encoded\":false,\"statusCode\":404,\"headers\":{\"Content-Type\":\"text/html\"},\"body\":\"<p>The requested resource could not be found.</p>\"}"
Tue Jan 29 22:59:57 UTC 2019 : Endpoint response headers: {Date=Tue, 29 Jan 2019 22:59:57 GMT, Content-Type=application/json, Content-Length=156, Connection=keep-alive, x-amzn-RequestId=bd41f477-92ae-4068-a062-16a149cf7748, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5c50daed-8291ac2e0f2b8a251dbf1865;sampled=0}
Tue Jan 29 22:59:57 UTC 2019 : Execution failed due to configuration error: Malformed Lambda proxy response
Tue Jan 29 22:59:57 UTC 2019 : Method completed with status: 502

除了转义,日志中包含的JSON似乎完全符合我的期望,并且也与documentation中的文档相匹配。

那我为什么会收到这个错误?

1 个答案:

答案 0 :(得分:1)

尝试对主体值进行JSON.stringify()

{
   "isBase64Encoded":false,
   "statusCode":404,
   "headers":{
      "Content-Type":"text/html"
   },
   "body":JSON.stringify("<p>The requested resource could not be found.</p>")
}

我知道这似乎很愚蠢,但是输出有所不同:

console.log("<p>The requested resource could not be found.</p>");
// "<p>The requested resource could not be found.</p>"

console.log(JSON.stringify("<p>The requested resource could not be found.</p>"));
// "'<p>The requested resource could not be found.</p>'"