Skip to content

Commit

Permalink
adding auto scalling feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dnx-solutions-ms committed Mar 27, 2024
1 parent 0886ff7 commit 876ba5d
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 1 deletion.
74 changes: 73 additions & 1 deletion _variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,76 @@ variable "task_role_policies_managed" {
variable "task_role_policies" {
default = []
description = "Custom policies to be added on the task role."
}
}

variable "autoscaling_cpu" {
default = false
description = "Enables autoscaling based on average CPU tracking"
}

variable "autoscaling_memory" {
default = false
description = "Enables autoscaling based on average Memory tracking"
}

variable "autoscaling_max" {
default = 4
description = "Max number of containers to scale with autoscaling"
}

variable "autoscaling_min" {
default = 1
description = "Min number of containers to scale with autoscaling"
}

variable "autoscaling_target_cpu" {
default = 50
description = "Target average CPU percentage to track for autoscaling"
}

variable "autoscaling_target_memory" {
default = 90
description = "Target average Memory percentage to track for autoscaling"
}

variable "autoscaling_scale_in_cooldown" {
default = 300
description = "Cooldown in seconds to wait between scale in events"
}

variable "autoscaling_scale_out_cooldown" {
default = 300
description = "Cooldown in seconds to wait between scale out events"
}

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

variable "autoscaling_custom" {
type = list(object({
name = string
scale_in_cooldown = number
scale_out_cooldown = number
target_value = number
metric_name = string
namespace = string
statistic = string
}))
default = []
description = "Set one or more app autoscaling by customized metric"
}

variable "schedule_cron_start" {
type = string
default = ""
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"
}

108 changes: 108 additions & 0 deletions appautoscaling.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
resource "aws_appautoscaling_target" "ecs" {
count = var.autoscaling_cpu || var.autoscaling_memory || length(var.autoscaling_custom) > 0 ? 1 : 0
max_capacity = var.autoscaling_max
min_capacity = var.autoscaling_min
resource_id = "service/${var.cluster_name}/${aws_ecs_service.default.name}"
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}

resource "aws_appautoscaling_policy" "scale_cpu" {
count = var.autoscaling_cpu ? 1 : 0
name = "scale-cpu"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.ecs[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension
service_namespace = aws_appautoscaling_target.ecs[0].service_namespace

target_tracking_scaling_policy_configuration {
target_value = var.autoscaling_target_cpu
disable_scale_in = false
scale_in_cooldown = var.autoscaling_scale_in_cooldown
scale_out_cooldown = var.autoscaling_scale_out_cooldown

predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
}
}

resource "aws_appautoscaling_policy" "scale_memory" {
count = var.autoscaling_memory ? 1 : 0
name = "scale-memory"
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.ecs[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension
service_namespace = aws_appautoscaling_target.ecs[0].service_namespace

target_tracking_scaling_policy_configuration {
target_value = var.autoscaling_target_memory
disable_scale_in = false
scale_in_cooldown = var.autoscaling_scale_in_cooldown
scale_out_cooldown = var.autoscaling_scale_out_cooldown

predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageMemoryUtilization"
}
}
}

resource "aws_appautoscaling_policy" "scale_custom" {
for_each = { for custom in var.autoscaling_custom : custom.name => custom }

name = each.value.name
policy_type = "TargetTrackingScaling"
resource_id = aws_appautoscaling_target.ecs[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs[0].scalable_dimension
service_namespace = aws_appautoscaling_target.ecs[0].service_namespace

target_tracking_scaling_policy_configuration {
scale_in_cooldown = each.value.scale_in_cooldown
scale_out_cooldown = each.value.scale_out_cooldown
target_value = each.value.target_value

customized_metric_specification {
metric_name = each.value.metric_name
namespace = each.value.namespace
statistic = each.value.statistic
dimensions {
name = "ClusterName"
value = var.cluster_name
}
dimensions {
name = "ServiceName"
value = var.name
}
}
}
}

resource "aws_appautoscaling_scheduled_action" "scale_service_out" {
count = var.enable_schedule ? 1 : 0
name = "${var.name}-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 = var.schedule_cron_stop
timezone = "UTC"

scalable_target_action {
min_capacity = 0
max_capacity = 0
}
}

resource "aws_appautoscaling_scheduled_action" "scale_service_in" {
count = var.enable_schedule ? 1 : 0
name = "${var.name}-scale-in"
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 = var.schedule_cron_start
timezone = "UTC"

scalable_target_action {
min_capacity = var.autoscaling_min
max_capacity = var.autoscaling_max
}
}

0 comments on commit 876ba5d

Please sign in to comment.