Alexa - 从 AWS Lambda 控制台调用本地主机中的 Web API

物联网 亚历克斯 aws-iot aws
2021-06-23 11:12:35

我在 AWS Lambda 控制台上有我的 Alexa 的 lambda 函数。在那里我调用了我创建的 Web API。

如果我在 Visual Studio Code 上调用我的 Web API,效果很好。但是如果我使用 Alexa Developer Console 来调用我的 Web API,它总是说:

错误:连接 ECONNREFUSED 127.0.0.1:63713

这是因为我的 Web API 在localhost? 我该如何解决这个问题?我还在努力使用 Alexa Developer Console 在本地进行测试......

我的代码:

var url = "http://localhost:63713/_apis/v1.0/Car/GetCarById?id=1";

http.get(url, function (res) {
    var webResponseString = '';

    if (res.statusCode != 200) {
        doWebRequestCallBack(new Error("Non 200 Response"), null);
    }

    res.on('data', function (data) {
        webResponseString += data;
    });

    res.on('end', function () {
        var webResponseObject = JSON.parse(webResponseString);
        doWebRequestCallBack(null, webResponseObject);
    });
}).on('error', function (e) {
    doWebRequestCallBack(new Error(e.message), null); // <-- where I recive the error message
});
1个回答

localhost 始终指向运行代码的机器。

在这种情况下,lambda 在 Amazon 的一台机器上运行,因此您尝试访问的 Web 应用程序将不存在(因为它在您的机器上运行)。

您需要将您的 Web 应用程序部署到某个公共位置(例如 AWS VM 或 Light Sailing 实例)并更新 lambda 以指向该位置。