AWS scheduler for stopping & starting EC2 , RDS & Aurora Clusters

yehan anushka
CloudTricks
Published in
6 min readFeb 15, 2020

Most of the time we need to maintain at least two separate environments one for production & others for testing we can call it as Dev, UAT, Staging. Actually we don’t need 24/7 up time for testing environments like UAT & Staging, if we can only run environment resources only our working hours we can save our money because AWS charge from as for computation power. If we can stop resource when we don’t use it the computation power cost will go away.

Now let’s see how we can automate stopping and start AWS resources according to time period.

Step 01
Login to AWS console with your credentials. After logging into AWS go to the link below,
https://docs.aws.amazon.com/solutions/latest/instance-scheduler/deployment.html and scroll bit down until finding the launch solution. Once clicked on that button it will redirect you to CloudFormation > Stacks > Create stack

Step 02
Click next. Then it will redirect to the following page where you need to configure stack name and parameters.

Stack name : This is the name for cloud formation stack

Parameters
I’ll only discuss the parameters what we need here.

Schedule : Tag name for associate with ec2 or rds, this is the key value of the tag.
Service: You can select EC2, RDS or Both according to your requirement.
Schedule Aurora Cluster : If you need to stop and start your aurora cluster you need to enable this option change it to yes.
Create RDS Instance snapshot : if you need to create snapshot before stop the rds instance you need to make this option to yes.
Region : If your resources are in same region you can leave it as blank, but your resources are in different regions you need to list region ids here.
Frequency : Scheduler running frequency default 5 you can change it according to your requirement.
Memory allocation : Allocation memory for the lambda function, make it large if you plan to schedule large number of instance or plan to stop aurora cluster.
CloudWatch Logs : Enable it if you need logs for troubleshoot purposes, this will push logs to cloudwath.
Started tag : Value which you provide to here it will add to resource as tag when scheduler start the resource Ex : Started_by=ec2_scheduler.
Stopped tag : Value which you provide to here it will add to resource as tag when scheduler stop the resource Ex : Stopped_by=ec2_scheduler.

Once done the configurations click next. It will appear below page.

Tags : Add tags which you need, it ups to you tag will be helpful to when you need to know about cost for this resource
IAM role : Create IAM role it should have permission to create lambda, CloudWatch create rule, create dynamo db and create tables.
I.e you can remove this after complete creation this cloud-formation stake.

Then click next. Now we can see the configuration overview, make sure to double-check all ok if it is ok then check the checkbox I acknowledge that AWS CloudFormation might create IAM resources

Click create stack and it will create 2 dynamodb tables, lambda function & cloudwatch trigger.

It will take some time to create. You can be able to monitor the creation process by clicking events on the stack.

Step 03 :

Configure Dynomodb table
Go to dynomodb then tables. There must be two dynamodb tables which are created by the cloud-formation stack.

State Table: It will store all the ec2 instances & rds details(instance_id, state change Ex running to stop, affected timestamp ) which are controlled by ec2sheduler.

Scheduler Configuration Table: It will store the configuration details according to parameters we gave in step 02, It also has some freely defined periods and schedules. These will help us to create our own period and schedule.

In the config tuple there is a column called tag name, this is the “Key” of the tag we need to add to resource which we need to schedule, our scheduler will only affect for the instances which included this tag.

Now move to create our own period.
A period is a configuration tuple which is referred by the scheduler to identify the instance running time and end time.

Ex:

Create a period according to our requirement.
Select one period, go to action and duplicate it, now you can see a popup window. On the pop-up window, you can change the values according to your requirement.change the configuration values on the pop-up window and save it.

begintime : time we need to start our resources.
description : Period description.
endtime : time we need to stop our resources.
name : name of the period this will refer by scheduler.
weekdays : days you need to schedule your resource in here I have mentioned Monday to Friday

Save new period and move to create the schedule.

Create the schedule with above created period.
Select one schedule and duplicate it like previous and it will popup a window like following.

name : This value use to identify map schedule and resource, this will be the value of the tag we need to add to resource Ex : tag_key : Scheduler & tag_value : sydney-office-hours.
period :
period created by above step
time zone : Time zone where your resources are located on

Step 04 :

We need to add this name string as a value of the schedule tag to resource which are plan to stop and start . After adding name string related EC2 or RDS will start and stop according to the time period. We can able to monitor it using the cloud watch logs and graphs.

Important Notes

Parameter Configuration

  • When we configure scheduler running frequency we have to be careful with the value because lambda function will be triggered on this frequency and it will cost more for us.
  • Memory Size: If we are working with a large number of instances then we have to increase the memory size for running lambda function. If not scheduler won’t be work.

--

--

yehan anushka
CloudTricks

DevOps Engineer at Novigi Pty Ltd | RHCSA | AWSCSAA