【问题标题】:Node.js: Getting EC2 instances information by using describeLoadBalancersNode.js:使用 describeLoadBalancers 获取 EC2 实例信息
【发布时间】:2019-08-03 22:51:44
【问题描述】:

我想获取 AWS 云清单的元数据。这包括负载均衡器、每个负载均衡器的 EC2 实例数、这些实例的状态和配置、其他配置等。

目前我正在使用describeLoadBalancers,然后当我得到描述时,我只是从中解析出实例信息。

这是脚本(在加载 AWS SDK 并设置 apt 区域后。凭证存储在文件中)

var elb = new AWS.ELB();
// var elb = new AWS.ELBv2({apiVersion: '2015-12-01'}); // For this, inside function ProcessDescriptions I get error: Cannot read property 'forEach' of undefined

elb.describeLoadBalancers(null, ProcessDescriptions);

function ProcessDescriptions(err, descriptions)
{
    if (err != undefined)
    {
        console.log (JSON.stringify(err));
    }
    else
    {
        descriptions.LoadBalancerDescriptions.forEach(ProcessDescription);
        // console.log (JSON.stringify(descriptions));
    }
}

function ProcessDescription(description)
{
    if(description.Instances[0] != undefined)
    {
        console.log(description.Instances[0].InstanceId);
        console.log(description.LoadBalancerName);
    }
}

问题:

  1. 使用 AWS.ELB 脚本运行但不返回所有负载平衡器。

  2. 使用 AWS.ELBv2 脚本运行并返回 #1 中缺少的负载平衡器(在 descriptions 中)。但不幸的是我收到错误Cannot read property 'forEach' of undefined。基本上我在 JSON 响应中没有得到 LoadBalancerDescriptions 导致错误。

问题:

我想知道这是获取负载均衡器和 EC2 库存的合适方法吗? AWS.ELBAWS.ELBv2 有什么区别?如何获取所有负载均衡器以及附加到它们的 EC2 实例的信息?

【问题讨论】:

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


    【解决方案1】:

    AWS.ELB 和 AWS.ELBv2 之间的区别在于它们与 AWS 中不同版本的负载均衡器交互。 AWS.ELBv2 将与新一代负载均衡器(应用程序负载均衡器和网络负载均衡器)一起使用。 AWS.ELB 仅适用于 Classic 类型的负载均衡器

    对于 ALB 和 NLB,您有 Target Groups 的概念,这是您注册实例的依据。您需要查询目标组以获取实例信息。

    您必须原谅快速而粗略的代码,但它演示了将实例从目标组注册到 ALB/NLB 所需的步骤。

    您可能需要修改此代码以满足您的需要。

    var AWS = require('aws-sdk');
    AWS.config.update({region: 'eu-west-2'});
    
    // create the promise object for ELB call
    var elb_describe = new AWS.ELB().describeLoadBalancers().promise();
    
    // handle the promise object
    elb_describe.then(
      function(data) {
        for (i = 0; i < (data.LoadBalancerDescriptions).length; i++) {
            console.log(`Instances attached to ${data.LoadBalancerDescriptions[i].LoadBalancerName}`)
            for (j = 0;  j < (data.LoadBalancerDescriptions[0].Instances).length; j++) {
                console.log(data.LoadBalancerDescriptions[0].Instances[j])
            } 
        }
      },
      function(error) {
        console.log(error)
      }
    );
    
    // create promise object for elbv2 call
    var elbv2_describe = new AWS.ELBv2().describeLoadBalancers().promise();
    
    // handle the promise object
    elbv2_describe.then(
      function(data) {
       // for number of results returned in describe load balancers call
       for (i = 0; i < (data.LoadBalancers).length; i++) {
            let lb_arn = data.LoadBalancers[i].LoadBalancerArn
            // get Target groups associated with the load balancer 
            elbv2_get_target_groups = new AWS.ELBv2().describeTargetGroups({ LoadBalancerArn: lb_arn}).promise();
            elbv2_get_target_groups.then(
                function(data) {
                    //only selecting the first result from the results, will need modifying for LBs forwarding to multiple TGs
                    let tg_arn = data.TargetGroups[0].TargetGroupArn
                    // get target health of instances registered to the target group (only way I could find of getting the instance-id)
                    elbv2_get_target_health = new AWS.ELBv2().describeTargetHealth({ TargetGroupArn: tg_arn}).promise();
                    elbv2_get_target_health.then(
                        function(data) {
                            console.log(`Instances registered to '${tg_arn}', associated with ${lb_arn}`)
                            for (j = 0; j < (data.TargetHealthDescriptions).length; j++) {
                                console.log(data.TargetHealthDescriptions[j].Target)
                            }
                        }
                    )
                }
            )
        }
      },
      function(error) {
        console.log(error)
      }
    );
    

    【讨论】:

      猜你喜欢
      • 2021-11-19
      • 2018-10-29
      • 2017-07-28
      • 2016-12-02
      • 2021-02-10
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多