Auto Schedule EC2 Instance by Harsh Patel

How Auto-scheduling your EC2 Instance can save you a fortune!

Cloud can be really costly. But not if it’s used wisely, the whole idea of Cloud Services is to utilize resources on-demand. You won’t wanna pay for the time you are not utilizing your resources and so you can schedule your resources to operate on optimal timing. Let’s see how to auto-schedule your EC2 Instance using Cloud Formation so it starts and stops when you want it to.

Let’s break down the process first,

  1. Creating a Cloud Formation Stack
  2. Configuring the schedule
  3. Applying the schedule to an EC2 Instance

How it works?

  1. The Cloud Formation template you define schedules the runtime for your EC2 instance using the AWS services underneath
  2. The Configuration values you set for the Schedule gets stored in DyanmoDB and the Lambda services fetches it every time the schedule is triggered
  3. You apply the associated tag to the desired EC2 instance to be scheduled

For detailed Architecture you can read official AWS documentation here.

This activity uses AWS services like DynamoDB, Lambda, etc. which might incur some cost that you can refer here but which is a small fraction of what it actually helps you save.

First we need to create a Cloud Formation Stack where we’ll define the configuration for AWS to be able to provision the services.

Creating new stack

AWS provides a ready made Instance Scheduling template which can be found here (download the aws-instance-scheduler.template), upload the template and click Next

specifying the template

We’ll then need to name the stack, you can name it anything you want

Naming the stack

Next, setting up the parameters for the stack. I’ll Leave everything to it’s default value as shown below since it doesn’t need to be explicitly changed for any particular reason unless you want it differently. For example if you prefer different Time Zone or more Memory Size for lambda.

Note the Instance Scheduler tag name that we’ll be using to tag the EC2 Instance

Setting up parameters
Defining Time Zone and Memory Size Parameters

The options are additional settings that can be configured as required like Enabling the CloudWatch Metrics, Logs,etc. I have kept it disabled for now as I don’t require it. You may want to change Log retention days as required.

Make a note of Started tags and Stopped tags which are crucial for further steps

Setting additional options

Next is configuring Stack options like Tags (Optional)

Optional Step: defining tags

We’ll leave the Permissions as-is to auto allocate a IAM role unless you need to set it to something specific

Permissions for IAM role

We won’t dive into advance options for now since we don’t need any advance metrics to be setup for time being

Once You have configured the entire Cloud Formation Stack, Review it and click on Create Stack

Review and create stack

After clicking on Create Stack the status of your stack will be in progress as shown below

Stack creation in progress

Post successful creation, the status of stack will change to complete as shown below

Stack created successfully

Once stack is created successfully, Go to ConfigTable under Resources to visit the DynamoDB Tables

Resources > ConfigTable

It will open the DynamoDB table where you’ll be able to see three tables

ConfigTable and StateTable

For now, we need to understand the purpose of ConfigTable and StateTable

  1. ConfigTable holds configuration details of the Schedule
  2. StateTable holds the current state of the tagged Instance

We’ll now duplicate schedule (uk-office-hours) to create a new copy and set it to india-office-hours (you can change it to whatever region you want)

Duplicate the schedule item to define a new copy of india-office-hours

We’ll define the new item as per our needs, as in my case I want to set it to Indian (IST) timings.

Now we’ll schedule the office-hours timings (Asia/Kolkata in my case)

edit the office-hours

I have kept it to Mon-Fri (9 AM — 5 PM) that’s when my usual operations occur. (You may change it to your needs).

Since we have configured everything, now we just need to tag the EC2 Instance we want to schedule.

Go to EC2 Instance > Manage Tags > Add tag

add the Schedule Tag

And Done!

You have successfully Scheduled your EC2 Instance, Now you can verify the schedule using the Tags associated with the EC2 Instance as shown below

ScheduledStart=true indicating successful start of the EC2 Instance

ScheduledStop=true indicating successful stop of the EC2 Instance

The EC2 Instance state before the Instance auto-started was as shown below having only two tags associate with it

Before scheduled auto-start

After Scheduled auto-start an additional tags get associated with the Instance

After scheduled auto-start

Post Scheduled auto-stop, ScheduledStop tag gets associated with the Instance

After scheduled auto-stop

You can apply same tag to multiple Instances if you want and they’ll all operate on same schedule and that’s how you schedule an EC2 Instance with the help of AWS Cloud Formation Stack. There might be other ways as well to schedule EC2 Instance but this comes handy and more organized.

I am an Rookie scaling vertically as an Veteran. Learning Cloud and DevOps technologies because “It ran on my machine” excuse doesn’t work anymore.