当前位置:  开发笔记 > 编程语言 > 正文

Method of finding instances attached to ELB

如何解决《MethodoffindinginstancesattachedtoELB》经验,为你挑选了3个好方法。

I am using the Amazon AWS ELB command line tools. Is there a way of finding out the instances attached to a particular Elastic Load Balancer (ELB)?



1> Till..:

2013/12/18:更新这个,因为链接已经死了!

我安装了新的AWS cli工具:

$ pip install awscli

然后跑了:

$ aws configure                                                                                                                                                
AWS Access Key ID [None]: my-key
AWS Secret Access Key [None]: my-secret
Default region name [None]: us-east-1
Default output format [None]:

此数据保存到~/.aws/config.

然后我可以找到连接到负载均衡器的实例,如下所示:

$ aws elb describe-load-balancers --load-balancer-name "my-name"
{
    "LoadBalancerDescriptions": [
        {
            "Subnets": [],
            "CanonicalHostedZoneNameID": "ID",
            "CanonicalHostedZoneName": "my-name-foo.us-east-1.elb.amazonaws.com",
            "ListenerDescriptions": [
                {
                    "Listener": {
                        "InstancePort": 80,
                        "LoadBalancerPort": 80,
                        "Protocol": "HTTP",
                        "InstanceProtocol": "HTTP"
                    },
                    "PolicyNames": []
                },
                {
                    "Listener": {
                        "InstancePort": 80,
                        "SSLCertificateId": "arn:aws:iam::x:server-certificate/x-ssl-prod",
                        "LoadBalancerPort": 443,
                        "Protocol": "HTTPS",
                        "InstanceProtocol": "HTTP"
                    },
                    "PolicyNames": [
                        "AWSConsole-SSLNegotiationPolicy-api-production"
                    ]
                }
            ],
            "HealthCheck": {
                "HealthyThreshold": 10,
                "Interval": 30,
                "Target": "HTTP:80/healthy.php",
                "Timeout": 5,
                "UnhealthyThreshold": 2
            },
            "BackendServerDescriptions": [],
            "Instances": [
                {
                    "InstanceId": "i-FIRST-INSTANCEID"
                },
                {
                    "InstanceId": "i-SECOND-INSTANCEID"
                }
            ],
            "DNSName": "my-name-foo.us-east-1.elb.amazonaws.com",
            "SecurityGroups": [],
            "Policies": {
                "LBCookieStickinessPolicies": [],
                "AppCookieStickinessPolicies": [],
                "OtherPolicies": [
                    "AWSConsole-SSLNegotiationPolicy-my-name"
                ]
            },
            "LoadBalancerName": "my-name",
            "CreatedTime": "2013-08-05T16:55:22.630Z",
            "AvailabilityZones": [
                "us-east-1d"
            ],
            "Scheme": "internet-facing",
            "SourceSecurityGroup": {
                "OwnerAlias": "amazon-elb",
                "GroupName": "amazon-elb-sg"
            }
        }
    ]
}

数据在LoadBalancerDescriptions.Instances.

我的负载均衡器被调用my-name- 这是您在创建时选择的名称.

老答案下面!

我不熟悉cli工具,但我使用了API.

我会检查这两个请求:

DescribeLoadBalancers

DescribeInstanceHealth

cli工具可能有类似的东西吗?

HTH!


在文档中的`DescribeLoadbalancers`的示例响应中有一个``.如果这不起作用,请尝试`DescribeInstanceHealth`?它需要两个参数 - 可选的实例列表和负载均衡器名称.从我的理解,这应该def.如果省略实例列表并仅提供loadbalancer的名称,则返回所有这些实例.

2> Mike D..:

假设您安装了aws-cli和jq,可以使用以下命令获取关联的ec2实例ID:

aws elb describe-load-balancers --load-balancer-name my-elb \
  | jq -r '.LoadBalancerDescriptions[].Instances[].InstanceId'

这将返回与该ELB关联的ec2 ID.

旁注:我建议您设置aws cli配置文件,这样您就不必使用环境变量和区域参数(尽可能多).



3> Cyril Duchon..:

因为我喜欢可以使用最少的搜索/替换和复制粘贴的答案

先决条件:已配置aws-cli

pip install awscli
aws configure

配置:您的ELB名称

$ELB_NAME = "Your-elb-name"

在终端中复制粘贴

for ID in $(aws elb describe-load-balancers --load-balancer-name $ELB_NAME \
    --query LoadBalancerDescriptions[].Instances[].InstanceId \
    --output=text);
do
    aws ec2 describe-instances --instance-ids $ID \
        --query Reservations[].Instances[].PublicIpAddress \
        --output text
done

将输出公共IP列表.您也可以只在括号内执行查询for ID in $(...)以获取实例ID

想要不同的东西?

随意看看结构

aws elb describe-load-balancers --load-balancer-name $ELB_NAME
aws ec2 describe-instances --instance-ids $INSTANCE_ID

并相应地更改查询!

推荐阅读
小色米虫_524
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有