AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 586315
Accepted
Nam Nguyen
Nam Nguyen
Asked: 2014-04-03 18:32:11 +0800 CST2014-04-03 18:32:11 +0800 CST 2014-04-03 18:32:11 +0800 CST

从附加的实例中获取 AWS ELB 名称

  • 772

我创建了一个 ELB 并将一些实例附加到这个 ELB。因此,当我登录到其中一个附加实例时,我想键入一个命令或运行一个 nodejs 脚本,它可以返回它的 ELB 名称。可能吗?我知道我可以在 AWS 控制台上查找,但我正在寻找一种以编程方式查找它的方法。如果可能的话,我想看看它是如何在命令或 AWS Nodejs SDK 中完成的。

谢谢!

amazon-ec2
  • 6 6 个回答
  • 8715 Views

6 个回答

  • Voted
  1. Nestor Pina
    2015-08-14T11:05:24+08:002015-08-14T11:05:24+08:00

    如果有人来这里寻找纯 bash 解决方案。

    使用jq过滤和解析 AWS CLI 的响应:

    aws elb describe-load-balancers | jq -r '.LoadBalancerDescriptions[] | select(.Instances[].InstanceId == "<YOUR-INSTANCE-ID>") | .LoadBalancerName ' 
    

    同样在 aws-codedeploy-samples 中,他们在common_functions.sh中定义了这个函数。我使用 ASG 时尚未对其进行测试,但我想它会起作用

    # Usage: get_elb_list <EC2 instance ID>
    #
    #   Finds all the ELBs that this instance is registered to. After execution, the variable
    #   "INSTANCE_ELBS" will contain the list of load balancers for the given instance.
    #
    #   If the given instance ID isn't found registered to any ELBs, the function returns non-zero
    get_elb_list() {
        local instance_id=$1
    
        local elb_list=""
    
        local all_balancers=$($AWS_CLI elb describe-load-balancers \
            --query LoadBalancerDescriptions[*].LoadBalancerName \
            --output text | sed -e $'s/\t/ /g')
    
        for elb in $all_balancers; do
            local instance_health
            instance_health=$(get_instance_health_elb $instance_id $elb)
            if [ $? == 0 ]; then
                elb_list="$elb_list $elb"
            fi
        done
    
        if [ -z "$elb_list" ]; then
            return 1
        else 
            msg "Got load balancer list of: $elb_list"
            INSTANCE_ELBS=$elb_list
            return 0
        fi
    }
    
    • 7
  2. Best Answer
    Ameer Deen
    2014-04-04T21:02:26+08:002014-04-04T21:02:26+08:00

    我在 JavaScript 方面不是最优秀的,但我测试了下面的代码并且工作正常。它基本上使用“describeLoadBalancers”API 调用来获取所有 ELB 的列表,然后遍历结果以查找您的实例。如果您的实例注册到特定的负载均衡器,它的名称将输出到控制台:

    // Require AWS SDK for Javascript
    var AWS = require('aws-sdk');
    
    // Set API Keys and Region
    AWS.config.update({
        "accessKeyId": "<your access key>", 
        "secretAccessKey": "<your secret key>",
        "region": "us-west-1" // specify your region
    });
    
    // Get All Load Balancers
    function GetLoadBalancers(fn)
    {
        var elb = new AWS.ELB();
        elb.describeLoadBalancers(null,function(err, data) {
            fn(data)
        });
    }
    
    // Loop through response to check if ELB contains myInstanceId
    var myInstanceId = "<your instance id>";
    GetLoadBalancers(function(elbs){
        elbs.LoadBalancerDescriptions.forEach(function(elb){
          if(elb.Instances[0] != undefined){
            if (elb.Instances[0].InstanceId == myInstanceId){
                console.log(elb.LoadBalancerName);
            }
          }
        });
    });
    
    • 2
  3. Pahud Hsieh
    2016-08-18T02:30:02+08:002016-08-18T02:30:02+08:00

    试试这个脚本:

    #!/bin/bash
    
    instanceId='i-XXXXXXXXXXXXX'
    
    aws elb describe-load-balancers --query \
    "LoadBalancerDescriptions[?Instances[?InstanceId=='${instanceId}']].LoadBalancerName"
    
    • 2
  4. EEAA
    2014-04-03T20:32:29+08:002014-04-03T20:32:29+08:00

    当然。使用 aws cli:

    $ aws elb describe-load-balancers --load-balancer-name "your-elb-name"
    

    您要查找的信息位于LoadBalancerDescriptions.Instances.

    • 1
  5. Drew Khoury
    2014-04-03T21:30:43+08:002014-04-03T21:30:43+08:00

    一个实例可以附加到任意数量的 ELB。

    您可以使用 API 为您的实例搜索所有 ELB。

    您可能需要考虑向您的 EC2 添加一个 TAG,其中包含有关它附加到哪个 ELB 的信息,以便您可以直接查询它(EC2)。

    • 0
  6. Robert Jackson
    2017-03-08T08:43:56+08:002017-03-08T08:43:56+08:00

    我想补充一下保罗的出色回答。此脚本将提示输入 EC2 实例 ID

    echo "Enter EC2 instance name?" 
    
    read instanceId
    
    echo
    echo The instance name is: $instanceId
    echo
    
    aws elb describe-load-balancers --query \
    "LoadBalancerDescriptions[?Instances[?InstanceId=='${instanceId}']].LoadBalancerName"
    
    • 0

相关问题

  • 权限被拒绝(公钥)。从本地 Ubuntu 到 Amazon EC2 服务器的 SSH

  • 管理员如何管理他们的 EC2 EBS 和快照?

  • 云有多大?[关闭]

  • EC2 映像启动

  • 如何将安全组添加到正在运行的 EC2 实例?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve