Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add appautoscaling scheduled action #15

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
29 changes: 24 additions & 5 deletions modules/autoscaling-policy/main.tf
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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)
}
22 changes: 22 additions & 0 deletions modules/autoscaling-policy/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}))
}
20 changes: 20 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}