Scale up & down your Beanstalk environment according to a schedule

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!

5 thoughts on “Scale up & down your Beanstalk environment according to a schedule

    1. There is NO NEED to create the load balancer.
      Everything you need does exist and any interaction is only with Elastic Beanstalk.

      1. Hi,

        is it applicable to both tiers of Environments i.e

        web server environment,
        worker environment.

        why am asking is for web server environment , there will be environment URL ,

        by using that url , we can check wheather file is working properly or not.

        but we are not getting URL for worker environment.

        1. You can use this feature for both types.
          You’re right: worker env doesn’t give you any endpoint, but if you want an way to check if your environment is up, then create a web server and integrate it with SQS. Not so complicated.

          1. Hi,

            i were started with web server environment,
            in it under configuration section—>capcity
            by defalut environment type is single instance

            i want to apply sechduler to my environment
            so i select environment type as load balanced

            so that by changing , it is creating load balancer
            automatically.

            in that page , under Time-based Scaling
            i were add sechduler to down my environment.

            it has downed my environment successfully

            but , i were charging for load balancer.
            in every month.

            after finding your post in google,
            i have created environment i.e worker environment

            in this environment, under configuration —–> capacity
            i were changed environment type as load balanced.

            i tried to down my environment,
            it has done successfully,

            but in this environment , it is not creating load balancer.

            i were down my environment sucessfully.
            now i think want to pay only for data storage in aws .

            would please help me ,
            to change web server environment to worker environment .

            why because i am maintaing no .of environments ,
            where they are under testing , i want to apply sheduler in my office timing.

Comments are closed.