AWS Instance Scheduler (Start/Stop EC2 and RDS instances automatically).
AWS Instance Scheduler is a solution implementation that helps customers to save specific costs by stopping and starting the EC2 and RDS instances accordingly.
In simpler words, it’s an AWS service to start and stop EC2 and RDS instances according to specific time periods and timezone.
So now the question is how can we implement and use it? We will be running down together the below-given steps to use and implement the AWS Instance Scheduler Solution.
1) We will use the following given link that will redirect us to the AWS Instance Scheduler Solution Implementation webpage.
https://aws.amazon.com/solutions/implementations/instance-scheduler/
2) Once we are there, we will see the following page. There click on “Launch in the AWS Console”. This will initiate this solution and you will land on the AWS account where you want to implement this solution.
3) We will see the following page of the CloudFormation service of AWS, there we will follow the specific steps of CloudFormation stack creation for the AWS Instance Scheduler Solution.
Step 1: Specify Template = We will choose all the default options over here.
Step 2: Specify stack details = We will provide the parameters for the AWS instance scheduler service to be implemented based on our requirements. We will give this stack a name. We can choose the EC2, RDS, or both services to implement this service.
We will be choosing the respective time zone. We can also choose the Frequency (time in minutes when the scheduler runs to check for the parameters specified) for the running of the AWS instance scheduler.
We can also enable/disable the CloudWatch metrics. And the most important part is Started and Stopped Tags that play an important role in identifying the resources if they are started or stopped manually or by the Instance Scheduler.
3.3) Step 3: Configure stack options = We can provide the tags as per our requirement and also specify the custom IAM roles for this Instance Scheduler being created (or leave the IAM section blank as the AWS instance scheduler creates the IAM roles for itself required to it).
3.4) Step 4: Review = We will review all the configuration parameters specified previously by us once again to double-check them as reviewing configured parameters is the best practice while creating a CloudFormation stack and other services too.
Once the review is completed, we will check to mark the “I acknowledge that AWS Cloudformation might create IAM resources” (this option will create the required IAM roles and policies itself for the AWS Instance Scheduler) and then click on “Create Stack”.
It will not take more than a few minutes to create the stack. Once the CloudFormation stack is created, we are ready to use this AWS instance scheduler Solution.
4) Once the CloudFormation Stack for AWS instance Scheduler is created i.e. Status shows “CREATE_COMPLETE”, there few resources are created such as AWS Lambda, AWS DynamoDB, AWS Cloudwatch, and others.
Among these the most important AWS service that will be used for making AWS Instance Scheduler work created through the AWS CloudFormation stack is DynamoDB.
5) We will head towards this service and inside Dynamodb we can see already three tables are created. Among these, we will use the Dynamodb table named “demo-instanceScheduler-ConfigTable-JVL2QPMJ17KO” which contains all the required items to get started with the AWS Instance Scheduler.
6) Once we go into this table, in the items section we will see the following items with specific parameters.
Where,
i) config = is an item that contains all the configuration details that we have configured previously while the creation of the CloudFormation Stack for the AWS Instance Scheduler.
The “config” item contains the details about “timezone”, “region”, “scheduled_services”, “Started_tags”, “Stopped_tags”, “Cloudwatch related options” and many more.
ii) period = is an item that contains parameters related to the timing on which to start and stop instances. Some parameters for the period are “weekdays”, “months”, “begin time”, “endtime”.
The most important ones the “begintime = is the time that specifies when to start the server” and “endtime = is the time that specifies when to stop the instance”
For example, There is an EC2 instance that has its begintime of 8:00 AM and endtime is 5:00 PM. This simply means that the EC2 server will be running between 8:00 AM morning to 5:00 PM evening and then stop after 5:00 PM evening up to 7:59 AM morning.
iii) Schedule = is an item that contains parameters related to “timezone” and “period”.
For example: Let’s say you created a period item that has the name “nepal_office_hours”. In the schedule item, we can use that period name (nepal_office_hours) in the period's column so that when the scheduler is expected in the defined frequency, it will first check the schedule and in the schedule, it will find the period name and will process the data in the period's row also accordingly.
The schedule and periods in the Dynamodb config item table are interrelated.
Schedule -> Period -> starts and stops EC2 and RDS instances.
Note: Don’t forget to tag the instances that you want to start and stop using the AWS Instance Scheduler solution.
We need to choose the Name for the tag as “Schedule” as we have given this name as the Instance Scheduler tag name during the creation of the CloudFormation stack for the AWS Instance Scheduler and the value will be the name of the Schedule that we are using (such as “office-hours” or “uk-office-hours” or others).
7) Once all of this is done (i.e. we have given specific begintime, stoptime, weekdays details in the period section and period name and timezone in the schedule section), you will be able to see the instances being tagged with related schedule name pointing to a period being starting and stopping accordingly at the specified time given by us.
Congratulations, We have successfully used the AWS Instance Scheduler service to start and stop the EC2/ RDS instances accordingly at the specified time period and timezone.
Questions:
- How to set a stopper for this scheduler on a particular date in a month? For Example, I want to start and stop an instance from 13 November to 15 November and 22 November to 27 November only and run instances on the other timeframe.
Answer:
To create a rule for the instance scheduler that stops for specific periods for the month of November. A quick configuration for this would be
{
"type": {
"S": "period"
},
"name": {
"S": "DaysInMonth"
},
"monthdays": {
"SS": [
"1-12, 16-21, 28-30"
]
},
"months": {
"SS": [
"11"
]
},
"description": {
"S": "Days in Month to stop/start"
}
}
which you can save by going to DynamoDB -> Items -> Select the ConfigTable -> Create Item -> On the top right select JSON -> Copy paste -> Save.
which runs the instance from November 1–12, stops the instance from Nov 13–15, starts the instance on Nov 16–21, stops the instance on Nov 22–27, and then keeps the instances running until the end of November.