Jeff Dickman - Blog There's something to this...

Terraform and Scheduling AutoScaling Groups

I often advocate the idea that, if done right, using AWS can lower your costs for infrastructure. One great way to accomplish this is to us AutoScaling groups wherever possible. As a most basic feature, AutoScaling Groups allow you to dial up and dial down instances based on driving metrics. Metrics like CPU, RAM and Network can allow your services to expaand when needed and drop down to minimal levels during quiet times. AutoScaling Groups also allow you to set a schedule for increasing or decreasing your capacity. The scheduling feature can come in very handy for managing the availability of resources in a development environment.

Let’s take the use case of a Jenkins Server in our DEV AWS environment. We put Jenkins into an AutoScaling group with a min/max of one instance. This ensures we always have a Jenkins server up and running. If for any reason it’s terminated, the ASG provisions a new one and in minutes we’re back to consistent state.

resource "aws_autoscaling_schedule" "asg_morning" {
  count                  = "${var.asg_schedule}"
  scheduled_action_name  = "${upper(var.org)}-${upper(var.group)}-${upper(var.environment)}-Jenkins-AM-Schedule"
  min_size               = 0
  max_size               = 1
  desired_capacity       = 0
  recurrence             = "${var.asg_schedule_am}"
  autoscaling_group_name = "${aws_autoscaling_group.jenkins_asg.name}"
}

resource "aws_autoscaling_schedule" "asg_evening" {
  count                  = "${var.asg_schedule}"
  scheduled_action_name  = "${upper(var.org)}-${upper(var.group)}-${upper(var.environment)}-Jenkins-PM-Schedule"
  min_size               = 1
  max_size               = 1
  desired_capacity       = 1
  recurrence             = "${var.asg_schedule_pm}"
  autoscaling_group_name = "${aws_autoscaling_group.jenkins_asg.name}"
}