Skip to content

Commit

Permalink
Add scale out schedules
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmacedot committed Aug 26, 2024
1 parent 3d50ca7 commit e2fe696
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
24 changes: 8 additions & 16 deletions _variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -511,32 +511,24 @@ variable "alarm_ecs_running_tasks_threshold" {
description = "Alarm when the number of ecs service running tasks is lower than a certain value. CloudWatch Container Insights must be enabled for the cluster."
}

variable "enable_schedule" {
default = false
type = bool
description = "Enables schedule to shut down and start up instances outside business hours."
}

variable "scale_in_schedules" {
type = list(object({
cron = string
name = string
}))
default = null
default = null
description = "Cron expression to define when to trigger a stop of the auto-scaling group. E.g. 'cron(00 21 ? * SUN-THU *)' to start at 8am UTC time."
}

variable "schedule_cron_start" {
type = string
default = ""
variable "scale_out_schedules" {
type = list(object({
cron = string
name = string
}))
default = null
description = "Cron expression to define when to trigger a start of the auto-scaling group. E.g. 'cron(00 21 ? * SUN-THU *)' to start at 8am UTC time."
}

variable "schedule_cron_stop" {
type = string
default = ""
description = "Cron expression to define when to trigger a stop of the auto-scaling group. E.g. 'cron(00 09 ? * MON-FRI *)' to start at 8am UTC time"
}

variable "command" {
type = list(string)
default = null
Expand Down
14 changes: 14 additions & 0 deletions appautoscaling.tf
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ resource "aws_appautoscaling_scheduled_action" "scale_in_schedules" {
}
}

resource "aws_appautoscaling_scheduled_action" "scale_out_schedules" {
for_each = { for schedule in var.scale_out_schedules : schedule.name => schedule }
name = "${var.name}-${each.key}-scale-out"
service_namespace = aws_appautoscaling_target.ecs[0].service_namespace
resource_id = aws_appautoscaling_target.ecs[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension
schedule = each.value.cron
timezone = var.schedule_timezone
scalable_target_action {
min_capacity = var.autoscaling_min
max_capacity = var.autoscaling_max
}
}

#resource "aws_appautoscaling_scheduled_action" "scale_service_in" {
# count = var.enable_schedule ? 1 : 0
# name = "${var.name}-scale-in"
Expand Down

0 comments on commit e2fe696

Please sign in to comment.