Search code examples
terraformterraform-provider-awsaws-auto-scaling

Managing Auto Scaling Group via terraform


Let say I have a auto-scaling group which I manage via terraform. And i want that auto scaling group to scale up and scale down based on our business hours .

The TF template for managing ASG :

resource "aws_autoscaling_group" "foobar" {
  availability_zones        = ["us-west-2a"]
  name                      = "terraform-test-foobar5"
  max_size                  = 1
  min_size                  = 1
  health_check_grace_period = 300
  health_check_type         = "ELB"
  force_delete              = true
  termination_policies      = ["OldestInstance"]
}

resource "aws_autoscaling_schedule" "foobar" {
  scheduled_action_name  = "foobar"
  min_size               = 0
  max_size               = 1
  desired_capacity       = 0
  start_time             = "2016-12-11T18:00:00Z"
  end_time               = "2016-12-12T06:00:00Z"
  autoscaling_group_name = aws_autoscaling_group.foobar.name
}

As we can see here i have to set a particular date and time for the action.

what I want is : I want to scale down on saturday night 9 pm by 10% of my current capacity, and then again want to scale up by 10% on monday morning 6 am .

How can I achieve this.

Any help is highly appreciated. Please let me know how to get through this.


Solution

  • The solution is not straightforward, but is doable. The required steps are:

    • create a Lambda function that scales down the ASG (e.g. with Boto3 and Python)
    • assign an IAM role with the right permissions
    • create a Cron trigger for "every saturday 9pm" with aws_cloudwatch_event_rule
    • create a aws_cloudwatch_event_target, with the previously created Cron trigger and Lambda function
    • repeat for scaling up

    This module will probably fit your needs, you just have to code the Lambda and use the module to trigger it on a schedule.