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,
- Creating a Cloud Formation Stack
- Configuring the schedule
- Applying the schedule to an EC2 Instance
How it works?
- The Cloud Formation template you define schedules the runtime for your EC2 instance using the AWS services underneath
- The Configuration values you set for the Schedule gets stored in DyanmoDB and the Lambda services fetches it every time the schedule is triggered
- 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.
Creating a Cloud Formation Stack
First we need to create a Cloud Formation Stack where we’ll define the configuration for AWS to be able to provision the services.
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
We’ll then need to name the stack, you can name it anything you want
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
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
Next is configuring Stack options like Tags (Optional)
We’ll leave the Permissions as-is to auto allocate a IAM role unless you need to set it to something specific
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
Configuring the Schedule
After clicking on Create Stack the status of your stack will be in progress as shown below
Post successful creation, the status of stack will change to complete as shown below
Once stack is created successfully, Go to ConfigTable under Resources to visit the DynamoDB Tables
It will open the DynamoDB table where you’ll be able to see three tables
For now, we need to understand the purpose of ConfigTable and StateTable
- ConfigTable holds configuration details of the Schedule
- 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)
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)
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).
Applying the Schedule to EC2 Instance
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
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
After Scheduled auto-start an additional tags get associated with the Instance
Post Scheduled auto-stop, ScheduledStop tag gets associated with the Instance
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.