AWS AMI discovery with PowerShell

Using PowerShell is one way of finding available Amazon Machine Images (AMIs). The two main cmdlets Amazon's AWSPowerShell.NetCore module provides are Get-EC2Image and Get-EC2ImageByName. Get-EC2Image uses the powerful type Amazon.EC2.Model.Filter to allow detailed filtering. By looking at the Amazon Web Services (AWS) documentation, you can see the many filtering options available. In this article, we will look into some of these and will also show a function I've created to simplify the AMI search.

AMIs are virtual appliances used to create instances in AWS. The best way to describe an AMI is as a template used to create a virtual machine in Amazon's Elastic Compute Cloud (EC2). A single AMI can serve to launch one or thousands of instances. There are many choices when it comes to finding an AMI, ranging from vendors, the public, and Amazon themselves to name a few.

Get-EC2ImageByName ^

But let's start with Get-EC2ImageByName. This cmdlet enables you to filter a list of Amazon's Windows AMIs based on the type of server configuration you require. Running the cmdlet without adding any parameters outputs the whole list. Adding one of the return name values will query EC2 to find the latest available image:

Searching the image by name

Searching the image by name

The information returned is helpful. It provides the creation date, which does change due to new features and security enhancements. It also shows the ImageId used to build the EC2 instance. These images can change depending on region, so it's worth being aware of this.

Get-EC2Image ^

The Get-EC2Image cmdlet retrieves a list of AMIs you can use. The AMI images available include public and private ones (which you have permissions to). Vendors also put custom AMIs in this space. As mentioned earlier, Get-EC2Image uses the Amazon.EC2.Model.Filter type. A hash table makes up the filter. The name option gives plenty of choices. Here's a snippet from the AWS documentation.

Available options to filter AMIs

Available options to filter AMIs

Here is a simple example. I've built the filter with a single hash table:

Now we can just run this on its own with the Get-EC2Image cmdlet:

The code returns a count of 654 images! By adding more to the search, we can shorten our return.

To add multiple hash tables to the query, you need to contain them inside an array. The filter values inside the hash table allow the use of wildcards. In the above example, I've also added the Region parameter to the cmdlet. This will narrow the search results returned.

My Get-EC2Ami function ^

To make this process a bit more dynamic, I created the function below:

The function requires three parameters: the type of image you are looking for (wildcards are available to use), the region of the image, and the owner. There are a few different owners, so it's worth referring to the link I provided earlier.

Here is a working example of the function I created:

Results from using the Get EC2Ami function

Results from using the Get EC2Ami function

Selecting the name, description, and ImageId gives a clear return of the AMI image. Using the ImageId allows you to create a new EC2 instance.

Summary ^

Amazon provide a vast number of AMIs to use and work with, using either Amazon's own, public, private, or vendor images. The available cmdlets Get-EC2ImageByName and Get-EC2Image deliver an excellent way to search and find the image you need.

Join the 4sysops PowerShell group!

Your question was not answered? Ask in the forum!


Leave a reply

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


© 4sysops 2006 - 2020


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account