AWS
EC2
Cloud Computing
Multi-Region
Infrastructure Management

How to see all running Amazon EC2 instances across all regions?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Amazon Elastic Compute Cloud (EC2) is a core part of Amazon Web Services (AWS) that offers resizable compute capacity in the cloud. Managing and monitoring EC2 instances is a routine task, especially in extensive AWS environments with multiple accounts and instances spread across different regions. This guide explores methods to view all running EC2 instances across all AWS regions, providing technical steps and examples for clarity.

Understanding the Problem

By default, the AWS Management Console displays EC2 instances only within the selected region. However, large organizations often have instances distributed across multiple regions for redundancy, localization, or performance optimization. Therefore, it's essential to have a centralized way to view all instances to maintain operational oversight and optimize costs.

Method 1: AWS CLI

The AWS Command Line Interface (CLI) is a powerful tool for interacting with AWS services from your terminal. It can be used to list EC2 instances across all regions.

Step-by-Step Process

  1. Install AWS CLI: Make sure you have the AWS CLI installed. You can download it from the AWS CLI official page.
  2. Configure AWS CLI: Before using the AWS CLI, configure it with your AWS account credentials:
bash
    aws configure

You will be prompted to enter your AWS Access Key ID, Secret Access Key, default region, and output format.

  1. List Available Regions: Use the following command to list all AWS regions:
bash
    aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text
  1. List Instances in All Regions: Iterate over each region and retrieve its EC2 instances. Here's a shell script example that demonstrates this process:
bash
1    #!/bin/bash
2
3    regions=$(aws ec2 describe-regions --query "Regions[].{Name:RegionName}" --output text)
4
5    for region in $regions
6    do
7        echo "Checking region: $region"
8        aws ec2 describe-instances --region $region --query "Reservations[].Instances[].[InstanceId, InstanceType, State.Name, Tags[?Key=='Name'].Value|[0], PublicIpAddress]" --output table
9    done

Example Output

This command provides a tabular output comprising instance IDs, types, states, instance names, and public IP addresses.

Method 2: AWS Management Console

  1. Access EC2 Global View: AWS provides a unified EC2 global view feature that enables you to view instances across regions directly from the AWS Management Console.
  2. Use AWS Resource Groups: Create a resource group in the AWS Management Console to filter resources (like EC2 instances) across multiple regions.
  3. Enable Organization View: For organizations using AWS Organizations, you can opt for consolidated billing and use the management account to view resources across all member accounts and regions.

Method 3: AWS SDKs

AWS Software Development Kits (SDKs) for languages like Python (Boto3), Java, and Node.js provide programmatic access to AWS. This can be particularly useful for automated reports or integrations into other applications.

Python Example

Here's how you might use Boto3, the AWS SDK for Python, to list instances:

python
1import boto3
2
3def list_instances():
4    client = boto3.client('ec2')
5    regions = [region['RegionName'] for region in client.describe_regions()['Regions']]
6
7    for region in regions:
8        print(f"Region: {region}")
9        ec2 = boto3.client('ec2', region_name=region)
10        response = ec2.describe_instances()
11        for reservation in response['Reservations']:
12            for instance in reservation['Instances']:
13                print(f"Instance ID: {instance['InstanceId']}, State: {instance['State']['Name']}, Type: {instance['InstanceType']}")
14
15list_instances()

Key Considerations

  • IAM Permissions: Ensure the IAM user or role has adequate permissions to list EC2 instances using AWS CLI, SDK, or console.
  • Cost and Resource Management: Seeing all instances aids in better cost management and operational adjustments.
  • Cross-Account Visibility: For a complete organizational view, include cross-account capabilities using AWS Organizations or IAM roles with assume role functionalities.

Summary Table

MethodTools/CommandsFeatures
AWS CLIaws ec2 describe-instancesDirect, programmatic CLI-based access
AWS Management ConsoleEC2 Global ViewUser-friendly, no coding required
AWS SDKsBoto3 for Python, AWS SDK for Java/Node.jsIntegrations into applications

These methods collectively offer comprehensive coverage for monitoring and managing AWS EC2 instances across multiple regions and accounts. Understanding and applying them effectively enhances operational efficiency in AWS environments.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.