AWS CLI commands to get ec2 instance list

Share

There are more than one ways to use amazon web services Web console, boto3 (aws sdk for python), AWS Command Line Interface (AWS CLI).

AWS CLI enables users to connect and interact with amazon web services using commands from their terminal, with minimal configuration steps one can start running commands to implement functionalities equivalent to that provided by Web console.

Steps to install the latest version of AWS Cloud Command Line Interface on Linux operating system. if you are installing CLI on Linux 64bit use below link to download, copy and paste it in your terminal

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

above link will download a compressed installer file, use “unzip” or any other similar command to unzip the  installer package.

unzip awscliv2.zip

above command will unzip the installer and will create directory named “aws” in the present working directory, run the installation command below after unzipping the installer,  this command uses a file  “install” from the newly created aws directory, all the files are installed to  “/usr/local/aws-cli” .

sudo ./aws/install

 

please check the installation by using the command “aws –version” should get output similar to snapshot below if properly installed

Now to configure the AWS CLI installation you need to have Access key ID, Secret access key, these details can be obtained by creating a IAM user ,follow below steps to create a IAM user.

Login to your aws console , search for iam in search bar and click on IAM as shown in the below image

 

 

on the left pane click on users under access management

 

In step-1 add the user and select the access type as required

 

In next step click on Attach existing policies directly and select policy as per the requirement.

Note: By selecting a policy you are giving a set of permissions to the user ,so always grant permissions only which are required to complete a specific task.

click on Next:Tags button on the right side of footer

 

In step-3 add tags if required, tags are use full to organize, track or control access to users

Review the details and click on the Ceate user button

 

In the last step click on the Download.csv save the file, copy Access key id and close.

Now to configure AWS CLI installation run aws configure and enter Access Key Id and secret Access Key  from the csv file downloaded from last step of creating IAM user, enter the  region    and output format json/yaml/text/table.

 

Step-1:How to get details of all the instances ?

aws ec2 describe-instances

above command gives information of all the instances available

describe-instances is a paginated operation, by default the AWS CLI automatically makes multiple calls to return all possible results to create pagination.

Using AWS CLI commands you can get information of a particular  instance  by specifying instance ID or you can get information of a set of instances belonging to a criteria by specifying filters.

 

Step-2: aws cli command to get details of a specific instance using instance id

aws ec2 describe-instances --instance-id i-0238c02f55e024cb5

This command gives information of a specific instance with instance “id i-0238c02f55e024cb5”

 

Let’s understand how to use filters

Filter Name and Values  can be used to return a specific list of information from a describe instance operation.  Here Name is a string , Values is list of strings and can be more than one for a single Name, both the Name and Value are case sensitive.

you can use multiple filters in a single command  they are  joined with an AND (&&)operation , and the command request returns only results that match all of the specified filters.

 If you specify multiple values for single  a filter, the values are joined with an OR(||) , and the request returns all results that match any of the specified values.

Syntax: Name=string,Values=string,string …

Below commands gives information of instances with a specific security group.

 

How to get details of ec2 instances associated with a specific “security group name

In this command “instance.group-name” is a filter Name which should be used when you want to get information of instances with a specific security group name and here we are using “web-ssh”(security gropu name) as a Value for the filter “instance.group-name”.

aws ec2 describe-instances --filters "Name=instance.group-name,Values=web-ssh"

This command gives information of instances with security group name “web-ssh”

 

How to use argument  –query to get specific information of a  ec2 instance

The  another argument that you can use with describe instances command is query.

some times you may need only specific necessary information of instances, “–query”  will extract specific information of instances as per the query parameters given in the command

Lets understand how to  use “–query”.

please observe previous output snapshots to get an idea of output format of “describe-instances”  it has  “Groups”: instance groups and  “Instances”: individual instances under the “Reservations”, argument query will extract the required information from results of “describe-instances” as per the parameters specified in the command

aws ec2 describe-instances --query "Reservations[*].Instances[*].{Instance:InstanceId}"

This command  will search for the information in all(*) the Reservations and in all the Instances and parameter “InstanceId” is used to get only  instance Id’s of all the instances.

 

 

 

How to use  both –filter and –query in a single command

using both “–filter” and “–query” helps you to get specific information of a instance for every particular filter you use.

In this command a filter “instance.group-id” is used which describes instances with a specific security group-id “sg-0ae6105546164429c”,additionally parameter “query” is used to get specific information of all the instances like instance-id, Subnet-Id,
Security-group-name.

