如果响应正文未字符串化,AWS API 网关将返回"internal server error"



我有以下处理程序代码:

export default function handler(lambda) {
return function (event, context) {
return Promise.resolve()
// Run the Lambda
.then(() => lambda(event, context))
// On success
.then((responseBody) => [200, responseBody])
// On failure
.catch((e) => {
return [500, { error: e.message }];
})
// Return HTTP response
.then(([statusCode, body]) => ({
statusCode,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": true,
},
body: JSON.stringify(body),
}))
// On failure
.catch((e) => {
return [500, { error: e.message }];
});
};
}

每当我创建一个新的Lambda函数时,我都会导入此代码并在此基础上编写代码,如下所示:

import handler from "../libs/handler-lib";
export const main = handler(async (event, context) => {
"my code"
}

这很有魅力,但问题是,我需要访问aws stepfunctions中的响应体,以确定下一步执行哪个函数,所以我更改了body: JSON.stringify(body),body: body,

然后,每个函数只能在lambda函数控制台中工作,但如果lambda是由aws-api网关触发的,则会失败。

我想我遗漏了一些显而易见的东西,但我说不出来。提前谢谢。

这是完整的api网关日志:

Execution log for request 8677ed2f-5e9e-4913-8fe9-f1d2f9b2ac12
Sun Nov 15 16:00:51 UTC 2020 : Starting execution for request: 8677ed2f-5e9e-4913-8fe9-f1d2f9b2ac12
Sun Nov 15 16:00:51 UTC 2020 : HTTP Method: GET, Resource Path: /latest-patch-json-data
Sun Nov 15 16:00:51 UTC 2020 : Method request path: {}
Sun Nov 15 16:00:51 UTC 2020 : Method request query string: {}
Sun Nov 15 16:00:51 UTC 2020 : Method request headers: {}
Sun Nov 15 16:00:51 UTC 2020 : Method request body before transformations: 
Sun Nov 15 16:00:51 UTC 2020 : Endpoint request URI: https://lambda.ap-northeast-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:xxxxxxxx:function:tactics-api-dev-getLatestPatchJsonData/invocations
Sun Nov 15 16:00:51 UTC 2020 : Endpoint request headers: {x-amzn-lambda-integration-tag=8677ed2f-5e9e-4913-xxxxxxxx, Authorization=*****************************************************************************************************************************************************************************************************************************************************************************************************************************7603ee, X-Amz-Date=20201115T160051Z, x-amzn-apigateway-api-id=f8brp6pbai, X-Amz-Source-Arn=arn:aws:execute-api:ap-northeast-1:296545255814:f8brp6pbai/test-invoke-stage/GET/latest-patch-json-data, Accept=application/json, User-Agent=AmazonAPIGateway_f8brp6pbai, X-Amz-Security-Token=IQoJb3JpZ2luX2VjEIf//////////wEaDmFwLW5vcnRoZWFzdC0xIkgwRgIhALgraV2CJa+NzHo/sSqH4MgbwVveHpse1s9DNkN5B/5JAiEAnghcO0tmUqhMhB+PxZ/lWbApusJKxJSM1zoIAtqFHycqxwMI8P//////////ARABGgw5NjkyMzY4NTQ2MjYiDI4NEh+wCOuUEqvN/iqbA+UIa11YzWxKIHWZ5hBsK7z/zSPD7Jz9S/3SH3L1M/IpjgbFC3g/y7B+tDj1lZ5nuzXbqlpg092hCt5JR1otT7w45xu0yQdZwcVPT9LV [TRUNCATED]
Sun Nov 15 16:00:51 UTC 2020 : Endpoint request body after transformations: {"resource":"/latest-patch-json-data","path":"/latest-patch-json-data","httpMethod":"GET","headers":null,"multiValueHeaders":null,"queryStringParameters":null,"multiValueQueryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"resourceId":"b31s1i","resourcePath":"/latest-patch-json-data","httpMethod":"GET","extendedRequestId":"WDmMCE8mtjMFqQg=","requestTime":"15/Nov/2020:16:00:51 +0000","path":"/latest-patch-json-data","accountId":"296545255814","protocol":"HTTP/1.1","stage":"test-invoke-stage","domainPrefix":"testPrefix","requestTimeEpoch":1605456051327,"requestId":"8677ed2f-5e9e-4913-8fe9-f1d2f9b2ac12","identity":{"cognitoIdentityPoolId":null,"cognitoIdentityId":null,"apiKey":"test-invoke-api-key","principalOrgId":null,"cognitoAuthenticationType":null,"userArn":"arn:aws:iam::296545255814:root","apiKeyId":"test-invoke-api-key-id","userAgent":"aws-internal/3 aws-sdk-java/1.11.864 Linux/4.9.217-0.3.ac.206.84.332.metal1.x86_64 OpenJD [TRUNCATED]
Sun Nov 15 16:00:51 UTC 2020 : Sending request to https://lambda.ap-northeast-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:xxxxxxxxxxxx:function:tactics-api-dev-getLatestPatchJsonData/invocations
Sun Nov 15 16:00:52 UTC 2020 : Received response. Status: 200, Integration latency: 805 ms
Sun Nov 15 16:00:52 UTC 2020 : Endpoint response headers: {Date=Sun, 15 Nov 2020 16:00:52 GMT, Content-Type=application/json, Content-Length=284, Connection=keep-alive, x-amzn-RequestId=753115e8-0264-4517-9a2a-f67f045b7011, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5fb150b3-b7a44f36147dc1a10b321a85;sampled=0}
Sun Nov 15 16:00:52 UTC 2020 : Endpoint response body before transformations: {"statusCode":200,"headers":{"Access-Control-Allow-Origin":"*","Access-Control-Allow-Credentials":true},"body":{"latestPatchVersion":"10.23","message":"PDH is up-to-date, PDH record: cc01f980-273a-11eb-b79f-676006f2be2a lastCheck time: 2020-11-15T16:00:51.996Z","type":"skip update"}}
Sun Nov 15 16:00:52 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response
Sun Nov 15 16:00:52 UTC 2020 : Method completed with status: 502

对于lambda代理,lambda返回对象必须是以下形状:

{
"isBase64Encoded": true|false,
"statusCode": httpStatusCode,
"headers": { "headerName": "headerValue", ... },
"body": "..."
}

这意味着,主体需要是一个字符串,而不是一个对象。

最新更新