Skip to content

Commit

Permalink
Set up a scale-in window
Browse files Browse the repository at this point in the history
  • Loading branch information
DrizzlyOwl committed Jun 21, 2024
1 parent 2e6351f commit 9f01f15
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
39 changes: 39 additions & 0 deletions scheduled-scaling.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
locals {
today_date = formatdate("YYYY-MM-DD", timestamp())
start_time_window = var.scale_down_start_time != "" ? "${local.today_date}T${var.scale_down_start_time}Z" : "${local.today_date}T23:00:00Z"
end_time_window = var.scale_down_end_time != "" ? "${local.today_date}T${var.scale_down_end_time}Z" : "${local.today_date}T06:00:00Z"
}

resource "aws_autoscaling_schedule" "off" {
count = var.scale_down_out_of_hours_enabled ? 1 : 0

scheduled_action_name = "asg-${aws_launch_template.ecs.name}-schedule-down"
min_size = var.scale_down_min_servers
max_size = var.scale_down_max_servers
desired_capacity = var.scale_down_servers
recurrence = var.scale_down_recurrance
start_time = local.start_time_window
autoscaling_group_name = aws_autoscaling_group.ecs.name
time_zone = "Europe/London"

lifecycle {
ignore_changes = [start_time]
}
}

resource "aws_autoscaling_schedule" "on" {
count = var.scale_down_out_of_hours_enabled ? 1 : 0

scheduled_action_name = "asg-${aws_launch_template.ecs.name}-schedule-up"
min_size = var.min_servers
max_size = var.max_servers
desired_capacity = var.servers
start_time = local.end_time_window
recurrence = var.scale_down_recurrance
autoscaling_group_name = aws_autoscaling_group.ecs.name
time_zone = "Europe/London"

lifecycle {
ignore_changes = [start_time]
}
}
42 changes: 42 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,45 @@ variable "max_instance_lifetime" {
type = string
default = "0"
}

variable "scale_down_out_of_hours_enabled" {
description = "Should the ECS cluster scale down outside of normal working hours"
type = bool
default = false
}

variable "scale_down_min_servers" {
description = "Minimum size to scale down to within the reduced scaling window"
type = number
default = 0
}

variable "scale_down_max_servers" {
description = "Maximum size to scale up to within the reduced scaling window"
type = number
default = 0
}

variable "scale_down_servers" {
description = "The desired capacity must be equal to or greater than the minimum group size, and equal to or less than the maximum group size"
type = number
default = 0
}

variable "scale_down_recurrance" {
description = "Cron expression for when to apply the scaling window. Defaults to 'Daily'"
type = string
default = "0 12 * * *"
}

variable "scale_down_start_time" {
description = "The time for the recurring schedule to start. Defaults to '23:00'"
type = string
default = ""
}

variable "scale_down_end_time" {
description = "The time for the recurring schedule to end. Defaults to '06:00'"
type = string
default = ""
}

0 comments on commit 9f01f15

Please sign in to comment.