aws ec2 describe-instances \
--filter "Name=instance.group-id,Values=sg-0ae6105546164429c" \
--query "Reservations[*].Instances[*].{Instance:InstanceId,Subnet:SubnetId,Vpc:VpcId,SecurityGroups:SecurityGroups[0].GroupName}"

Output:

 

Using filter “tag” to get details of instances associated with specific tags.

Tags can be given to every resource in aws  every tag will have a Key and a Value for each key.

Lets see some commands to get the information of instances using “key/value” combination of a “tag”

Using both key and value of a tag

aws ec2 describe-instances                                                    
--filters "Name=tag:web-servers,Values=server1"
--query "Reservations[*].Instances[*].{Instance:InstanceId,Tags:Tags}"

here “web-server” is a key and “server1” is a value in a tag , this command will give “InstanceId”  and all the tags of the instances with tag-key  webservers and tag-value server1.

Note: “value” for a “key” in tags and “Values” for a “Name” in filters are different.

 

You  can use filter “tag”  with any one of its parameters either with tag-key or tag-value

With tag-key regardless of tag value:

aws ec2 describe-instances \
--filters "Name=tag-key,Values=web-servers" \
--query "Reservations[*].Instances[*].{Instance:InstanceId,Subnet:SubnetId}"

This command will give a output with “InstanceId” and “subnetId” whose
tag-key is web-servers

 

With tag-value regardless of tag key:

aws ec2 describe-instances \
--filters "Name=tag-value,Values=server2" \
--query "Reservations[*].Instances[*].{Instance:InstanceId,Subnet:SubnetId,Vpc:VpcId}"

This command will give instance-Id,subnet-id and vpc-id of instances with tag-value
server2

 

How to get information of instances using “instance types”

To get details of instances using instance type

aws ec2 describe-instances \
--filters "Name=instance-type,Values=t2.nano" \
--query "Reservations[*].Instances[*].{Instance:InstanceId,Subnet:SubnetId,Vpc:VpcId,SecurityGroups:SecurityGroups[0].GroupName}"

above command gives instance-id, subnet-id, vpc-id, security group name of instances whose instance type is “t2.nano”.

 

Using filter “vpc-id” to get information of instances which are in a specific vpc

To get the information of instances using filter “vpc-id”

aws ec2 describe-instances \
--filters "Name=vpc-id,Values=vpc-053a656302897f3de" \
--query "Reservations[*].Instances[*].{Instance:InstanceId,Subnet:SubnetId}"

This command gives instance id’s and subnet id’s of the instances launched
in a specific vpc with id “vpc-053a656302897f3de”.

How to get list of instances based on “state of the instance”

To get list of Stopped instances

aws ec2 describe-instances 
--filters "Name=instance-state-name,Values=stopped"
--query "Reservations[*].Instances[*].{Instance:InstanceId,State:State.Name}"

Here we are using filter “instance-state-name” and “stopped” as a value for it,
this command results in all the instances with “state” stopped.
“InstanceId”, “State.Name” are given as parameters for argument query which gives
final output with instance-id’s of all the stopped instances

 

To get list of  Terminated instances

aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=terminated" \
--query "Reservations[*].Instances[*].{Instance:InstanceId,LaunchTime:LaunchTime,State:State.Name}"

In the above command, filter “instance-state-name” and value “terminated” is used
for it and “InstanceId”, “LaunchTime” are given as parameters to argument “–query”.

This commands gives information all theof terminated instance launch time
and instance id.

How to change  output format of aws cli commands

Aws command line interface supports different types of output formats, you can easily change the output format of your cli command using option –output .

  • json
  • text
  • table
  • yaml

Use option “–output” to get the output in required format  along with type of  output format you want.

Using option “–output” to get output in yaml 

aws ec2 describe-instances 
--filters "Name=instance-state-name,Values=running"
--query "Reservations[*].Instances[*].{Instance:InstanceId,LaunchTime:LaunchTime,State:State.Name,VPC:VpcId,SecurityGroups:SecurityGroups[0].GroupId}" 
--output yaml

In this command filter “instance-state-name” for “Name” and “running” as a “Value”
are used and output format “yaml” are used.

output in yaml

 

Please Contact us if you are looking for cloud support team company for IT Staff augmentation services, Hire dedicated Developer ,Cloud DevOps services. Our Virtual CTO Services  will be delighted to assist you for technology architecture consulting.

Leave a Reply

Your email address will not be published.

*