Scheduled Powerdown of Ec2 instances

After working on the Aws report I decided to start working on using AWS tags to control a script.

There are plenty of ways to stop and start Ec2 instances that don’t need tags. You can use Data pipe lines and do the same thing with an array and a scheduled tasks, but it wouldn’t auto-scale, etc.

This ended up being a fun way to mess around with Tags and reading tags. I would in no way recommend anyone use tags to the same extend I did to manage there EC2 instances. With great tools like Jenkins, and lamda functions there are far more sane way to go about this.

A simple tag detect and power off script looks a lot like my previous report script:

# AWS PowerOn/Off Schedular
# Tag Name: Power_Options
# Toggle power on and off script.
# Requires AWS plugin:
# Set-AWSCredentials -AccessKey -SecretKey -Storeas # Full instructions found

#profiles to check
$profile_list = (“Example_1”)

#Just a quick check for powershell 3.0 and older changes the method of plugin loading.
if($PSVersionTable.PSVersion.Major -ge 4) {
Import-Module AWSPowerShell
} else {
Import-Module “C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1”

#Actual Engine.
#Parses through profiles and regions.
foreach($profile in $profile_list) {
Set-AWSCredentials -ProfileName $profile
$region_list = Get-AWSRegion | select -expandproperty Region

foreach($region in $region_list) {
$Instance_list = Get-EC2Instance -region $region |select -expandproperty instances

$VPC_list = Get-EC2Vpc -Region $region
foreach ($VPC in $VPC_list) {
$Instance_list | Where-Object {$_.VpcId -eq $VPC.VpcId} | foreach-object {
$Instance_name = ($_.Tags | Where-Object {$_.Key -eq ‘Name’}).Value
$power_Action = $NULL
if($Power_Options = ($_.Tags | Where-Object {$_.Key -eq ‘Power_Options’}).Value) {
if($_.State.Name -like “running”) {
stop-EC2Instance -InstanceId $_.InstanceId -Region $region
} else {
Start-EC2Instance -InstanceId $_.InstanceId -Region $region

The above script can be found on Paste Bin or on my github

But with 256 characters to use in a tag you can do much better than turn an already on an instance off or an off instance on. I got a bit carried away and built a multi-delimited tag system based around powering on and off systems and notifying users. The tag system accepts abbreviations of days “Mon, Tue, Wed, Thu, Fri, Sat, Sun” Hours entered in 24 hour format 6,7,8 etc for AM. and 18,19,20 etc for PM. This creates a pretty horrific tag for a human to read. A Monday through Friday Power on and off Schedule with emails sent for each action looks like this:

The format being {command}{coma}{scheduled day}{colon}{scheduled hour}{coma}{semi colon to end command}. The commands can repeat for multiple shut downs etc. I added a few “short cut” times in order to make the schedule readable by humans “weekend” checks Saturdays and Sundays only, Weekday checks for Monday through Friday, and Allweek is everyday.

The difficulty in reading isn’t really an issue as I plan on this format being generated and read by other scripts, as part of a larger management system. That system will be used to notify a team or person they are going to shutdown a vm or a group of Vms that do not need to be used after work hours. The notification will also provide a link that allows the shutdown to be canceled for the day, or for the Vms to be powered back on. I’ll do that section in PHP. The PHP will largely be a front end/user input setup.

The main driver for this is EC2 instances cost money while they are running, so shutting down unneeded instance for a few hours a day can add up. Some examples that came to mind for use were: a call center open from 6 am to 6 pm may not need a reporting server online from 7:00 pm till 5 am. Maybe a company doesn’t need a Staging or POC environment running over the weekend.

The larger segment of script using the tags can be found on paste bin or on my github
I don’t like how out of control scheduling got with the tag delimiter I wrote; though I would still use tags in this manner for things like adding monitoring. An example that comes to mind would be using Ichinga2 monitor with the groups listed such as: Ichinga2:LAMP_standard,Autobouncer add those tags to an autoscaling group to add the correct tags to my ec2 instances and Ichinga can grow and shrink with the environment without human intervention.

Well that is it for today. I’ll post again soon with the completion of my notifying/canceling shutdown system.

Sharing is caring!

Leave your comment

10 + 13 =