Using AWS Instance Scheduler to Save Costs

AWS compute services has several pricing models – on demand (for unpredictable workloads), reserved instances or saving plans (for long term predictable workloads) and spot (for scenarios where users want to save costs with the understanding that the compute instance will be taken back by AWS within 1 or 2 minutes).

In scenarios where there are predictable scheduled intervals for running compute services (such as EC2 machines or Amazon RDS), there is a new option for saving up to 70% of the service cost by using AWS Instance Scheduler.

A common scenario is for development environments or workloads that be shut down during non-working hours since no customers are using specific environments during that timeframe.

Architecture

AWS Instance Scheduler is based on CloudFormation template. We configure a CloudWatch event that triggers a Lambda function to shut down or power on the EC2 or RDS instance at pre-configured intervals.

 

The entire configuration is based on tagging, which allow us to control multiple EC2 or RDS instances under the same configuration.

The entire configuration (such as time zone, scheduled interval, etc.) is stored inside an Amazon DynamoDB table.

AWS Instance Scheduler supports both current AWS accounts and remote (secondary) AWS accounts, for organizations with multiple environments and multiple AWS accounts.

Deployment of AWS Instance Scheduler Stack

  1. Login to the CloudFormation console https://console.aws.amazon.com/cloudformation/home
  1. From the upper pane, switch to the target Region (such as eu-west-1)
  2. From the main pane, click on “Create stack”
  3. Under “Specify template”, select “Amazon S3 URL” -> specify the URL below:
    https://s3.amazonaws.com/solutions-reference/aws-instance-scheduler/latest/instance-scheduler.template
  4. Click Next
  5. Fill in / select the following details:

> Stack name – specify stack name, for example: Instance-Scheduler

> Instance Scheduler tag name – specify tag name or leave the default value

> Service(s) to schedule – Select either EC2, RDS or both

> Create RDS instance snapshot – Select “Yes” in-case you wish to schedule a snapshot of a RDS instance before shutdown, otherwise, leave “No”

> Scheduling enabled – leave the default value “Yes”

> Region(s) – For enabling Instance Scheduler on a specific Region (such as eu-west-1), specify the Region code, otherwise leave blank

> Default time zone – select the relevant time zone

> This account – leave the default value “Yes”

> Frequency – select the frequency to run the Instance Scheduler

> Memory size – leave the default value (unless you have a large amount of EC2 and RDS instances)

> Send anonymous usage data – select “No”

  1. Click Next -> on the Options page leave the default settings and click Next
  2. On the Review page, select “I acknowledge that AWS CloudFormation might create IAM resources” and click on “Create stack”
  3. Wait for the CloudFormation stack creation to indicate “CREATE_COMPLETE”

 

Install Scheduler CLI, configure Period and Schedule

  1. Configure Scheduler CLI on a Linux machine, as instructed here:
    https://docs.aws.amazon.com/solutions/latest/instance-scheduler/appendix-a.html
  1. Run the command below to configure the “Period” to weekdays (Monday to Friday) between 09:00-18:00
    scheduler-cli create-period –name “weekdays” –begintime 09:00 –endtime 18:00 –weekdays mon-fri –stack Instance-Scheduler
  1. Run the command below to configure “Schedule” called “OfficeHours” in London time zone:
    scheduler-cli create-schedule –name OfficeHours –periods weekdays,weekends –timezone Europe/London –stack Instance-Scheduler

 

Instance Tagging

Deploy a new EC2 or Amazon RDS instance, and add a new tag with key “Schedule” and value “OfficeHours”.

For more information about EC2 tagging, see: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#Using_Tags_Console

For more information about RDS tagging, see: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_Tagging.html

 

Additional Notes

When deploying an Amazon EC2 instance, the “Shutdown behavior” must be set to “Stop” for the Instance Scheduler to function.

When using encrypted EBS volumes with Instance Scheduler, make sure to grant the Instance Scheduler the role of kms:CreateGrant in order to allow the service access to the customer managed encryption key.

When using Instance Scheduler with Amazon RDS instances, it is recommended to allow Instance Scheduler to take a snapshot before shutting down the RDS instance.

Enabling CloudWatch metrics to the Instance Scheduler configuration will add additional cost to the service.

 

Reference

AWS Instance Scheduler Implementation Guide
https://s3.amazonaws.com/solutions-reference/aws-instance-scheduler/latest/instance-scheduler.pdf