Start or stop EC2 instances with one line of PowerShell

Start an EC2 instance in PowerShellSure, you can start or stop an EC2 instance in the AWS console by just searching for its name and click, click, click, get it done.

But it’s much more manly (or womanly) to start the instance via a command, which in my case means an AWS PowerShell cmdlet. The rub is that to start an instance using the AWS API, you have to supply the unique EC2 instance ID. When you are working in the console, you are probably thinking in instance names, not instance IDs. But in a cmdlet talking to the AWS API, logical names would be imprecise.

So, as long as you are sure your instance name tags are unique, this one-liner does the trick.

( (Get-EC2Instance | ? { $_.instances.tag.value -match "YOUR NAME TAG HERE" } ).Instances).InstanceID | Start-EC2Instance

And no surprise, you can stop it the same way.

( (Get-EC2Instance | ? { $_.instances.tag.value -match "YOUR NAME TAG HERE" } ).Instances).InstanceID | Stop-EC2Instance

 


Posted

in

, ,

by

Tags:

Comments

Leave a Reply

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