How to automatically shut down EC2 (AWS) instances over night and safe money!

Ok, guys, you were asking for it, I deliver: How to automatically shut down EC2 (AWS) instances overnight and save money.I talk to a couple of you about automatically shutting down an EC2 instance overnight and how to set it up in AWS. Some of you, same as me, had a hard time following the official AWS documentation, so I thought I simplify things for you a little bit.

Without further ado, let’s dive right in.

 

1. How it works

We basically create a piece of code that runs on Amazon’s Lambda, their Serverless Computing Service, that allows us to run code at certain times. We will leverage this code to shut down and start our EC2 instances at our desired times.

We can either enter the code manually using Lambda directly or, the easier way which we will choose, utilize the already existing ec2-scheduler AWS CloudFormation template.

 

2. Launching the Stack

First, log in to your AWS Console. Then, open This Link (that’s the link from the official documentation) to create the ec2-scheduler stack.

Per default, US East (N. Virginia) is selected as a region. So run through the 3 steps below:

  1. Change Region to your Region
  2. Verify that the ec2-scheduler template is selected
  3. Click Next

On the Specify Details Page that comes up next, you can change the name of the stack if you want, and more importantly, you can set the default time you want your servers to shut down. A more detailed description of each function is found in the official documentation.

This time is not a fixed time. This is the default time, you can still adjust the start and stop time for each server individually, using tag values, I’ll explain that to you later.

The Default time is ALWAYS in UTC, so you have to do some calculations to fit your timezone.

Just choose a good default time that you maybe want to use on more than one server.

Click Next after you are done with this.

On the Options page, click Next.

On the Review page, double check if all is to your liking and scroll down to the bottom to tick the box that you acknowledge that the template will create an AWS identity:

Click on Create to create the Stack.

Now you view the status of the stack in the AWS CloudFormation console in the Status column, it should take roughly 5 minutes until you see: CREATE_COMPLETE.

Grab a nice glass of wine in the meanwhile, and more importantly, donate something to me so I can grab a glass too 😉

Alright! Creating a Stack is done – let’s go Tagging!

 

2. Tagging our EC2 Instances

Navigate to your EC2 Dashboard and find the EC2 instance you want to automatically shut down.

Select it and then click on Tags and Add / Edit Tags:

 

Now you probably already have a Name for your server in place, so you will see the Key Name with the Value “Yourservername”. That can stay as it is, simply click on Create Tag to create a new tag.

For the Key you want to enter: scheduler:ec2-start-stop

For the Value, you want to enter: default

This means, the server stops and starts by the time you set earlier when we were creating the Stack. You can individually change this by entering, for example, 0800;1800;utc;all as the Value. So this server will not use the default time, but the time you define at value. You can find a list of those commands in the Official Documentation when you scroll down close to the bottom.

Click on Save after you are done.

And there you have it. It’s that simple if you don’t have to read through the documentation, that is 🙂

 

Leave a Reply