1 minute read

June 2019

Overview

Today I needed to go through and check the details of EC2 instances and the AMI they were created from for an audit. You can get this information via the AWS console, however this is laborious and I wanted to share the results with others so used PowerShell and the AWS PowerShell Module to get the required information and save it to a CSV file.

AWS console showing ami name

The Script

I use an EC2 filter to get only running instances, then create a new list ($noAgentList) of instances that do not have the name like TeamCityAgent (I am not interested in these instances).

Next I create an ordered hashtable to store the properties that are required. I use the ami id to get the AMI Name.

Note: The AMI name may not be available, I found that older instances that used Windows didn’t always have a an AMI name and in the console it was unavailable too.

Once the hashtable is created, a custom object is created with the properties made up of the hashtable values and saved to the list variable. This is done for each of the objects in the no agent list.

Below we can see the output in the PowerShell terminal for the same instance in the console screenshot above.

Custom PowerShell object output with ami name

For the last step, I sort the instance details by ami image name and save them to a CSV file.

EC2 instance details saved to csv

Summary

Working with EC2 instances and outputting the required data takes a bit extra work but once you get the hang of it, you can use it to extract information quickly and easily and build useful reports of the details of the EC2 instances. This script could be modified to get other details such as volume attachment information, snapshot dates and be run across different AWS regions.