Display EC2 instance attributes in a table

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://www.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

 


Posted

in

, ,

by

Tags:

Comments

4 responses to “Display EC2 instance attributes in a table”

  1. Colin Chambers Avatar

    I added a bit more which retrieves the “name” tag from the subnet and displays it in the table, just we have lots of named subnets (web-AZA, data-AZB, App-AZA etc) and I found it to be easier to read than just having the subnet ID’s 

     

    $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
        $CurrentSubnet = $subnets | Where-Object { $_.SubnetId -eq $iSubnetID }
        $iSubnettags = $currentSubnet.Tag | Where-Object { ($_.key -eq "Name")  } | select -ExpandProperty Value
            $o = New-Object -TypeName System.Management.Automation.PSObject -Property ([ordered]@{
                'Instance Name' = $iName;
                'InstanceID' = $iInstanceId;
                'Subnet' = $iSubnetID;
                'SubnetName' = $iSubnettags;
                'AZ' = $iAZ;
            })
        $out += $o
    }
    $out | Out-GridView

     

    1. Alex Neihaus Avatar
      Alex Neihaus

      Nice. I hope it helps other users.

      Thanks.

  2. Colin Chambers Avatar

    Error on line 45, should read 

     $iSubnetID = $i.SubnetID

    not

     $iSubnetID = $i.iSubnetID

    This error breaks columns 3 and 4 in the output window.  replace and works a treat, thanks!

    1. Alex Neihaus Avatar
      Alex Neihaus

      Thank you very much, Colin.

      Appreciate the code review!

      I fixed the code in the post.

      Alex

Leave a Reply

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