我遇到了 AWS API Gateway 和 Lambda 集成的问题。我有一个 Terraform 配置,用于设置具有 Lambda 代理集成的 API 网关。 aws_api_gateway_integration 中的类型设置为“AWS_PROXY”。但是,当我使用 Postman 测试 API 端点时,我收到内部服务器错误。
有趣的是,当我取消选中并选中 API Gateway 控制台中的“使用 Lambda 代理集成”选项时,端点开始正常工作,返回 200 响应。但我想了解问题的根本原因并确保配置一致。
这是我已经检查过的:
是否有我可能遗漏的东西或我需要考虑的任何其他配置?如果您能深入了解为什么取消选中并选中“使用 Lambda 代理集成”选项可以解决内部服务器错误,我们将不胜感激。
提前感谢您的帮助!
这是 terraform“aws_api_gateway_integration”资源详细代码:
resource "aws_api_gateway_integration" "ok_integration" {
rest_api_id = aws_api_gateway_rest_api.api_gateway_rest_api_ok.id
resource_id = aws_api_gateway_resource.product_resource.id
http_method = aws_api_gateway_method.method_resource.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.lambda_function_ok.invoke_arn
}
这是我的 lambda 函数:
exports.handler = async (event) => {
console.log("Lambda function invoked successfully");
// Process the event or perform any desired actions
// ...
const response = {
statusCode: 200,
body: "OK",
};
return response;
};
请注意,您的 API 网关需要将请求正确代理到 Lambda 函数。
检查并调查您是否已在 Lambda 代理端点上正确设置
greedy path parameter
。
感谢使用 IaC 原则,在 UI 中进行调试可能会节省您解决问题的时间。