List all EC2 Instances from all regions | Arkit

Amazon Web Services gives the ability to create multiple instances in multiple regions, around 16 regions you may create instances in one of the regions or other regions. If you would like to see/verify how many instances you have then login to AWS console and switch to each and every region EC2 Dashboard. AWS List All Instances In ALL Regions using AWS CLI script.

AWS List All Instances In All Regions

First install and configure AWS cli then run shell script.

RHEL / Centos

# yum install python python-pip python-wheel python3

above command installed python and pip packages

# pip --version
# python --version
# curl -O https://bootstrap.pypa.io/get-pip.py
# python get-pip.py --user

# export PATH=~/.local/bin:$PATH
# source .bashrc
# pip install awscli --upgrade --user
# aws --version
aws-cli/1.16.73 Python/2.7.5 Linux/3.10.0-693.21.1.el7.x86_64 botocore/1.12.63

AWS CLI installed successfully. Let’s configure AWS CLI to use scripting method

Login to AWS Console

Services > Security, Identity, & Compliance > IAM > Users

  • Add User
AWS Add user

AWS Add user

Must select programmatic access otherwise you can’t run scripts

Click Next: Permissions

IAM policy

Click Next: Tags

Add tags if you want Click Next: Review

Create User

download access key AWS

Download CSV File and keep it ready

AWS CLI Configuration

Login to your linux machine and run below command

# aws configure
AWS Access Key ID [None]: BKIATRWCGXNR2IQLVLVQ
AWS Secret Access Key [None]: Aljasdoifhasdneur8972934asndfhasdfo
Default region name [None]: us-east-2
Default output format [None]: json

Copy and paste below script

#!/bin/bash
for region in `aws ec2 describe-regions --output text | cut -f3`
do
echo -e "\nListing Instances in region:'$region'..."
aws ec2 describe-instances --query "Reservations[*].Instances[*].{IP:PublicIpAddress,ID:InstanceId,Type:InstanceType,State:State.Name,Name:Tags[0].Value}" --output=table --region $region
done

it will list out all the instance details

Listing Instances in region:'us-east-2'...
-----------------------------------------------------------------------------------------
| DescribeInstances |
+----------------------+---------------+-------------------------+----------+-----------+
| ID | IP | Name | State | Type |
+----------------------+---------------+-------------------------+----------+-----------+
| i-ad9de230a8b1a8657 | 14.69.148.22 | Server-Computer-Group | running | t2.micro |
+----------------------+---------------+-------------------------+----------+-----------+

That’s it you successfully listed all AWS instances from all regions.

Related Articles

Thanks for your wonderful Support and Encouragement