Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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" 
like image 768
user1788294 Avatar asked May 29 '14 14:05

user1788294


People also ask

How do I get a list of EC2 instances?

Go to the instances section and click on "instances". It will show you all the running instances in the select region.

How do I know which EC2 instances are under utilized?

01 Sign in to the AWS Management Console. 02 Navigate to EC2 dashboard at https://console.aws.amazon.com/ec2/. 03 In the navigation panel, under INSTANCES section, choose Instances. 04 Select the underused EC2 instance that you want to resize (see Audit section part I to identify the right resource).

What command allows you to list and describe the instances deployed to the cloud?

You can use the AWS Command Line Interface (AWS CLI) to launch, list, and terminate Amazon Elastic Compute Cloud (Amazon EC2) instances.


2 Answers

It is easy using the aws-cli:

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId]' --filters Name=instance-state-name,Values=running --output text 
like image 63
John Rotenstein Avatar answered Sep 22 '22 08:09

John Rotenstein


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.

like image 22
jonatan Avatar answered Sep 21 '22 08:09

jonatan