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

ECS Cluster time based autoscaling #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions scheduled-scaling.tf
Original file line number Diff line number Diff line change
@@ -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"]
}
21 changes: 21 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,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
})
)
}