PowerShell getting AWS instances from all regions

By default, AWS CLI or PowerShell AWS cmdlets only list EC2 resources from your default region. You need to specify a region to see what services are there or a profile created with AWS CLI’s configuration which contains a different region.

Within the AWS Management Console, you can use “EC2 Global View” to list all enabled resources from all regions for an account.

Sometimes you need to use scripts to manage instances, PowerShell comes very handy for this purpose. For example, the one below will list all EC2 instances from all regions.

$regions=Get-EC2Region

foreach ($r in $regions) {
	$rname=$r.RegionName

	
	if ( $i=(Get-EC2Instance -Region $rname).Instances )
	{
		Write-Output "`n$rname"
		$i|format-list
	}
}

Advertisement

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 )

Facebook photo

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

Connecting to %s