Auto starting and stopping an EC2 instance at a given time

Posted by on Jan 13, 2014 in AWS, Cloud

This blog post covers the process of automatically starting and stopping an EC2 instance at a given point in time. In my case I needed to spin up an instance, do some work and then shut it down afterwards. This is perfectly suited to AWS and cloud computing and typifies the ethos of elastic scaling and capacity-on-demand. In order to start/stop an instance you will need to make use of the AWS Auto Scaling capabilities which are both straight-forward but very, very powerful.

The first thing that you’ll need to do is set up the Auto scaling tools. Amazon have an excellent step-by-step guide posted here

If done correctly you should be able to open a new command prompt (or powershell terminal) and type

You should get a listing of all auto-scaling commands:

Command Name Description
------------ -----------
as-attach-instances Attaches Instances to Auto Scaling group
as-create-auto-scaling-group Create a new Auto Scaling group.
as-create-launch-config Creates a new launch configuration.
as-create-or-update-tags Create or update tags.
as-delete-auto-scaling-group Deletes the specified Auto Scaling group.
as-delete-launch-config Deletes the specified launch configuration.
as-delete-notification-configuration Deletes the specified notification configuration.
as-delete-policy Deletes the specified policy.
as-delete-scheduled-action Deletes the specified scheduled action.
as-delete-tags Delete the specified tags
as-describe-account-limits Describes limits for the account.
as-describe-adjustment-types Describes all policy adjustment types.
as-describe-auto-scaling-groups Describes the specified Auto Scaling groups.
as-describe-auto-scaling-instances Describes the specified Auto Scaling instances.
as-describe-auto-scaling-notification-types Describes all Auto Scaling notification types.
as-describe-launch-configs Describes the specified launch configurations.
as-describe-metric-collection-types Describes all metric colle... metric granularity types.
as-describe-notification-configurations Describes all notification...given Auto Scaling groups.
as-describe-policies Describes the specified policies.
as-describe-process-types Describes all Auto Scaling process types.
as-describe-scaling-activities Describes a set of activit...ties belonging to a group.
as-describe-scheduled-actions Describes the specified scheduled actions.
as-describe-tags Describes tags
as-describe-termination-policy-types Describes all Auto Scaling termination policy types.
as-disable-metrics-collection Disables collection of Auto Scaling group metrics.
as-enable-metrics-collection Enables collection of Auto Scaling group metrics.
as-execute-policy Executes the specified policy.
as-put-notification-configuration Creates or replaces notifi...or the Auto Scaling group.
as-put-scaling-policy Creates or updates an Auto Scaling policy.
as-put-scheduled-update-group-action Creates or updates a scheduled update group action.
as-resume-processes Resumes all suspended Auto... given Auto Scaling group.
as-set-desired-capacity Sets the desired capacity of the Auto Scaling group.
as-set-instance-health Sets the health of the instance.
as-suspend-processes Suspends all Auto Scaling ... given Auto Scaling group.
as-terminate-instance-in-auto-scaling-group Terminates a given instance.
as-update-auto-scaling-group Updates the specified Auto Scaling group.
version Prints the version of the CLI tool and the API.

For help on a specific command, type ' --help'

Getting started

Now you can start implementing auto scaling.

Auto scaling on a schedule requires a number of components which form the what, where and when to scale:
1. A launch configuration (the ‘what’)
2. A scaling group (the ‘where’)
3. A schedule policy (the ‘when’)

1. [WHAT] Create the launch configuration

From your command prompt enter the following command:

Obviously substitute the name of the AMI in here – you can either pick one of the AMI’s from the EC2 marketplace or if you have an existing EC2 instance just right click on it and select ‘Create Image’. Remember the name you enter here (the first parameter) as you’ll need this in the next step.
If this worked, your console should ouput:

OK-Created Launch Config

2. [WHERE] Create the auto scale group

The next step is creating a scaling group. From the command prompt enter:

Here we specify that we want to launch our previously created configuration into the eu-west-1a availability zone. If you want to distribute your instances then you can supply a comma separated list here. As we want to start and stop a single instance we’ve set the upper and lower bounds of the group to 1 and 0 respectively.

If the command worked the output should be:

OK-Created AutoScalingGroup

3. [WHEN] Auto-scaling schedule

AWS needs to be configured to start and stop the instance(s) at a given time. The schedule is performed in accordance to the CRON job format (see . The format looks a little daunting at first but isn’t too bad once you’ve got used to it. We need to create a scale up policy to start the instance and a scale down policy:

Enter the following two commands:

The above commands creates a scale-up and scale-down policy. The first commands requests a single instance to start at 3pm every day (15:00hrs) and the second requests zero instances running at 4pmm (16:00hrs) – hence AWS will terminate this instance.

For both of these you should get a nice friendly

OK-Put Scheduled Update Group Action

And that’s the lot!

A quick

should list your launch configuration and you should now have an AMI that is started at 3pm every day and terminated at 4pm. Hopefully, your AMI is configured to do something during this period or on startup.

Clearing down is a case of running

for both the actions, passing in the name given above. Then clear down the group:

and finally the launch config:

And that’s it. You should now have an instance which is created at your set start time and left to run until you set end time. Simple and elegant – such is the power of AWS.