From 04b975cef49ad0191da19420f3bd4087e1763d40 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] ECS Cluster time based autoscaling * Allows configuring time based autoscaling for the Infrastructure ECS Cluster's autoscaling group. * Cron expressions can be provided to set the desired capacity to the min or max sizes. Custom scaling sizes can also be provided, to set the min/max sizes for a given cron expression (in which case the min size will be set as the desired capacity) --- scheduled-scaling.tf | 42 ++++++++++++++++++++++++++++++++++++++++++ variables.tf | 21 +++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 scheduled-scaling.tf diff --git a/scheduled-scaling.tf b/scheduled-scaling.tf new file mode 100644 index 0000000..bf97a05 --- /dev/null +++ b/scheduled-scaling.tf @@ -0,0 +1,42 @@ +locals { + autoscaling_time_based_max = toset(var.autoscaling_time_based_max) + autoscaling_time_based_min = toset(var.autoscaling_time_based_min) + autoscaling_time_based_custom = { + for custom in toset(var.autoscaling_time_based_custom) : "${custom["min"]}-${custom["max"]} ${custom["cron"]}" => custom + } +} + +resource "aws_autoscaling_schedule" "ecs_infrastructure_time_based_max" { + for_each = local.autoscaling_time_based_max + + autoscaling_group_name = aws_autoscaling_group.ecs.name + scheduled_action_name = "asg-${aws_launch_template.ecs.name}-schedule-max ${each.value}" + time_zone = "Europe/London" + desired_capacity = var.max_servers + min_size = -1 + max_size = -1 + recurrence = each.value +} + +resource "aws_autoscaling_schedule" "ecs_infrastructure_time_based_min" { + for_each = local.autoscaling_time_based_min + + autoscaling_group_name = aws_autoscaling_group.ecs.name + scheduled_action_name = "asg-${aws_launch_template.ecs.name}-schedule-min ${each.value}" + time_zone = "Europe/London" + desired_capacity = var.min_servers + min_size = -1 + max_size = -1 + recurrence = each.value +} + +resource "aws_autoscaling_schedule" "ecs_infrastructure_time_based_custom" { + for_each = local.autoscaling_time_based_custom + + autoscaling_group_name = aws_autoscaling_group.ecs.name + scheduled_action_name = "asg-${aws_launch_template.ecs.name}-schedule-custom ${each.value["cron"]} ${each.value["min"]}-${each.value["max"]}" + desired_capacity = each.value["min"] + min_size = each.value["min"] + max_size = each.value["max"] + recurrence = each.value["cron"] +} diff --git a/variables.tf b/variables.tf index 69d9975..f9a89a5 100644 --- a/variables.tf +++ b/variables.tf @@ -183,3 +183,24 @@ variable "max_instance_lifetime" { type = string default = "0" } + +variable "autoscaling_time_based_max" { + description = "List of cron expressions to scale the ECS cluster to the configured max size" + type = list(string) +} + +variable "autoscaling_time_based_min" { + description = "List of cron expressions to scale the ECS cluster to the configured min size" + type = list(string) +} + +variable "autoscaling_time_based_custom" { + description = "List of objects with min/max sizes and cron expressions to scale the ECS cluster. Min size will be used as desired." + type = list( + object({ + cron = string + min = number + max = number + }) + ) +}