From 9f01f15b3fd72f52082d9d526809034b763bbde7 Mon Sep 17 00:00:00 2001 From: Ash Davies <3853061+DrizzlyOwl@users.noreply.github.com> Date: Fri, 21 Jun 2024 11:57:52 +0100 Subject: [PATCH] Set up a scale-in window --- scheduled-scaling.tf | 39 +++++++++++++++++++++++++++++++++++++++ variables.tf | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 scheduled-scaling.tf diff --git a/scheduled-scaling.tf b/scheduled-scaling.tf new file mode 100644 index 0000000..517fd55 --- /dev/null +++ b/scheduled-scaling.tf @@ -0,0 +1,39 @@ +locals { + today_date = formatdate("YYYY-MM-DD", timestamp()) + start_time_window = var.scale_down_start_time != "" ? "${local.today_date}T${var.scale_down_start_time}Z" : "${local.today_date}T23:00:00Z" + end_time_window = var.scale_down_end_time != "" ? "${local.today_date}T${var.scale_down_end_time}Z" : "${local.today_date}T06:00:00Z" +} + +resource "aws_autoscaling_schedule" "off" { + count = var.scale_down_out_of_hours_enabled ? 1 : 0 + + scheduled_action_name = "asg-${aws_launch_template.ecs.name}-schedule-down" + min_size = var.scale_down_min_servers + max_size = var.scale_down_max_servers + desired_capacity = var.scale_down_servers + recurrence = var.scale_down_recurrance + start_time = local.start_time_window + autoscaling_group_name = aws_autoscaling_group.ecs.name + time_zone = "Europe/London" + + lifecycle { + ignore_changes = [start_time] + } +} + +resource "aws_autoscaling_schedule" "on" { + count = var.scale_down_out_of_hours_enabled ? 1 : 0 + + scheduled_action_name = "asg-${aws_launch_template.ecs.name}-schedule-up" + min_size = var.min_servers + max_size = var.max_servers + desired_capacity = var.servers + start_time = local.end_time_window + recurrence = var.scale_down_recurrance + autoscaling_group_name = aws_autoscaling_group.ecs.name + time_zone = "Europe/London" + + lifecycle { + ignore_changes = [start_time] + } +} diff --git a/variables.tf b/variables.tf index 69d9975..b9d87f0 100644 --- a/variables.tf +++ b/variables.tf @@ -183,3 +183,45 @@ variable "max_instance_lifetime" { type = string default = "0" } + +variable "scale_down_out_of_hours_enabled" { + description = "Should the ECS cluster scale down outside of normal working hours" + type = bool + default = false +} + +variable "scale_down_min_servers" { + description = "Minimum size to scale down to within the reduced scaling window" + type = number + default = 0 +} + +variable "scale_down_max_servers" { + description = "Maximum size to scale up to within the reduced scaling window" + type = number + default = 0 +} + +variable "scale_down_servers" { + description = "The desired capacity must be equal to or greater than the minimum group size, and equal to or less than the maximum group size" + type = number + default = 0 +} + +variable "scale_down_recurrance" { + description = "Cron expression for when to apply the scaling window. Defaults to 'Daily'" + type = string + default = "0 12 * * *" +} + +variable "scale_down_start_time" { + description = "The time for the recurring schedule to start. Defaults to '23:00'" + type = string + default = "" +} + +variable "scale_down_end_time" { + description = "The time for the recurring schedule to end. Defaults to '06:00'" + type = string + default = "" +}