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)?
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!
假设您安装了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配置文件,这样您就不必使用环境变量和区域参数(尽可能多).
因为我喜欢可以使用最少的搜索/替换和复制粘贴的答案
pip install awscli aws configure
$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
并相应地更改查询!