ec2 instance
15 Apr
15Apr

Aws EC2 cli are useful if you need to view or modify your existing running  ec2 instances, if you want to avoid console or when using script.

In this guide i'll try to show some useful examples.

first you'll need to install and configure aws-cli .

once install check that it works with 'aws --version'

you should get output like that:

"aws-cli/1.32.59"


next will also use jq to filter the output, you can install it from here

let's jump straight into some examples:

1) list all ec'2 in table include Private ip address, OS, Subnet, Availability zone

aws ec2 describe-instances \
 --filters Name=tag-key,Values=Name \
 --query 'Reservations[*].Instances[*].{Instance:InstanceId,IpAddress:PrivateIpAddress,Subnet:SubnetId,AZ:Placement.AvailabilityZone,Name:Tags[?Key==`Name`]|[0].Value,OS:PlatformDetails}' \
 --output table 

the output will look like(data hidden from security reason):

if you need to sort by OS for example, add 

(head -n 2  && sort -t '|' -k5) #to ignore the first title row

so the command should look like:

aws ec2 describe-instances \
 --filters Name=tag-key,Values=Name \
 --query 'Reservations[*].Instances[*].{Instance:InstanceId,IpAddress:PrivateIpAddress,Subnet:SubnetId,AZ:Placement.AvailabilityZone,Name:Tags[?Key==`Name`]|[0].Value,OS:PlatformDetails}' \
 --output table | (head -n 2  && sort -t '|' -k6)


Comments
* The email will not be published on the website.