How can I get list of only running instances when using ec2-describe-tags

a 夏天 提交于 2020-12-29 04:58:17

问题


How can I get list of only running instances when using ec2-describe-tags. I am using my command like this:

ec2-describe-tags --filter "resource-type=instance" --filter "value=somevalue" --filter "key=key"

回答1:


It is easy using the aws-cli:

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId]' --filters Name=instance-state-name,Values=running --output text



回答2:


Expanding on John Rotenstein's answer you can definitely do this with describe-instances.

This will list Name tag and IP of all running instances whose Name tag contains a specific word:

name_tag=mongodb
aws ec2 describe-instances --filter "Name=tag-key,Values=Name" "Name=tag-value,Values=*$name_tag*" "Name=instance-state-name,Values=running" --query "Reservations[*].Instances[*][Tags[?Key=='Name'].Value[],NetworkInterfaces[0].PrivateIpAddresses[0].PrivateIpAddress]" --output text

Output:

10.100.2.10
mongodb0
10.100.2.11
mongodb1
10.100.2.12
mongodb2

A bonus here is that leaving name_tag empty lists all running instances.



来源:https://stackoverflow.com/questions/23936216/how-can-i-get-list-of-only-running-instances-when-using-ec2-describe-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!