List EC2 instances in all Regions with boto3

This helps get a 10,000 foot view of any EC2 instances running across all EC2 regions.

import boto3

#list regions
client = boto3.client('ec2')
regions = [region['RegionName'] for region in client.describe_regions()['Regions']]

print("Listing EC2 instances for each region....\n")

for i in regions:
    print()
    print(i)
    print('---------')
    Region = i
    client = boto3.client('ec2', region_name=i)
    response = client.describe_instances()
    for reservation in response["Reservations"]:
        for instance in reservation["Instances"]:
            print(instance["InstanceId"])

Output looks like this:

~ $ python3.7 list_ec2_instances_in_all_regions.py
Listing EC2 instances for each region....

eu-north-1
---------

ap-south-1
---------

eu-west-3
---------

eu-west-2
---------

eu-west-1
---------

ap-northeast-2
---------

ap-northeast-1
---------

sa-east-1
---------

ca-central-1
---------

ap-southeast-1
---------

ap-southeast-2
---------

eu-central-1
---------

us-east-1
---------
i-0336a147dc021b4d7

us-east-2
---------

us-west-1
---------
i-0dcccceea80e060e6
i-05b88a70f526dd8e8

us-west-2
---------
~ $

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s