1

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.

  • What's wrong with the current code? Any errors? – Marcin Apr 22 '21 at 07:36
  • Is there more complexity here that you haven't shown? Right now you have a very static ASG and then you change it at a specific time. If you don't also attach autoscaling policies to the ASG to scale the number of instances then there's nothing wrong with your above code (although I assume it would be you have eg 10 instances during the week and then scale down to 9 during the weekend). If there is more complexity than that then you should explain so in the question and ideally show the minimal code required to have that scaling. – ydaetskcoR Apr 22 '21 at 08:07
  • I have created a new post with the detailed code I am using , and what i want to achieve. Here is the link. : https://stackoverflow.com/questions/67212282/manage-asg-via-terraform. Please refer to this and help – Legitimate_Figure_4430 Apr 22 '21 at 11:25

1 Answers1

1

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.

  • I have created a new post with the detailed code I am using , and what i want to achieve. Here is the link. : stackoverflow.com/questions/67212282/manage-asg-via-terraform. Please refer to this and help. thanks for your help on this 1 – Legitimate_Figure_4430 Apr 22 '21 at 12:22
  • Why would you need a Lambda function for this, when AWS already has time-based auto scaling? https://docs.aws.amazon.com/autoscaling/ec2/userguide/schedule_time.html – Mark B Apr 22 '21 at 15:55