From ae9bcb7658c8edbfd547b05c13f6717b74412a97 Mon Sep 17 00:00:00 2001 From: Karen Shaw Date: Tue, 17 Dec 2024 21:52:57 +0000 Subject: [PATCH] Automatically scale Meadow tasks based on set schedule --- infrastructure/deploy/ecs_services.tf | 41 +++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/infrastructure/deploy/ecs_services.tf b/infrastructure/deploy/ecs_services.tf index 8056e3004..d458d544d 100644 --- a/infrastructure/deploy/ecs_services.tf +++ b/infrastructure/deploy/ecs_services.tf @@ -90,3 +90,44 @@ resource "aws_ecs_service" "meadow_all" { tags = var.tags } + +resource "aws_appautoscaling_target" "ecs_target" { + count = var.environment == "p" ? 1 : 0 + max_capacity = 2 + min_capacity = 1 + resource_id = "service/${ aws_ecs_cluster.meadow.name}/${aws_ecs_service.meadow.name}" + scalable_dimension = "ecs:service:DesiredCount" + service_namespace = "ecs" +} + +# Scheduled action to scale UP at 7:15 AM Central +resource "aws_appautoscaling_scheduled_action" "scale_up" { + count = var.environment == "p" ? 1 : 0 + name = "scale-up-to-2-tasks" + service_namespace = aws_appautoscaling_target.ecs_target.service_namespace + resource_id = aws_appautoscaling_target.ecs_target.resource_id + scalable_dimension = aws_appautoscaling_target.ecs_target.scalable_dimension + + schedule = "cron(15 13 ? * MON-FRI *)" # 7:15 AM Central in UTC (13:15 UTC) + + scalable_target_action { + min_capacity = 2 + max_capacity = 2 + } +} + +# Scheduled action to scale DOWN at 6:00 PM Central +resource "aws_appautoscaling_scheduled_action" "scale_down" { + count = var.environment == "p" ? 1 : 0 + name = "scale-down-to-1-task" + service_namespace = aws_appautoscaling_target.ecs_target.service_namespace + resource_id = aws_appautoscaling_target.ecs_target.resource_id + scalable_dimension = aws_appautoscaling_target.ecs_target.scalable_dimension + + schedule = "cron(0 0 ? * MON-FRI *)" # 6:00 PM Central in UTC (00:00 UTC next day) + + scalable_target_action { + min_capacity = 1 + max_capacity = 1 + } +}