簡體   English   中英

Node.js:使用describeLoadBalancers獲取EC2實例信息

[英]Node.js: Getting EC2 instances information by using describeLoadBalancers

我想獲取AWS雲清單的元數據。 其中包括負載均衡器,每個負載均衡器的EC2實例數,這些實例的狀態和配置,其他配置等。

當前,我正在使用describeLoadBalancers ,然后在獲得描述時,我只是從中解析出實例信息。

這是腳本(加載AWS開發工具包並設置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實例的信息?

AWS.ELB和AWS.ELBv2之間的區別在於它們與AWS中不同版本的負載均衡器進行交互。 AWS.ELBv2將與新一代負載均衡器(應用程序負載均衡器和網絡負載均衡器)一起使用。 AWS.ELB僅適用於經典類型的負載均衡器

使用ALB和NLB,您可以擁有目標組的概念,即注冊實例所針對的對象。 您需要查詢目標組以獲取實例信息。

您將不得不原諒快速而粗糙的代碼,但是它演示了將實例從目標組注冊到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)
  }
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM