查找附加到ELB的实例的方法
我正在使用 Amazon AWS ELB 命令行工具。有没有办法找到附加到特定弹性负载均衡器(ELB)的实例?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
2013/12/18:要更新此内容,因为链接已失效!
我安装了新的 AWS cli 工具:
然后运行:
此数据保存到
~/.aws/config.
然后我可以找到连接到负载均衡器的实例,如下所示:
数据位于 LoadBalancerDescriptions.Instances 中。
我的负载均衡器名为
my-name
— 这是您在创建它时选择的名称。下面是旧答案!
我不熟悉cli工具,但我使用了API。
我会检查这两个请求:
DescribeLoadBalancers
DescribeInstanceHealth
cli 工具可能有类似这些的东西?
哈!
2013/12/18: To update this and since the links are dead!
I installed the new AWS cli tools:
Then ran:
This data is saved into
~/.aws/config
.Then I can find instances connected to a loadbalancer like so:
The data is in
LoadBalancerDescriptions.Instances
.My loadbalancer is called
my-name
— this is the name you selected when you created it.Old answer below!
I'm not familiar with the cli tool, but I used the API.
I'd check these two requests:
DescribeLoadBalancers
DescribeInstanceHealth
The cli tool probably has something to resemble these?
HTH!
假设您有 aws-cli 和 jq 安装后,您可以使用以下命令获取关联的 ec2 实例 id:
这将返回与该 ELB 关联的 ec2 id 。
旁注:我建议您设置 aws cli 配置文件< /a> 所以你不必摆弄环境变量和区域参数(尽可能多)。
Assuming you have aws-cli and jq installed, you can use the following command to get associated ec2 instance ids:
This will return the ec2 ids associated with that ELB.
Side note: I recommend you setup aws cli profiles so you don't have to fiddle with environment variables and region params (as much).
因为我喜欢可以通过最少的搜索/替换和复制粘贴使用的答案
先决条件:配置了 aws-cli
配置:您的 ELB 名称
在终端中复制粘贴
将输出公共 IP 列表。您还可以在
for ID in $(...)
的括号内执行查询来获取实例 ID想要不同的东西吗?
请随意查看 的结构
并相应地更改查询!
Because I love answers that can be used with a minimum of search/replace and copy paste
Prerequisites : aws-cli configured
Configure : your ELB name
Copy-n-Paste in terminal
Will output a list of Public IPs. You could also just execute the query inside the parenthesis of the
for ID in $(...)
to just get the instance IDsWant something different ?
Feel free to have a look at the structure of
and change the query accordingly!
如果有人通过搜索到达这里,了解为什么
elb-describe-lbs
命令在启动并运行 ELB 时不返回任何内容,我意识到我需要添加EC2_REGION=eu-west -1
到我的环境变量(或使用elb-describe-lbs --region
命令)If anyone arrives here from a search as to why the
elb-describe-lbs
command returns nothing when they have ELBs up and running, what I realised was I needed to addEC2_REGION=eu-west-1
to my environment variables (or useelb-describe-lbs --region
command)如果您想查看所有 ELB 和附加的实例,请使用 JMESPath,如下所示:
结果
如果您知道该实例的名称ELB 并希望查看附加内容,请使用 JMESPath,如下所示:
结果:
If you want to see all your ELB's and the instances attached use JMESPath like this:
Result
If you know the name of the ELB and want to see what is attached use JMESPath like this:
Result:
将 INSTANCEID 替换为实际实例 ID
aws elb describe-load-balancers --query "LoadBalancerDescriptions[*].{ID:LoadBalancerName,InstanceId:Instances[?InstanceId=='INSTANCEID'].InstanceId}[*].{ ID:ID,InstanceId:InstanceId[0]}" --output=text | grep 实例ID | awk '{print $1}'
replace INSTANCEID with actual instance id
aws elb describe-load-balancers --query "LoadBalancerDescriptions[*].{ID:LoadBalancerName,InstanceId:Instances[?InstanceId=='INSTANCEID'].InstanceId}[*].{ID:ID,InstanceId:InstanceId[0]}" --output=text | grep INSTANCEID | awk '{print $1}'
在node.js 中,您可以使用
aws-sdk
来完成此操作。data.LoadBalancerDescriptions
是一个数组,数组中的每个元素都是一个具有属性Instances
的对象,该属性具有实例 ID。In node.js you can do this by using
aws-sdk
.data.LoadBalancerDescriptions
is an array and each element in the array is an object with the propertyInstances
that has the instance id.您可以循环遍历所有负载均衡器实例 ID,如下所示:
您可以循环遍历负载均衡器名称,如下所示:
前提是您已配置 aws cli :
src: http://docs.aws.amazon.com/ cli/latest/topic/config-vars.html
猫<< “EOF”> ~/.aws/config
并配置您的安全凭证:
You can loop trough all you load balancer instance ids as follows:
You can loop trough your load balancers names as follows :
Provided that you have configured your aws cli :
src: http://docs.aws.amazon.com/cli/latest/topic/config-vars.html
cat << "EOF" > ~/.aws/config
And configured your security credentials:
aws elb 描述负载均衡器 --负载均衡器名称“LB_NAME”| grep "实例ID" | awk '{print $2}' | sed 's/\"//g'
aws elb describe-load-balancers --load-balancer-name "LB_NAME" | grep "InstanceId" | awk '{print $2}' | sed 's/\"//g'
首先执行 elb-describe-lbs 来获取负载均衡器及其名称的列表。
然后执行
elb-describe-instance-health
获取该负载均衡器背后的实例列表。 LB_NAME 是 elb-describe-lbs 输出中第二列的值。First do
elb-describe-lbs
to get a list of your load balancers and their names.Then do
elb-describe-instance-health <LB_NAME>
to get a list of instances behind that load balancer. LB_NAME is the value of the 2nd column in the output ofelb-describe-lbs
.您可以将 AWS 命令行工具与一些 bash 管道结合使用:
这将为您提供附加到 ELB 的每个实例的公共 DNS 名称,您可以分别更改 awk 列以获取其他详细信息。
You can use AWS command line tools with some bash piping:
This will give you the public DNS name for every instance attached to the ELB, you can change the awk columns respectively to get other details.