aws

Howto list all instances in all regions from mutliple accounts using awscli – AWS

AWS Cloud spans 69 Availability Zones within 22 geographic regions around the world, with announced plans for 9 more Availability Zones and three more Regions in Cape Town, Jakarta, and Milan.

If you are using more than one region it takes much time to browse through all regions in a browser and check which instances are running.

To save time, we are using awscli command in a shell script which will list all instances from all regions. You can use multiple profile names.

 

You can specify multiple profile names as follows:

#!/bin/bash
for i in profile1 profile2
do
    OWNER_ID=`aws iam get-user --profile $i --output text | awk -F ':' '{print $5}'`
    tput setaf 2;echo "Profile : $i";tput sgr0
    tput setaf 2;echo "OwnerID : $OWNER_ID";tput sgr0
    for region in `aws --profile $i ec2  describe-regions --output text | cut -f4`
    do
        tput setaf 1;echo  "Listing Instances in region $region";tput sgr0
        aws ec2 describe-instances --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value , InstanceId]' --profile $i --region $region --output text
    done &
done
wait

This will run jobs in parallel and exit when all jobs are completed.

2 thoughts on “Howto list all instances in all regions from mutliple accounts using awscli – AWS

  1. Warning: Attempt to read property "entry" on string in /var/app/current/wp-content/plugins/jetpack/modules/gravatar-hovercards.php on line 190 Warning: Trying to access array offset on value of type null in /var/app/current/wp-content/plugins/jetpack/modules/gravatar-hovercards.php on line 190 Warning: Attempt to read property "displayName" on null in /var/app/current/wp-content/plugins/jetpack/modules/gravatar-hovercards.php on line 191
    Harera Radj says:

    Hi Akhil, how do profile1, and profile2 look like?

Leave a Reply

Your email address will not be published. Required fields are marked *