diff --git a/main.tf b/main.tf index 655f92b..e36eb01 100644 --- a/main.tf +++ b/main.tf @@ -85,4 +85,6 @@ 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 = 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 5eb4b01..d7e5e18 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 @@ -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" @@ -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 v.create } + + 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 + + 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..7720b97 100644 --- a/modules/autoscaling-policy/variables.tf +++ b/modules/autoscaling-policy/variables.tf @@ -117,3 +117,25 @@ variable "memory_statistics" { type = string default = "Average" } + +variable "autoscaling_step_size" { + description = "How many containers to spin up each time need autoscale" + type = number + default = 1 +} + +################################################################################ +# Autoscaling scheduler +################################################################################ + +variable "autoscaling_scheduled_actions" { + type = map(object({ + create = bool + min_capacity = number + max_capacity = number + schedule = string + start_time = string + end_time = string + timezone = string + })) +} diff --git a/variables.tf b/variables.tf index 705076c..7fb1324 100644 --- a/variables.tf +++ b/variables.tf @@ -267,3 +267,23 @@ variable "service_scale_out_cooldown" { type = number default = 300 } + + +variable "service_autoscaling_scheduled_actions" { + type = map(object({ + create = bool + min_capacity = number + max_capacity = number + schedule = string + start_time = string + end_time = string + timezone = string + })) +} + + +variable "service_autoscaling_step_size" { + description = "How many containers to spin up each time need autoscale" + type = number + default = 1 +}