How to Access EC2 Instances From PowerShell
Brien Posey, who frequently writes about various techniques for using PowerShell to manage Hyper-V virtual machines, demonstrates how the task automation and configuration management framework can also be used to manage AWS EC2 instances.
In my Posey's Tips and Tricks blogs, I frequently write about various techniques for using PowerShell to manage Hyper-V virtual machines. What you might not realize however, is that PowerShell can also be used to manage AWS EC2 instances. In fact, with enough creativity and coding skills there are even ways of managing Hyper-V and EC2 VMs side by side using a PowerShell script. Side-by-side Hyper-V and EC2 management is another topic for another blog. For now, I want to show you how to access your EC2 instances from PowerShell.
When it comes to managing EC2 instances through PowerShell, there are two main things that you need to know. First, PowerShell is not natively capable of managing EC2 instances. Just as a module is required for managing Hyper-V VMs, a module is also required for managing EC2 instances. You can get everything that you need by installing the AWS Tools for Windows (note: clicking on link may initiate download).
The other important thing that you need to know is that once you have installed the AWS Tools for Windows, the Hyper-V specific PowerShell cmdlets do not magically begin working with your EC2 instances. There is an entirely different set of cmdlets used for managing EC2. For example, you can use the Get-VM cmdlet to retrieve a list of your Hyper-V VMs. However, if you want to retrieve a list of your EC2 instances, you will need to use the Get-EC2Instance cmdlet.
Before I show you an example, I need to point out that the AWS specific cmdlets are not available through the default PowerShell window. When you install the AWS Tools for Windows, the installer will create a PowerShell for AWS environment that can be accessed from the Amazon Web Services section of the Windows Start menu, as shown in Figure 1. Keep in mind that you will need to provide PowerShell with your AWS credentials before anything that I am about to show you will work.
As previously noted, the cmdlet used to retrieve a list of EC2 instances is Get-EC2Instance. You can see the information returned by this cmdlet in Figure 2.
As you look at the figure above, you will probably notice that PowerShell isn't really telling you very much about the instances. If you were examining a Hyper-V VM, you could append the Select-Object * cmdlet to the Get-VM cmdlet as a way of acquiring all of the available information. Unfortunately appending the Select-Object * cmdlet to the Get-EC2Instance cmdlet does not cause PowerShell to provide much more information than what you see in the figure above.
If you want to use PowerShell to retrieve detailed information about a specific ECC2 instance then you will need to know the instance ID. To get a list of your EC2 instance IDs, use this command:
(Get-EC2Instance).Instances | Select-Object InstanceID
Once you know the instance ID for the instance that you want to examine, you can use that instance ID in conjunction with the Get-EC2Instance cmdlet to retrieve additional information about the instance. Here is an example of such a command:
(Get-EC2Instance -InstanceID <instance ID>).Instances
You can see an example of this command being used in Figure 3.
Now you can use the Select-Object * command to retrieve all of the available attributes. You can see what this looks like in Figure 4.
Although you can append an asterisk to the Select-Object cmdlet to force PowerShell to display all of the available attributes, you can just as easily focus on specific attributes. If for example, you wanted to see an instance's platform, architecture, and VPC, you could use this command:
(Get-EC2Instance -InstanceID ).Instances | Select-Object Platform, Architecture, VPCID
Although it is handy to be able to retrieve virtual machine attributes through PowerShell, the techniques that I have shown can be used as building blocks for advanced scripts for managing cross platform virtualized environments.
Brien Posey is a 19-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.