gpt4 book ai didi

amazon-web-services - 错误: Unexpected server response: 502 on trying to connect to a lambda function through Amazon API gateway Websocket API

转载 作者:行者123 更新时间:2023-12-03 16:41:40 25 4
gpt4 key购买 nike

我有一个简单的Node.js 10.x Lambda函数,在Lambda function console上进行测试时可以正常工作:

exports.handler = async (event) => {
var msg = 'connected';
return {
statusCode: 200,
body: { msg: msg}
};
};

我通过他们的 API Gateway console创建了 WebSocket API ,并通过 LAMBDA_PROXY集成将其集成到其中:

Websocket API on Amazon API Gateway web console

当我尝试使用 wscat连接到它时,出现错误:

wscat -c "wss://awesomeid1.execute-api.us-west-2.amazonaws.com/dev"
error: Unexpected server response: 502

我该如何解决?

额外的问题:我们如何找到Websocket API连接错误的日志?

无法解决问题的相关链接:
  • 502 internal server error response from API gateway from lambda function
  • AWS - Using @connections websocket call back url to send response from back-end(one-way) - API Gateway websocket protocol
  • AWS API Gateway error response generates 502 "Bad Gateway"
  • 最佳答案

    我找到了this answer的根本原因。基本上,使用Lambda代理集成时的,Lambda函数响应body值的需要为string 。我们需要使用JSON.stringify()或类似的东西:

    exports.handler = async (event) => {
    var msg = 'connected';
    return {
    statusCode: 200,
    body: JSON.stringify({ msg: msg}) /*required on lambda proxy integration*/
    };
    };

    Output Format of a Lambda Function for Proxy Integration部分上研究文档:

    In Lambda proxy integration, API Gateway requires the backend Lambda function to return output according to the following JSON format

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

    [...]

    The output body is marshalled to the frontend as the method response payload [...]

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



    回答红利问题:

    在添加新的IAM角色 AmazonAPIGatewayPushToCloudWatchLogs 并在其摘要页面上获取其ARN之后,我们可以按照以下步骤操作:

    Set up API Logging Using the API Gateway Console

    并看到服务器上的日志消息更具描述性:
    Execution failed due to configuration error: Malformed Lambda proxy response

    关于amazon-web-services - 错误: Unexpected server response: 502 on trying to connect to a lambda function through Amazon API gateway Websocket API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57438756/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com