Start, stop, and monitor AWS EC2 instance status using PowerShell

With PowerShell, we can manage just about everything having to do with Amazon Web Services (AWS) Elastic Compute Cloud (EC2) instances. In this article, let's check out how to perform some basic tasks like starting, stopping, and monitoring the status of our AWS EC2 instances.

Before we get too far, I'm going to assume you've already downloaded and installed the AWSPowerShell module available via the PowerShell Gallery (Install-Module -Name AWSPowerShell) and have authenticated to your AWS account.

Using Get-EC2Instance ^

To get started, the first command I'm going to use is the Get-EC2Instance command. I'll run Get-EC2Instance, and this will put the instance object inside that instance variable. Once I have this I can then dive down into it and see what some of the output is. You can see below that the instance's property has the instance ID, instance type, and a lot of other information about that particular instance.

Get EC2Instance

Get EC2Instance

I would now like to start up this EC2 instance. One way to do this is to pass the instance ID to the instance ID parameter on the Start-EC2Instance command, so I need to get the instance ID by itself. To do this, I'll create an instance ID variable.

Now I will run Start-EC2Instance and pass it to the instance ID I'm working with.

To see if this instance is started, I can use the Get-EC2InstanceStatus command, but when I do so, it returns nothing.

However, if I wait just a bit and run Get-EC2InstanceStatus again, you'll see that it does eventually return some output. When it does though, there's nothing really useful.

Get EC2InstanceStatus

Get EC2InstanceStatus

To figure out the status of the EC2 instance, we'll need to drill down into the InstanceState property. This will provide the code and name of the status.

Starting or stopping an EC2 instance ^

We can now determine the status of an EC2 instance, but what if we'd like to stop a running one? We can do this with the Stop-EC2Instance simply by piping the instance Get-EC2Instance returns directly to it.

The same method can start EC2 instances via the Start-EC2Instance command!

Stopping or starting many EC2 instances at once ^

That's all well and good—we can start, stop, and figure out the current status of a single EC2 instance. But what if we've got a lot of these things to control? No problem! We simply need to use Get-EC2Instance to find all the instances and then pass them to the appropriate command.

To find all instances we're looking for, we can use the Filter parameter on Get-EC2Instance. This parameter is a hash table that has a specific syntax, so be aware of this. I only have a single EC2 instance in my test environment, but using the filter can return one or 1,000 instances.

Once I have all the instances, I then just need to pass those instance objects to the Start-EC2Instance or Stop-EC2Instance commands just like I did before.

Waiting for instances to change state ^

You'll find that EC2 instances don't change state automatically. They need time to boot up and shut down. When part of an automation script where other steps depend on this state, it's advantageous to have a way to wait for a particular state and then continue with the rest of your script. We can build a PowerShell function to do just this! I'll call mine Wait-EC2Instance.

You can see below that I'm using the Get-EC2InstanceStatus command to check for the status repeatedly, retrying every five seconds by default.

I can then use this function by simply passing the Get-EC2Instance output directly to it and wait for the state I'm interested in. You can see below that I'm finding all the EC2 instances in my account and am waiting for them all to achieve a running state.

Summary ^

Using PowerShell to manage your EC2 instances is a great way to manage them at scale. With just a few commands in the AWSPowerShell module, you can control all of your EC2 instances via the command line and build some useful scripts as well!

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