How-To

Keeping Track of Your AWS EC2 Instances

Having received surprise bills for forgotten instances, Brien Posey creates a PowerShell Script to display them all, regardless of which region they are in.

One of the most frustrating problems that I encounter when working with AWS is that of forgotten EC2 instances. I try to be vigilant about terminating instances when I am done with whatever it is that I happen to be working on, but sometimes I admittedly forget. On more than one occasion I have received a surprise bill because I accidentally left an instance running.

While some of these surprise bills are undeniably due to neglect on my part, there have been other occasions in which I have accidentally left a virtual machine instance running in a different region. Unfortunately, AWS does not provide a GUI-based option for seeing all of your EC2 instances across all of your regions (although you might be able to do it in a roundabout way using tagging). That being the case, I decided to create a PowerShell script that would display all of my EC2 instances, regardless of which region they are located in. Here is the script:

$DefRegion = Get-DefaultAWSRegion
$Regions = aws ec2 describe-regions --all-regions --query "Regions[].{Name:RegionName}" --output text
ForEach ($Region in $Regions){
	Write-Host('Region: ') $Region
	Set-DefaultAWSRegion $Region
	$Instances = (Get-EC2Instance).Instances
	Write-Host $Instances.InstanceID
	}
Set-DefaultAWSRegion $DefRegion

I will explain how the script works in a moment, but first I want to show you what the output looks like. As you can see in Figure 1, the script lists the various AWS regions and displays the instance ID of any EC2 instances that exist in the region. In this case, I have instances running in US-West-1 and US-West-2. The error messages that you see are triggered when the script attempts to query a region for which the current user does not have permission.

Figure 1: This is the output from my script
[Click on image for larger view.] Figure 1: This is the output from my script

The script's first line creates a variable called $DefRegion and sets it to be equal to the default region. This is the region that PowerShell is mapped to right now. The reason why I am doing this is because the script switches from one region to the next as it executes. The last line of the script uses this variable in conjunction with the Set-DefaultAWSRegion cmdlet to set PowerShell back to the region that was being used before the script ran. It's more of a convenience feature than a necessity.

The script's second line creates a variable named $Regions. I am mapping the $Regions variable to the aws ec2 describe-regions command. The command is set up in a way that causes the $Regions variable to become populated with a list of the various AWS region names.

The script's third line sets up a ForEach loop. This loop causes PowerShell to go through the regions in the $Regions list one by one. The loop is constructed in such a way that a variable named $Region (not to be confused with $Regions) holds the name of the region that PowerShell is about to examine.

The loop itself performs four simple tasks. The first of these tasks involves using the Write-Host cmdlet to display the word Region: followed by the name of the region that the script is currently processing.

The loop's next line sets the default AWS region (the region that PowerShell is currently mapped to) to the value stored in the $Region variable. In other words, each time that the loop executes PowerShell points itself toward a different AWS region.

The third thing that the loop does is to create a variable named $Instances. The $Instances variable is mapped to a list of all of the instances that exist within the current region.

Finally, the fourth task is to use the Write-Host cmdlet to display the instance ID for any instances that have been discovered within the region. This command could easily be modified to display additional information about the instances if you want. For my purposes, displaying the instance ID was sufficient.

As you can see, this script is relatively simple, but it has the potential to save you a significant amount of money by helping you to find EC2 instances that you might have forgotten about.

About the Author

Brien Posey is a 22-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.

Featured

Subscribe on YouTube