【问题标题】:Timeout connecting to OpsWorks from a lambda function从 lambda 函数连接到 OpsWorks 时超时
【发布时间】:2017-03-09 10:30:12
【问题描述】:

我正在编写一个 lambda 函数来尝试连接到在给定 OpsWorks 层下运行的所有实例上的端点。我的问题是当我尝试测试它时该功能超时。输出中没有列出其他错误,所以我不确定从哪里开始调试这个问题。

我找到了this StackOverflow 问题,我尝试在没有 VPC 的情况下创建函数,但问题仍然存在(有问题的 OpsWorks 堆栈位于 VPC 后面)。

有问题的 lambda 代码是:

exports.handler = function(event, context) {
    var done = false; 

    var awsOptions = {
        region: 'us-east-1'
    };

    var AWS = require('aws-sdk');
    var opsWorks = new AWS.OpsWorks(awsOptions);

    var appIds = ["MY_APP_ID"];
    var layerIds = ["MY_LAYER_ID"];
    var stackId = "MY_STACK"

    var isOnline = function(instance) {
        return instance.Status == 'online';
    }

    console.log("Have all the variables set up, time to run...");

    opsWorks.describeLayers({StackId: stackId, LayerIds: layerIds}, 
        function(error, layers) {

        console.log("Described the layer...");
        console.log(layers);

        opsWorks.describeInstances({LayerId: layers[0].LayerId}, 
            function(err, instances) {

            console.log("Described the instances on the layer");
            if (err) {
                console.error("No instances found!");
            }

            console.log("No errors, keep going...");
            instances.Instances.filter(isOnline).forEach(function(instance) {

                console.log("Filtered out the offline instances.");
                var options = {
                    method: "GET",
                    path: "/endpoint",
                    port: 123,
                    timeout: 10000,
                    url: instance.Hostname    
                };

                http.request(options, function(response) {

                    if (response.statusCode != 200) {

                        console.error("Instance " + instance.InstanceId 
                            + " is not responding!");

                    } else {

                        console.log("Got a response, time to do stuff!");

                    }
                });

            });

            done = true;

        });
    });

    while (!done) {
        // Keep waiting...
    }  
};

【问题讨论】:

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


    【解决方案1】:

    埃里克,

    我通过两种方式解决了我面临的类似问题:

    1. 移除了 VPC(你已经提到你已经尝试过了)
    2. JS 代码问题,如果 JS 代码遇到错误,它会停止执行 lambda 函数,并且会冻结 lambda,并在您可能在 lambda 控制台中指定的超时后超时。在我的例子中,我没有声明一个变量并开始使用它并得到了超时错误,但是当我声明它并解决了其他一些与 JS 相关的错误时,它就像一个魅力。

    希望对你有帮助!

    【讨论】:

    • 谢谢!我最终放弃了 Node 并用 Java 编写它,它最终为我工作。
    猜你喜欢
    • 2021-02-14
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2017-10-22
    • 2017-10-05
    • 2021-08-28
    • 1970-01-01
    • 2018-03-03
    相关资源
    最近更新 更多