问题
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