Get all AWS EC2 instances across all profiles and regions
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:
- Get all AWS Backup protected resources across all profiles and regions
- Get all AWS Backup recovery points grouped by resource name across all profiles and 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 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:
- Install and configure AWS Tools for PowerShell.
- Log in to all required AWS accounts/profiles.
- Set your IAM permissions to allow for Amazon EC2 access.
PowerShell Cmdlets
This is a list of PowerShell cmdlets we will use.
- 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.
- Get-Culture Gets the current culture set in the operating system.
- Get-EC2Instance Describes the specified instances or all instances.
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
$EC2Instances = Get-EC2Instance Write-Host "EC2 count = $($EC2Instances.Count)" If ($EC2Instances) { $EC2Instances.Instances } # If ($EC2Instances) <# Return # EC2 count = 37 . . GroupNames : {} Groups : {} Instances : {PRD-PICP2} OwnerId : 804446482589 RequesterId : ReservationId : r-0df958c67edf5e313 #> |
Select EC2 Instance’s information
Second, I selected one EC2 instance (-First 1) to fine tune returned information.
1 2 3 4 5 6 7 |
# Get all instances. (https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Instance.html) $EC2Instances.Instances | Select -First 1 <# Return # InstanceId InstanceType Platform PrivateIpAddress PublicIpAddress SecurityGroups ---------- ------------ -------- ---------------- --------------- -------------- i-05b9999e42a4e9999 t3.xlarge Windows 10.10.10.76 {SG_PRD_MONITOR, SG_PRD_FILE,... #> |
Select the required instance’s information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# Get all instances. (https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Instance.html) $EC2Instances = Get-EC2Instance Write-Host "EC2 instances count = $($EC2Instances.Count)" If ($EC2Instances) { $EC2Instances.Instances | Select -First 1 ` InstanceId, LaunchTime, PlatformDetails, PrivateDnsName, ` PrivateIpAddress, PublicDnsName, PublicIpAddress, ` RootDeviceName, RootDeviceType, SubnetId, VpcId } # If ($EC2Instances) <# Return # InstanceId : i-05b9999e42a4e9999 LaunchTime : 10/18/2023 11:23:00 AM PlatformDetails : Windows PrivateDnsName : ip-10.10.10.76.ec2.internal PrivateIpAddress : 10.10.10.76 PublicDnsName : PublicIpAddress : RootDeviceName : /dev/sda1 RootDeviceType : ebs SubnetId : subnet-05b9999e42a4e9999 VpcId : vpc-05b9999e42a4e9999 #> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# Get all instances. (https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Instance.html) $EC2Instances = Get-EC2Instance Write-Host "EC2 instances count = $($EC2Instances.Count)" If ($EC2Instances) { $EC2Instances.Instances | Select -First 1 ` @{N="Name";E={$_.Tag.Value[$_.Tag.Key.IndexOf("Name")]}}, InstanceId, ` @{N="State";E={(Get-Culture).TextInfo.ToTitleCase($_.State.Name)}}, ` LaunchTime, PlatformDetails, PrivateDnsName, ` PrivateIpAddress, PublicDnsName, PublicIpAddress, ` RootDeviceName, RootDeviceType, SubnetId, VpcId } # If ($EC2Instances) <# Return # Name : prd-filesrv01 InstanceId : i-05b9999e42a4e9999 State : Stopped LaunchTime : 10/18/2023 11:23:00 AM PlatformDetails : Windows PrivateDnsName : ip-10.10.10.76.ec2.internal PrivateIpAddress : 10.10.10.76 PublicDnsName : PublicIpAddress : RootDeviceName : /dev/sda1 RootDeviceType : ebs SubnetId : subnet-05b9999e42a4e9999 VpcId : vpc-05b9999e42a4e9999 #> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# Get a list of supported regions. (https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Region.html) $EnabledRegions = (Get-EC2Region -ProfileName $Profile | Sort-Object RegionName).RegionName # For each region: ForEach ($Region in $EnabledRegions) { # Get all instances. (https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Instance.html) $EC2Instances = Get-EC2Instance -Region $Region Write-Host "`t$Region, EC2 instances count = $($EC2Instances.Count)" If ($EC2Instances) { $EC2Instances.Instances | Select -First 1 ` @{N="Region";E={$Region}}, ` @{N="Name";E={$_.Tag.Value[$_.Tag.Key.IndexOf("Name")]}}, InstanceId, ` @{N="State";E={(Get-Culture).TextInfo.ToTitleCase($_.State.Name)}}, ` LaunchTime, PlatformDetails, PrivateDnsName, ` PrivateIpAddress, PublicDnsName, PublicIpAddress, ` RootDeviceName, RootDeviceType, SubnetId, VpcId } # If ($EC2Instances) } # ForEach ($Region <# Returned # Region : us-west-2 Name : prd-filesrv01 InstanceId : i-05b9999e42a4e9999 State : Stopped LaunchTime : 10/18/2023 11:23:00 AM PlatformDetails : Windows PrivateDnsName : ip-10.10.10.76.ec2.internal PrivateIpAddress : 10.10.10.76 PublicDnsName : PublicIpAddress : RootDeviceName : /dev/sda1 RootDeviceType : ebs SubnetId : subnet-05b9999e42a4e9999 VpcId : vpc-05b9999e42a4e9999 #> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# Save result in a CSV file $CsvFile = "D:\Temp\EC2InstancesInfo.csv" # Get the names of all CredentialProfiles saved in local storage. $Profiles = (Get-AWSCredential -ListProfileDetail | Where-Object {$_.ProfileName -notlike "default"} | Sort-Object ProfileName).ProfileName # Initialize output. $EC2InstancesInfo = @() # For each profile: ForEach ($Profile in $Profiles) { "*** " + $Profile # Get a list of supported regions. (https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Region.html) $EnabledRegions = (Get-EC2Region -ProfileName $Profile | Sort-Object RegionName).RegionName # For each region: ForEach ($Region in $EnabledRegions) { # Get all instances. (https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Instance.html) $EC2Instances = Get-EC2Instance -ProfileName $Profile -Region $Region Write-Host "`t$Region, EC2 instances count = $($EC2Instances.Count)" If ($EC2Instances) { $EC2InstancesInfo += $EC2Instances.Instances | Select @{N="Profile";E={$Profile}}, ` @{N="Region";E={$Region}}, ` @{N="Name";E={$_.Tag.Value[$_.Tag.Key.IndexOf("Name")]}}, InstanceId, ` @{N="State";E={(Get-Culture).TextInfo.ToTitleCase($_.State.Name)}}, ` LaunchTime, PlatformDetails, PrivateDnsName, ` PrivateIpAddress, PublicDnsName, PublicIpAddress, ` RootDeviceName, RootDeviceType, SubnetId, VpcId } # If ($EC2Instances) } # ForEach ($Region } # ForEach ($Profile # Display the result #$EC2InstancesInfo # Save result to a CSV file $EC2InstancesInfo | Sort-Object Profile, Region, Name | Export-Csv -NoTypeInformation -Path $CsvFile |
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.
There's no comments