Despite the advanced filtering and sorting capabilities in the AWS Console, sometimes you need more flexibility in displaying results.
I discovered this recently when I was trying to figure out why a prior architect had created two VPC subnets in the same availability zone for the same type of instance. There are reasons to do this, of course, especially if your routing requirements are different for the two subnets. But that wasn’t the case here and to make it easier to prove the routing was the same, I wanted a table of all instances in an region with columns for the name tag, subnet and availability zone.
Go ahead: try to do this in the AWS Console. :-). You can get either the subnets or the AZs but not both — at least not easily.
So, I wrote the script below to produce the output I wanted. You could easily modify it to output what you want to see on a per instance basis. I also like changing Out-GridView
to Export-Csv
which produces a file you can use Excel sorting and, even better, Excel filtering on.
As always, I look forward to your comments and feedback.
<# .SYNOPSIS List all instances in region showing containing subnet and AZ .DESCRIPTION Iterates through all instances in a region and display instance name, ID, subnetid and AZ .NOTES Alex Neihaus 2017-04-26 .INPUT None .EXAMPLE ./List-all-instances-in-region-with-subnetid-and-AZ.ps1 .OUTPUTS List of instances in Out-GridView or whatever your favorite format is .NOTES (c) 2017 Air11 Technology LLC -- licensed under the Apache OpenSource 2.0 license, https://opensource.org/licenses/Apache-2.0 Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Author's blog: https://yobyot.com #> Import-Module AWSPowerShell $subnets = Get-EC2Subnet $instances = (Get-EC2Instance).instances $out =@() foreach ($i in $instances) { $iName = ($i.tags | Where-Object -Property key -EQ 'Name').Value $iInstanceId = $i.instanceID $iSubnetID = $i.SubnetID $iAZ = ($subnets | Where-Object -Property subnetid -EQ $isubnetID).AvailabilityZone $o = New-Object -TypeName System.Management.Automation.PSObject -Property ([ordered]@{ 'Instance Name' = $iName; 'InstanceID' = $iInstanceId; 'Subnet' = $iSubnetID; 'AZ' = $iAZ }) $out += $o } $out | Out-GridView
Leave a Reply