502 Bad Gateway Exception,通常是返回不兼容的输出
来自 Lambda 代理集成后端,偶尔用于
由于负载过重而导致乱序调用。
API 网关输出不会告诉您问题与 Lambda 错误或 API 网关或策略问题有关。
API 网关返回 502,这意味着它不理解 Lambda 返回的输出,并为您提供 {“message”: “Internal server error”} 502。
使用 API 网关上的启用日志记录进行调试
创建新的 IAM 角色以允许 API Gateway 将日志推送到 CloudWatch。
附以下政策附件:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:DescribeLogGroups",
"logs:DescribeLogStreams",
"logs:PutLogEvents",
"logs:GetLogEvents",
"logs:FilterLogEvents"
],
"Resource": "*"
}
]
}
信托政策:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
在 API Gateway 控制台中 --> 转到设置 -> 添加 API Gateway-CloudWatch 日志记录角色的 ARN --> '保存'
转到您的 API 阶段。在“CloudWatch 设置”下,选择“启用 CloudWatch Logs”。将“日志级别”设置为“信息”。选择“记录完整请求/响应数据”。
请检查日志并分享有问题的错误日志。
如果函数输出的格式不同或格式错误,API Gateway 将返回 502 Bad Gateway 错误响应。
- https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
-
https://aws.amazon.com/premiumsupport/knowledge-center/malformed-502-api-gateway/ https://aws.amazon.com/premiumsupport/knowledge-center/malformed-502-api-gateway/.