node.js - 为什么 Lambda 在这个简单的函数上返回 502 服务器错误?

标签 node.js amazon-web-services aws-lambda

这是我的简单函数:

'use strict';

console.log('Loading function');

exports.handler = (event, context, callback) => {

let myMessage = "Testing this message thing.";
callback(null, myMessage);

};

然而,每当我运行它时,我都会收到 502 内部服务器错误。

以下是日志:

2018-04-24T01:08:24.731Z    fcd84d74-475b-11e8-b29c-773ba9f4ad9c    Loading function
START RequestId: fcd84d74-475b-11e8-b29c-773ba9f4ad9c Version: $LATEST
END RequestId: fcd84d74-475b-11e8-b29c-773ba9f4ad9c
REPORT RequestId: fcd84d74-475b-11e8-b29c-773ba9f4ad9c  Duration: 9.04 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 19 MB  
START RequestId: 0558d0e2-475c-11e8-a47c-7df3b9f2c46d Version: $LATEST
END RequestId: 0558d0e2-475c-11e8-a47c-7df3b9f2c46d
REPORT RequestId: 0558d0e2-475c-11e8-a47c-7df3b9f2c46d  Duration: 4.93 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 19 MB  

我在这里缺少什么吗?这应该是非常基本的东西。

TTIA

最佳答案

好吧,看来 502 的原因是因为我没有返回对象。我将代码更改为您在下面看到的内容。我留下了一些注释行来显示哪些内容也有效(我将内容类型更改为 text/html 只是为了看看它是否会返回真正的 HTML,它确实返回了),剩下的是最低限度的我需要成功获得响应。

'use strict';

exports.handler = (event, context, callback) => {

    var responseBody = "Hi there";

    var response = {
        // "statusCode": 200,
        // "headers": {
        //     "Content-Type": "text/html"
        // },
        body: responseBody
        // "isBase64Encoded": false
    };

    callback(null, response);
};

我希望这可以帮助其他正在为此苦苦挣扎的人。

关于node.js - 为什么 Lambda 在这个简单的函数上返回 502 服务器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49992241/

相关文章:

node.js - body-parser,JSON 中位置处出现意外标记 u

javascript - Mustache.js 递归

amazon-web-services - LabLearner CloudFormation 中的 LabRole

amazon-web-services - 使用 Cloudformation 和 Pystache 派生 IP 范围和网络掩码

logging - 有没有办法查看 lambda 函数何时部署以及谁部署的?

python - AWS Lambda 的 python 请求模块问题

amazon-web-services - Azure Functions 是否有与 LambdaEntryPoint 等效的工具?

javascript - 使用 Node.js 和 restify 进行基本身份验证

php - 连接到 socket.io 服务器的 easyRTC 错误

python-3.x - 访问事件时,AWS Lambda 带有代理集成的 Lambda 格式错误