AWS API Gateway始终返回502错误的网关 [英] AWS API Gateway always returns 502 bad gateway

查看:70
本文介绍了AWS API Gateway始终返回502错误的网关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AWS中创建了一个简单的lambda函数,该函数从DynamoDB返回列表.我还创建了API网关来触发lambda函数.当我在AWS控制台中进行测试时,该功能可以正常工作.但是当我在Postman中测试此功能时,总是会在错误下方出现502错误的网关.({"message":内部服务器错误"}

I have created a simple lambda function in AWS that returns list from DynamoDB. I have also created API Gateway to trigger the lambda function. The function works fine when I test in AWS console. But I always get 502 bad gateway below error when I test this function in Postman. ({ "message": "Internal server error" }

下面是node.js中的函数:

Below is the function in node.js:

const doc = require('dynamodb-doc');

const dynamo = new doc.DynamoDB();

/**
 * Provide an event that contains the following keys:
 *
 *   - operation: one of the operations in the switch statement below
 *   - tableName: required for operations that interact with DynamoDB
 *   - payload: a parameter to pass to the operation being performed
 */
exports.handler = async (event) => {

    const operation = event.operation;
    const payload = event.payload;

    if (event.tableName) {
        payload.TableName = event.tableName;
    }

    switch (operation) {
        case 'create':
            return await dynamo.putItem(payload).promise();
        case 'read':
            return await dynamo.getItem(payload).promise();
        case 'update':
            return await dynamo.updateItem(payload).promise();
        case 'delete':
            return await dynamo.deleteItem(payload).promise();
        case 'list':
            return await dynamo.scan(payload).promise();
        case 'echo':
            return payload;
        case 'ping':
            return 'pong';
        default:
            throw new Error(`Unrecognized operation "${operation}"`);
    }
};

下面是生成的API网关端点详细信息.

Below is generated API Gateway Endpoint details.

API endpoint: https://iabzqisam7.execute-api.us-east-1.amazonaws.com/test/moneyAppDynamoDBOperationsLambda
Authorization: NONE
Method: ANY
Resource path: /moneyAppDynamoDBOperationsLambda
Stage: test

这是我尝试使用邮递员测试API的方式:

Here is how I am trying to test API using Postman:

 Postman  URL(Get) : https://iabzqisam7.execute-api.us-east-1.amazonaws.com/test/moneyAppDynamoDBOperationsLambda

    Headers: Key: content-type, Value: application/json
    Body (raw) :
    {
        "operation": "list",
        "tableName": "Advertiser",
        "payload": {
            "TableName": "Advertiser"
        }
    }

它在AWS控制台中运行良好.

It works perfectly fine within AWS console.

有人知道为什么我从Postman调用API Gateway时仍然遇到502错误的网关错误吗?

Any idea why I am keep getting 502 bad gateway error while calling API Gateway from Postman ?

推荐答案

502错误的网关异常,通常用于返回不兼容的输出来自Lambda代理集成后端,有时用于由于重载而导致的无序调用.

502 Bad Gateway Exception, usually for an incompatible output returned from a Lambda proxy integration backend and occasionally for out-of-order invocations due to heavy loads.

API getway输出不会告诉您该问题与Lambda错误或API getway或策略问题有关.

API getway output will not tell you that the problem is related to a Lambda error or API getway or policy issue .

API网关返回502,表示它不理解Lambda返回的输出,并向您提供{消息":内部服务器错误"}} 502.

The API Gateway returned a 502 which means that it didn’t understand the output returned by Lambda and give you {"message": "Internal server error"} 502.

在API getway上使用启用登录进行调试

创建新的IAM角色以允许API网关将日志推送到CloudWatch.随附以下政策附件:

Create new IAM role to allow API Gateway to push logs to CloudWatch. Attached following policy attach:

{
    "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->'保存'

In API Gateway console -->Go to settings -> Add ARN of the API Gateway-CloudWatch logging role--> 'Save'

进入您的API阶段.在"CloudWatch设置"下,选择启用CloudWatch Logs".将日志级别"设置为信息".选择记录完整的请求/响应数据".

Go to the stage of your API. Under 'CloudWatch Settings', select 'Enable CloudWatch Logs'. Set 'Log level' to 'INFO'. Select 'Log full requests/responses data'.

Plesae检查日志并共享有问题的错误日志.

Plesae check log and share error logs in question .

用于代理集成的Lambda函数的输出格式

如果函数输出的格式不同或格式错误,则API Gateway会返回502 Bad Gateway错误响应.

If the function output is of a different format or malformed , API Gateway returns a 502 Bad Gateway error response .

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