In this article, you will learn:
One of the main benefits of using AWS Cloud is the elasticity and flexibility you can leverage. Backed by Amazon’s massive infrastructure, you have access to computing and storage resources when you need them. But how can you start and stop instances automatically without manual intervention? This is where AWS Instance Scheduler can help.
What is AWS Instance Scheduler?
The AWS Instance Scheduler is a solution that automates the starting and stopping times of Amazon Elastic Compute Cloud (Amazon EC2) and Amazon Relational Database Service (Amazon RDS) instances. This solution is also referred to as the AWS EC2 Instance Scheduler and RDS Instance Scheduler.
The reason for using such a solution is to reduce AWS costs by stopping instances that are not in use and starting resources only when their capacity is really needed. For example, a company can use AWS Instance Scheduler in a non-production environment to automatically stop instances outside of working hours every day when developers do not need it.
For instances where resources are only required during regular working hours, weekly utilization can be reduced from 168 hours (seven days) to approximately 50 hours (a five-day working week). This solution can result in up to 70% cost savings.
Read more about AWS cost optimization tools in our blogpost - 8 AWS Cloud Cost Optimization Strategies and Tools
Benefits of AWS Instance Scheduler
Configuration is done by Scheduler CLI: The AWS Instance Scheduler includes a simple command-line interface (CLI) that provides commands for configuring schedules and periods. You are also able to estimate cost savings for a given schedule.
Instance scheduling in a multi-account environment: The AWS solution includes a template that creates the AWS Identity and Access Management (IAM) roles necessary to start and stop instances in different accounts.
Automated tagging: AWS Instance Scheduler is able to automatically add tags to all instances it starts or stops. It also includes macros that allow you to add variable information to the tags.
How does AWS Instance Scheduler work?
AWS Instance Scheduler leverages AWS resource tags and AWS Lambda to automatically stop and restart EC2 and RDS instances across multiple AWS Regions and accounts on a defined schedule.
The schedule with configuration (such as time zone, time interval, etc.) is stored inside an Amazon DynamoDB table.
You can also configure Amazon Simple Notification Service (SNS) topics and Amazon CloudWatch for notifications and logging.
The solution creates an AWS managed Custom Master Key (CMK), which is used to configure server-side encryption for the SNS topic and the DynamoDB tables.
AWS architecture is shown in a schema below; the AWS Instance Scheduler is deployed as a CloudFormation template.
Deployment of AWS Instance Scheduler solution
This AWS Instance Scheduler tutorial should help you with the deployment of this solution. If you need more information about its pricing read the last section of this article.
You can also follow the instructions provided in the video below.
1. Log in to the AWS Management Console. The link below will get you into the CloudFormation console https://console.aws.amazon.com/cloudformation/home.
2. Choose a region for the deployment of this stack in the upper panel (Frankfurt: eu-central-1).
3. From the main panel, click on the “Create stack” button.
4. In the “Specify template” section, select “Amazon S3 URL” and specify the URL below: https://s3.amazonaws.com/solutions-reference/aws-instance-scheduler/latest/instance-scheduler.template
This S3 URL leads to the latest official instance scheduler template from AWS.
And click “Next”.
5. Set Stack name, for example: "My-scheduler".
6. Fill-in or select following details in the “Parameters” section.
a. Instance Scheduler tag name: Leave the default value as “Schedule”.
b. Service(s) to schedule: Select “EC2”.
c. Scheduling enabled: Leave the default value “Yes”.
d. Enable CloudWatch Metrics: Select “Yes”.
e. Send anonymous usage data: Select “No”.
There are more settings, but in this case, you can leave them as default:
Region(s): You can specify more regions where Instance Scheduler will work (such as EU-west-1).
Default time zone: Select the most relevant time zone.
Frequency: Define frequency (1 to 60 minutes) to run the AWS Instance Scheduler.
Memory size: Define memory for the AWS Lambda function. This is applicable for those with a large number of EC2 and RDS instances.
7. Click “Next”.
8. On the Options page leave the default settings and click “Next”.
9. Review your settings on the last page and select “I acknowledge that AWS CloudFormation might create IAM resources” and click on “Create stack”.
10. Wait for the CloudFormation stack creation to indicate “CREATE_COMPLETE” – it can take a couple of minutes.
How to configure your new AWS instance schedule
After the previous steps, you should be able to find a table in the DynamoDB console which will be named “My-scheduler-configtable-….” In this table, you will find some predefined “tags” for your usage and you can also create your own schedules, this is described in my video.
With the next steps, you can create your own schedule:
As a first step, you will need to configure AWS CLI credentials on any of your EC2 Linux instances. You will need to get your AWS access keys and store them inside the EC2 Linux instance. The following should help you with this process:
Quick configuration of CLI with AWS configure As a second step, you will need to install Scheduler CLI on this EC2 Linux instance:
1. Download and unzip the Scheduler CLI zip file on an EC2 Linux instance.
$ curl -O https://s3.amazonaws.com/solutions-reference/aws-instance-scheduler/latest/scheduler-cli.zip $ unzip scheduler-cli.zip
2. In the created Scheduler-CLI directory, run the setup script:
$ python setup.py install
3. As a last step, you can create your schedule. In this case, this will schedule instance to be running only on Monday to Tuesday from 9:00 to 18:00 UTC. If you need something more specific, try reading about command structure.
$ scheduler-cli create-period --stack my-scheduler --name onlymontue --begintime 09:00 --endtime 18:00 --weekdays mon-tue --timezone UTC
Instance tagging and AWS Instance Scheduler example
The Instance Scheduler monitors tags on instances. If the key on the instance tag matches the defined scheduler tag, then it applies the schedule that's set for the value of the instance tag. For example, if a tag has the key set to Schedule and the value set to onlymontue, then this schedule will be applied to that instance.
Test predefined tag
In the following steps, you test a predefined schedule that's running on a stopped instance. You can find a predefined running schedule in the DynamDB configuration table. The Instance Scheduler starts the instance when you apply a running schedule on the stopped instance.
Important: Stop the EC2 instance that you're testing so that you can test a predefined running schedule.
1. Open the Amazon EC2 console.
2. Choose the stopped instances that you want to tag.
3. Choose the “Tags” view, and then click on “Add/Edit Tags”.
4. Choose “Create Tag”.
5. For Key, enter Schedule, for Value, enter running and click on “Save”.
6. Go back to the Amazon EC2 console and then wait for the Lambda function to be triggered (in default it is triggered every five minutes).
How to uninstall the AWS Instance Scheduler solution?
You can simply uninstall it by using the following procedure:
1. Login to the AWS CloudFormation console from this link: https://console.aws.amazon.com/cloudformation/home
2. Select this solution’s installation stack, for example “My-scheduler” and click “Delete”.
AWS Instance Scheduler pricing
This solution on AWS is not free. The cost of it depends on the number of accounts used and if you set it up for EC2, RDS, or both types of instances. At this moment, the cost for running this solution which was described in the previous tutorial with default settings (only EC2 instances, one account) in the EU Central (Frankfurt) region is approximately $2 per month. The optional custom Amazon CloudWatch metric (you don’t have to enable this) will cost $0.90 per month per schedule or scheduled service. By default, this solution uses on-demand scaling for its Amazon DynamoDB tables to provide sufficient read and write capacity.
The cost of the solution per run will depend on the number of instances being tagged and managed by the solution. As the number of EC2 instances increases, the Lambda runtime also increases.
If you need assistance in analyzing your usage to identify EC2 and RDS instances that would be a good fit for using the AWS Instance Scheduler, please contact us and we will be glad to help.