From b8937a1ec49f7dac58454d5133dca843f0304617 Mon Sep 17 00:00:00 2001 From: panchalnimesh Date: Thu, 20 Jul 2023 23:48:09 +0800 Subject: [PATCH 01/10] add appautoscaling scheduled action --- main.tf | 1 + modules/autoscaling-policy/main.tf | 19 +++++++++++++++++++ modules/autoscaling-policy/variables.tf | 17 +++++++++++++++++ variables.tf | 13 +++++++++++++ 4 files changed, 50 insertions(+) diff --git a/main.tf b/main.tf index 655f92b..f5db7f0 100644 --- a/main.tf +++ b/main.tf @@ -85,4 +85,5 @@ module "service_cpu_autoscaling_policy" { scale_out_cooldown = lookup(each.value, "service_scale_out_cooldown", var.service_scale_out_cooldown) ecs_cluster_name = module.cluster.ecs_cluster_name ecs_service_name = module.service[each.key].ecs_service_name + autoscaling_scheduled_actions = lookup(each.value, "service_autoscaling_scheduled_actions", var.service_autoscaling_scheduled_actions) } diff --git a/modules/autoscaling-policy/main.tf b/modules/autoscaling-policy/main.tf index 5eb4b01..beb8446 100644 --- a/modules/autoscaling-policy/main.tf +++ b/modules/autoscaling-policy/main.tf @@ -97,3 +97,22 @@ resource "aws_autoscaling_policy" "asg_memory_autoscaling" { } } } + +resource "aws_appautoscaling_scheduled_action" "this" { + for_each = { for k, v in var.autoscaling_scheduled_actions : k => v if lookup(v, "create", true) } + + name = "${var.name}-scheduler" + service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace + resource_id = aws_appautoscaling_target.ecs_target[0].resource_id + scalable_dimension = aws_appautoscaling_target.ecs_target[0].scalable_dimension + + scalable_target_action { + min_capacity = each.value.min_capacity + max_capacity = each.value.max_capacity + } + + schedule = each.value.schedule + start_time = try(each.value.start_time, null) + end_time = try(each.value.end_time, null) + timezone = try(each.value.timezone, null) +} diff --git a/modules/autoscaling-policy/variables.tf b/modules/autoscaling-policy/variables.tf index 32ac9a2..799d73b 100644 --- a/modules/autoscaling-policy/variables.tf +++ b/modules/autoscaling-policy/variables.tf @@ -117,3 +117,20 @@ variable "memory_statistics" { type = string default = "Average" } + +################################################################################ +# Autoscaling scheduler +################################################################################ + +variable "autoscaling_scheduled_actions" { + type = map(object({ + create = bool + desired_capacity = number + min_size = number + max_size = number + recurrence = string + start_time = string + end_time = string + suspend_actions = list(string) + })) +} diff --git a/variables.tf b/variables.tf index 705076c..cc9999b 100644 --- a/variables.tf +++ b/variables.tf @@ -267,3 +267,16 @@ variable "service_scale_out_cooldown" { type = number default = 300 } + +variable "service_autoscaling_scheduled_actions" { + type = map(object({ + create = bool + desired_capacity = number + min_size = number + max_size = number + recurrence = string + start_time = string + end_time = string + suspend_actions = list(string) + })) +} From 98f15ff0ef3301764b992c35c6a2630bbd02709a Mon Sep 17 00:00:00 2001 From: panchalnimesh Date: Fri, 21 Jul 2023 00:43:52 +0800 Subject: [PATCH 02/10] cosmatic change --- modules/autoscaling-policy/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/autoscaling-policy/main.tf b/modules/autoscaling-policy/main.tf index beb8446..9133966 100644 --- a/modules/autoscaling-policy/main.tf +++ b/modules/autoscaling-policy/main.tf @@ -99,7 +99,7 @@ resource "aws_autoscaling_policy" "asg_memory_autoscaling" { } resource "aws_appautoscaling_scheduled_action" "this" { - for_each = { for k, v in var.autoscaling_scheduled_actions : k => v if lookup(v, "create", true) } + for_each = { for k, v in var.autoscaling_scheduled_actions : k => v if v.create } name = "${var.name}-scheduler" service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace From fff05ba5f291ffc28a98a36556e1480dd612eca9 Mon Sep 17 00:00:00 2001 From: panchalnimesh Date: Fri, 21 Jul 2023 01:00:49 +0800 Subject: [PATCH 03/10] passing correct variables --- modules/autoscaling-policy/variables.tf | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/modules/autoscaling-policy/variables.tf b/modules/autoscaling-policy/variables.tf index 799d73b..eda40bf 100644 --- a/modules/autoscaling-policy/variables.tf +++ b/modules/autoscaling-policy/variables.tf @@ -124,13 +124,12 @@ variable "memory_statistics" { variable "autoscaling_scheduled_actions" { type = map(object({ - create = bool - desired_capacity = number - min_size = number - max_size = number - recurrence = string - start_time = string - end_time = string - suspend_actions = list(string) + create = bool + min_capacity = number + max_capacity = number + schedule = string + start_time = string + end_time = string + timezone = string })) } From 59922ac712448b3291116cc37a13a49e3b8c5381 Mon Sep 17 00:00:00 2001 From: panchalnimesh Date: Fri, 21 Jul 2023 01:38:12 +0800 Subject: [PATCH 04/10] refactor code --- main.tf | 2 +- modules/autoscaling-policy/variables.tf | 2 +- variables.tf | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/main.tf b/main.tf index f5db7f0..abf240f 100644 --- a/main.tf +++ b/main.tf @@ -85,5 +85,5 @@ module "service_cpu_autoscaling_policy" { scale_out_cooldown = lookup(each.value, "service_scale_out_cooldown", var.service_scale_out_cooldown) ecs_cluster_name = module.cluster.ecs_cluster_name ecs_service_name = module.service[each.key].ecs_service_name - autoscaling_scheduled_actions = lookup(each.value, "service_autoscaling_scheduled_actions", var.service_autoscaling_scheduled_actions) + autoscaling_scheduled_actions = var.service_autoscaling_scheduled_actions } diff --git a/modules/autoscaling-policy/variables.tf b/modules/autoscaling-policy/variables.tf index eda40bf..1d023a2 100644 --- a/modules/autoscaling-policy/variables.tf +++ b/modules/autoscaling-policy/variables.tf @@ -130,6 +130,6 @@ variable "autoscaling_scheduled_actions" { schedule = string start_time = string end_time = string - timezone = string + timezone = "Asia/Singapore" })) } diff --git a/variables.tf b/variables.tf index cc9999b..4e92fcb 100644 --- a/variables.tf +++ b/variables.tf @@ -268,15 +268,15 @@ variable "service_scale_out_cooldown" { default = 300 } + variable "service_autoscaling_scheduled_actions" { type = map(object({ - create = bool - desired_capacity = number - min_size = number - max_size = number - recurrence = string - start_time = string - end_time = string - suspend_actions = list(string) + create = bool + min_capacity = number + max_capacity = number + schedule = string + start_time = string + end_time = string + timezone = string })) } From c0f6fc2ec92b8d75acfb098149f78e26733a9060 Mon Sep 17 00:00:00 2001 From: panchalnimesh Date: Fri, 21 Jul 2023 01:48:02 +0800 Subject: [PATCH 05/10] change variable value --- modules/autoscaling-policy/variables.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/autoscaling-policy/variables.tf b/modules/autoscaling-policy/variables.tf index 1d023a2..eda40bf 100644 --- a/modules/autoscaling-policy/variables.tf +++ b/modules/autoscaling-policy/variables.tf @@ -130,6 +130,6 @@ variable "autoscaling_scheduled_actions" { schedule = string start_time = string end_time = string - timezone = "Asia/Singapore" + timezone = string })) } From 09b38ba3003fee689be8556e9493ecea02f32f30 Mon Sep 17 00:00:00 2001 From: Nimesh Panchal Date: Fri, 8 Sep 2023 10:22:14 +0800 Subject: [PATCH 06/10] add schedule_names as additional parameter to create multi-schedules --- main.tf | 1 + modules/autoscaling-policy/main.tf | 2 +- variables.tf | 6 ++++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/main.tf b/main.tf index abf240f..fc6dfd6 100644 --- a/main.tf +++ b/main.tf @@ -86,4 +86,5 @@ module "service_cpu_autoscaling_policy" { ecs_cluster_name = module.cluster.ecs_cluster_name ecs_service_name = module.service[each.key].ecs_service_name autoscaling_scheduled_actions = var.service_autoscaling_scheduled_actions + schedule_names = var.schedule_names } diff --git a/modules/autoscaling-policy/main.tf b/modules/autoscaling-policy/main.tf index 9133966..a3b20d4 100644 --- a/modules/autoscaling-policy/main.tf +++ b/modules/autoscaling-policy/main.tf @@ -101,7 +101,7 @@ resource "aws_autoscaling_policy" "asg_memory_autoscaling" { resource "aws_appautoscaling_scheduled_action" "this" { for_each = { for k, v in var.autoscaling_scheduled_actions : k => v if v.create } - name = "${var.name}-scheduler" + name = var.schedule_names[each.key] service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace resource_id = aws_appautoscaling_target.ecs_target[0].resource_id scalable_dimension = aws_appautoscaling_target.ecs_target[0].scalable_dimension diff --git a/variables.tf b/variables.tf index 4e92fcb..15fe86d 100644 --- a/variables.tf +++ b/variables.tf @@ -280,3 +280,9 @@ variable "service_autoscaling_scheduled_actions" { timezone = string })) } + +variable "schedule_names" { + description = "Names of the schedules" + type = list(string) + default = [] +} From 1cec50abeca0e9a83bc2fec5e4db7db3773e583c Mon Sep 17 00:00:00 2001 From: Nimesh Panchal Date: Fri, 8 Sep 2023 10:53:09 +0800 Subject: [PATCH 07/10] cosmatic change --- main.tf | 1 - modules/autoscaling-policy/main.tf | 2 +- variables.tf | 6 ------ 3 files changed, 1 insertion(+), 8 deletions(-) diff --git a/main.tf b/main.tf index fc6dfd6..abf240f 100644 --- a/main.tf +++ b/main.tf @@ -86,5 +86,4 @@ module "service_cpu_autoscaling_policy" { ecs_cluster_name = module.cluster.ecs_cluster_name ecs_service_name = module.service[each.key].ecs_service_name autoscaling_scheduled_actions = var.service_autoscaling_scheduled_actions - schedule_names = var.schedule_names } diff --git a/modules/autoscaling-policy/main.tf b/modules/autoscaling-policy/main.tf index a3b20d4..b1ad380 100644 --- a/modules/autoscaling-policy/main.tf +++ b/modules/autoscaling-policy/main.tf @@ -101,7 +101,7 @@ resource "aws_autoscaling_policy" "asg_memory_autoscaling" { resource "aws_appautoscaling_scheduled_action" "this" { for_each = { for k, v in var.autoscaling_scheduled_actions : k => v if v.create } - name = var.schedule_names[each.key] + name = "${var.name}-${each.key}" service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace resource_id = aws_appautoscaling_target.ecs_target[0].resource_id scalable_dimension = aws_appautoscaling_target.ecs_target[0].scalable_dimension diff --git a/variables.tf b/variables.tf index 15fe86d..4e92fcb 100644 --- a/variables.tf +++ b/variables.tf @@ -280,9 +280,3 @@ variable "service_autoscaling_scheduled_actions" { timezone = string })) } - -variable "schedule_names" { - description = "Names of the schedules" - type = list(string) - default = [] -} From a15331786030b3dd6ca6db8f848b7334b01f0f48 Mon Sep 17 00:00:00 2001 From: Nimesh Panchal Date: Tue, 12 Sep 2023 09:50:20 +0800 Subject: [PATCH 08/10] add step size scaling to test --- main.tf | 1 + modules/autoscaling-policy/main.tf | 4 ++-- modules/autoscaling-policy/variables.tf | 6 ++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/main.tf b/main.tf index abf240f..e36eb01 100644 --- a/main.tf +++ b/main.tf @@ -86,4 +86,5 @@ module "service_cpu_autoscaling_policy" { ecs_cluster_name = module.cluster.ecs_cluster_name ecs_service_name = module.service[each.key].ecs_service_name autoscaling_scheduled_actions = var.service_autoscaling_scheduled_actions + autoscaling_step_size = var.service_autoscaling_step_size } diff --git a/modules/autoscaling-policy/main.tf b/modules/autoscaling-policy/main.tf index b1ad380..6a582a3 100644 --- a/modules/autoscaling-policy/main.tf +++ b/modules/autoscaling-policy/main.tf @@ -1,5 +1,5 @@ resource "aws_appautoscaling_target" "ecs_target" { - count = var.enable_ecs_cpu_based_autoscaling || var.enable_ecs_memory_based_autoscaling ? 1 : 0 + count = var.enable_ecs_cpu_based_autoscaling || var.enable_ecs_memory_based_autoscaling ? var.autoscaling_step_size : 0 min_capacity = var.min_capacity max_capacity = var.max_capacity @@ -9,7 +9,7 @@ resource "aws_appautoscaling_target" "ecs_target" { } resource "aws_appautoscaling_policy" "ecs_service_cpu_policy" { - count = var.enable_ecs_cpu_based_autoscaling ? 1 : 0 + count = var.enable_ecs_cpu_based_autoscaling ? var.autoscaling_step_size : 0 name = "${var.name}-service-cpu" resource_id = aws_appautoscaling_target.ecs_target[0].resource_id diff --git a/modules/autoscaling-policy/variables.tf b/modules/autoscaling-policy/variables.tf index eda40bf..7720b97 100644 --- a/modules/autoscaling-policy/variables.tf +++ b/modules/autoscaling-policy/variables.tf @@ -118,6 +118,12 @@ variable "memory_statistics" { default = "Average" } +variable "autoscaling_step_size" { + description = "How many containers to spin up each time need autoscale" + type = number + default = 1 +} + ################################################################################ # Autoscaling scheduler ################################################################################ From 3a643062bde510298f9da02913844c2d32f84940 Mon Sep 17 00:00:00 2001 From: Nimesh Panchal Date: Tue, 12 Sep 2023 10:03:17 +0800 Subject: [PATCH 09/10] add missing saved file --- modules/autoscaling-policy/main.tf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/autoscaling-policy/main.tf b/modules/autoscaling-policy/main.tf index 6a582a3..d7e5e18 100644 --- a/modules/autoscaling-policy/main.tf +++ b/modules/autoscaling-policy/main.tf @@ -30,7 +30,7 @@ resource "aws_appautoscaling_policy" "ecs_service_cpu_policy" { } resource "aws_appautoscaling_policy" "ecs_service_memory_policy" { - count = var.enable_ecs_memory_based_autoscaling ? 1 : 0 + count = var.enable_ecs_memory_based_autoscaling ? var.autoscaling_step_size : 0 name = "${var.name}-service-memory" resource_id = aws_appautoscaling_target.ecs_target[0].resource_id @@ -51,7 +51,7 @@ resource "aws_appautoscaling_policy" "ecs_service_memory_policy" { } resource "aws_autoscaling_policy" "asg_cpu_autoscaling" { - count = var.enable_asg_cpu_based_autoscaling ? 1 : 0 + count = var.enable_asg_cpu_based_autoscaling ? var.autoscaling_step_size : 0 name = "${var.name}-asg-cpu" policy_type = "TargetTrackingScaling" @@ -75,7 +75,7 @@ resource "aws_autoscaling_policy" "asg_cpu_autoscaling" { } resource "aws_autoscaling_policy" "asg_memory_autoscaling" { - count = var.enable_asg_memory_based_autoscaling ? 1 : 0 + count = var.enable_asg_memory_based_autoscaling ? var.autoscaling_step_size : 0 name = "${var.name}-asg-memory" policy_type = "TargetTrackingScaling" From d0f859b830606c49a1a5c57b4d507d2518044153 Mon Sep 17 00:00:00 2001 From: Nimesh Panchal Date: Tue, 12 Sep 2023 10:48:34 +0800 Subject: [PATCH 10/10] fix variables --- variables.tf | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/variables.tf b/variables.tf index 4e92fcb..7fb1324 100644 --- a/variables.tf +++ b/variables.tf @@ -280,3 +280,10 @@ variable "service_autoscaling_scheduled_actions" { timezone = string })) } + + +variable "service_autoscaling_step_size" { + description = "How many containers to spin up each time need autoscale" + type = number + default = 1 +}