Why keep your non-production environments up when nobody is up?
An environment in AWS usually is a group of these resources:
- Load balancers
- EC2 instances (hopefully part of an Autoscaling Group)
- RDS instances
- EBS volumes
- S3 buckets
- Caches, ElasticSearch, etc.
In this tutorial, we will show how to turn off the most costly members of an environment: EC2 and RDS instances.
By shutting it down outside working hours, you can cut by around 50% the total monthly cost of an environment.
EC2s with an Autoscaling Group
For ASGs, we will use the Scheduled Actions feature provided and create 2 actions:
- One to start the instances in the morning by setting the number of instances (desired, max, min) to what they were before, and
- One to turn off all the instances at night by setting the ASG numbers to zero.
Under EC2 > Auto Scaling groups, select the group for the environment you would like to schedule.
Then click “Create scheduled action”
Enter the details like below for the start actionA:
Enter the Desired capacity, Min and Max for the same numbers you have today.
Set a Cron recurrence.
In our example, we would like to start the instances every weekday at 6am, Sydney time. Converting that to UTC, we got:
0 20 * * 0-4
Which translates to every Sunday to Thursday, 08:00pm.
It follow the crontab specifications where:
[Minute] [Hour] [Day_of_Month] [Month_of_Year] [Day_of_Week]
Converting 6am AEST to UTC we get 8pm of the previous day.
Being in the previous day, we had to adjust the
[Day_of_Week] field to run from Sunday to Thursday (
And set the start time for the first occurrence of the schedule.
Now for the Stop Action:
Set the Desired capacity, Min and Max to 0.
Set a Cron recurrence for
0 10 * * 1-5
That translates to Monday to Friday, 10:00am, UTC time.
This converts to every weekday, 8pm AEST (Sydney) time, which is what we want.
For more examples and documentation, please see AWS docs at https://docs.aws.amazon.com/autoscaling/ec2/userguide/schedule_time.html
The final result should be something like:
To achieve the same using Terraform: