API Gateway new (beta) http api



我已经使用 API 网关创建了一个示例 HTTP API(目前处于测试版(。此 API 不使用任何身份验证,并且具有 lambda 作为集成。该路由接受任何 HTTP 方法,并且我已经确认 lambda 具有适当的 API 网关权限。此权限是在我创建 API 时添加的。

但是,当我调用 API 时,我收到的 HTTP 状态为 500,正文为:{"message":"Internal Server Error"}.

如果我将其设置为REST API而不是HTTP API,则相同的lambda和API将起作用。

任何想法为什么这在HTTP API中不起作用?

我在Internal Server Error上遇到了类似的问题。我正在使用新的API Gateway HTTP API(beta(和nodejs lambda集成。lambda本身工作,REST API工作正常,正如你提到的。

花费我 8 个小时的 2 个问题:

  1. 我必须向处理程序添加async前缀或使用回调,但我不能只返回没有async的响应。请参阅 https://docs.aws.amazon.com/lambda/latest/dg/nodejs-prog-model-handler.html

  2. 响应结构必须正确,请参阅此页面:https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-gateway-proxy-integration-create-lambda-backend,滚动到示例函数代码,您将看到以下内容:

// The output from a Lambda proxy integration must be 
// in the following JSON object. The 'headers' property 
// is for custom response headers in addition to standard 
// ones. The 'body' property  must be a JSON string. For 
// base64-encoded payload, you must also set the 'isBase64Encoded'
// property to 'true'.

所以我的函数最终看起来像这样,请注意async和正确的响应结构:

exports.handler = async function(event, context) {
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!')
};
return response;
}

最新更新