Beanstalk launched long time ago support for scaling your environment according to a defined schedule. For some unknown reasons, we discovered this feature is known by very few people.
We use it in 2 scenarios:
- To scale up one environment when we expect a high load
- To completely close our staging environments out of office hours in order to avoid wasting money.
To setup it, you have 2 options:
Using the AWS console
Open your environment, then go to Configuration -> Capacity and at the bottom of that page there is a section: Time-based scaling.
Add 2 new scheduled options, one for scaling up, with the following recurrence: 30 5 * * 0,1,2,3,4.
And another for scaling down, having the following recurrence: 30 17 * * 0,1,2,3,4
Basically, the whole idea is : starting Monday until Friday, on 5:30 UTC AM set number of instances to 1 and decrease it to 0 at 17:30 UTC
Using config files
Recently we wrote an article about how to use Beanstalk config files. Another use case for these .ebextensions is to set scaling options. Of course, if you use this approach but you have many environments (staging, production) in a single application, then all of them will be affected by this policy. That could a reason why this approach is not the best one. But let’s say it’s ok for the moment.
The content of the config files should look like:
Resources: StopHostInTheAfternoonWorkingDay: DependsOn: AWSEBAutoScalingGroup Properties: AutoScalingGroupName: Ref: AWSEBAutoScalingGroup DesiredCapacity: "0" MaxSize: "0" MinSize: "0" Recurrence: "30 17 * * 0,1,2,3,4" Type: "AWS::AutoScaling::ScheduledAction" StartHostWorkingDayMorning: DependsOn: AWSEBAutoScalingGroup Properties: AutoScalingGroupName: Ref: AWSEBAutoScalingGroup DesiredCapacity: "1" MaxSize: "1" MinSize: "1" Recurrence: "30 5 * * 0,1,2,3,4" Type: "AWS::AutoScaling::ScheduledAction"
Of course you will find several examples in the official documentation that could be adjusted to fit your needs. In not, let a comment here and we’ll work together on that. Since the whole setup shouldn’t take more than 5 minutes, we consider it worth giving a try. At least in staging, it’s a good fit for us.
Happy cloud computing!