In an attempt to further reduce costs of our EC2 instances, we determined some instances that are running 24/7 unnecessarily. An automated process to schedule  stop and start instances would greatly help cutting costs. Of course, the solution itself should not add an extra instance to our infrastructure. The solution has to be an example of serverless computing.

Automagically stop running EC2 instances 24/7  unnecessarily

We created a lambda function that scans all instances for a specific tag. The tag we use is named ‘Schedule’ and contains the desired ‘runtime’ for the specific instance. Instances without a Schedule tag will not be affected. We support the following content format in the Schedule tag:

08:00-19:15   start the instance at 08:00, stop it at 19:15
21:00-07:45   start the instance at 21:00, stop it at 07:45 the next day
-17:45              stop this instance at 17:45 today
-16:30T           Terminate instance at 16:30 today
# whatever     Anything starting with a # is totally ignored

NOTE: All times are UTC times!

Invalid tag-content is automatically prefixed with an # so that it is ignored in future invocations of te lambda function. Also instances with only an end-time will have their tag rewritten to avoid restarts at 00:00.

Our lambda function is written in python using boto3 for AWS integration. The function requires a role to be able to interact with EC2. The lambda function has to be able to read and change tags, stop and start instances and even terminate them!

To further automate the process we need to automatically execute the lambda function, which can be achieved by creating a Cloudwatch Event Rule. It is a bit of a funny place for a schedule, but that’s where you configure it. The event will be used as a trigger to start our lambda function.

Instead of giving you instructions on how to create all of this nice stuff, we have written a cloud formation template for you to deploy in your own environment. It creates the lambda function, a role for it and a scheduler to activate the lambda function regularly. Make sure to adopt it for your local needs! It is a starting point more than a ready-to-use product.

The scheduler we create in the stack runs the lambda function every 10 minutes, giving your EC2 scheduler a granularity of 6 runs per hour.

Tag your instances with a Schedule tag and start reducing costs!

You can download the cloud formation template here and deploy it using cloud formation!

Or just press the button below and it will automatically be deployed!

Lanuch EC2 scheduler
Launch EC2 scheduler

The scheduler only works in the region(s) you deploy it in.

Share & Enjoy