AWS EC2 – Get all EC2 instances in all profiles and all regions

By Saad Khamis

November 27, 2023

15767 views

In this blog I will share one way to get all AWS EC2 instances in all profiles and all regions including instance’s Id, name, state, private & public IPs and other instance information. I will show how to calculate the instance’s name and state (running, stopped, etc.) The result can be displayed or saved to a file.

Other blogs I wrote that may interest you:

AWS Backup – List all protected resources in all profiles and all regions

Introduction

Within the AWS Management Console, you can use EC2 Global View to list all enabled resources from all regions for an account. What I needed is a list of all EC2 instances in all regions and all profiles or accounts. I also needed to know the state of each EC2 instance. Hence, using PowerShell and AWS Tools for PowerShell to accomplish this task.

Prerequisites

To run the EC2 commands in this blog, you need to:

  1. Install and configure AWS Tools for PowerShell.
  2. Log in to all required AWS accounts/profiles.
  3. Set your IAM permissions to allow for Amazon EC2 access.

PowerShell Cmdlets

This is a list of PowerShell cmdlets we will use.

  1. Get-AWSCredential Returns an AWSCredentials object initialized with from either credentials currently set as default in the shell or saved and associated with the supplied name from the local credential store.
  2. Get-Culture Gets the current culture set in the operating system.
  3. Get-EC2Instance Describes the specified instances or all instances.
  4. Get-EC2Region Describes the Regions that are enabled for your account, or all Regions.

Get EC2 Instances

First, I used Get-EC2Instance cmdlet to get available EC2 instances.

Select EC2 Instance’s Information

Second, I selected one EC2 instance (-First 1) to fine tune returned information.

Select the required instance’s information.

Get EC2 Instance’s Name and State

Third, I calculated instance’s name, included in the instance’s Tag, and state, in the instance’s State.

Get all EC2 Instances in all Supported Regions

Fourth, I got a list of supported regions and executed the above commands for each region. Note, I added the Region name to the cmdlet output.

Get all EC2 Instances in all Supported Regions for all Profiles

Last, I got a list of all profiles and executed the above commands for each profile. Note, I added the Profile name to the cmdlet output and I used $EC2InstancesInfo to save the result.

Conclusion

Can you believe it? You’ve made it to the end of this blog. The code may be challenging, difficult or lengthy but you have made it. Congratulations.

I used PowerShell and AWS Tools for PowerShell to get a consolidated list of all EC2 instances in all regions for all profiles. The result can be saved in a CSV file.

Did you find this blog easy to follow and helpful to you? I certainly would love to hear your feedback and suggestions. So, let me know in the comments below. Happy PowerShelling.

Disclaimer

Purpose of the code contained in blog is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.

Comments

There's no comments

Leave a Reply

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

Related Posts

Windows – Get all installed patches, updates and hotfixes

By Saad Khamis 20323 views October 25, 2023

Azure – Get Storage Account Lifecycle Management Policy Rules

By Saad Khamis 33902 views October 12, 2023

Windows – Find built-in administrator account name and disable it

By Saad Khamis 33654 views December 14, 2022

Azure SQL Server: Get Azure SQL Server Firewall Rules

By Saad Khamis 35471 views August 25, 2022