The Curious Dev

Various programming sidetracks, devops detours and other shiny objects

Sep 13, 2016 - 2 minute read -

Scheduling actions on an ASG

This post builds on Cloudified MoinMoin further where we’re going to schedule the application to be down at certain times of the day/week. A relatively new feature of CloudFormation templates is the AWS::AutoScaling::ScheduledAction block, which allows us to alter the MinSize and MaxSize properties of the AutoScalingGroup.

This can be useful in a number of settings, but perhaps a good example is an internal webapp for a business where there are quite clear “business hours” where it’s critical to have the application up and responsive all day long. The flipside is at night time and other out of hours times it might be desirable to trim the number of instances deployed down to the minimum, which could be zero.

So in this case I’m going to bring the MoinMoin ASG down completely just before midnight every night and then wake it up at 6pm the next day. This makes sense as a dead simple way to save 75% on instance costs (obviously the EBS storage keeps on costing, but that is quite minimal anyway). Which gives me a great wiki for use in the evenings.

I’ve added two new sections to the Resources segment of the template, one to schedule the ASG down to be at MinSize=0 and MaxSize=0 and another to bring it up to be at MinSize=1 and MaxSize=1. As this little wiki is hardly taxing the t2.nano instance, there’s plenty of room to scale up and certainly no need to scale out but it is likely it would be easy enough to have two or more instances in an ASG sharing an EFS file system if high-availability was desired.

Scaling down the ASG:

"moinmoinScheduleSleep": {
  "Type" : "AWS::AutoScaling::ScheduledAction",
  "Properties" : {
    "AutoScalingGroupName" : { "Ref": "moinmoinASG" },
    "DesiredCapacity" : 0,
    "MaxSize" : 0,
    "MinSize" : 0,
    "Recurrence": "59 15 * * *"
  }
}

Waking the ASG back up:

"moinmoinScheduleWake": {
  "Type" : "AWS::AutoScaling::ScheduledAction",
  "Properties" : {
    "AutoScalingGroupName" : { "Ref": "moinmoinASG" },
    "DesiredCapacity" : 1,
    "MaxSize" : 1,
    "MinSize" : 1,
    "Recurrence": "0 10 * * *"
  }
}

This is how it then appears in your ASG configuration within the AWS console:

Schedule tab for ASG

Note: You may have noticed the cron / “Recurrence” times are not 6pm and midnight as I described above, these times are actually UTC time and I’m in UTC+8 so I’ve had to adjust for this.