Get all AWS EC2 instances across all profiles and regions

By Saad Khamis

November 27, 2023

33196 views

In this blog I will share one way to get all AWS EC2 instances across all profiles and 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:

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 across all profiles and regions. 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 across 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 across all profiles and supported regions

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 across all profiles and regions. 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

List all AWS Certificate Manager certificates

By Saad Khamis 14091 views July 31, 2024

Get all AWS EBS snapshots across all profiles and regions

By Saad Khamis 18449 views June 26, 2024