-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add an autoscaling module for app-level scaling + setup a cpu-based policy as a starting point * switch to a target tracking policy type * tweak our variable formatting to support using predefined or custom metrics optionally * bump desired count / min capacity to 2 + add an ignore so TF doesn't undo the scaling
- Loading branch information
Showing
5 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Originally inspired by: https://github.com/techservicesillinois/terraform-aws-ecs-service/blob/main/autoscale.tf | ||
|
||
data "aws_partition" "current" {} | ||
data "aws_region" "current" {} | ||
data "aws_caller_identity" "current" {} | ||
|
||
resource "aws_iam_role" "ecs-autoscale-role" { | ||
name = format("%s-%s-app-scaling", var.env, var.service_name) | ||
|
||
assume_role_policy = <<EOF | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Action": "sts:AssumeRole", | ||
"Principal": { | ||
"Service": "application-autoscaling.amazonaws.com" | ||
}, | ||
"Effect": "Allow" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "ecs-autoscale" { | ||
role = aws_iam_role.ecs-autoscale-role.id | ||
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceAutoscaleRole" | ||
} | ||
|
||
resource "aws_appautoscaling_target" "default" { | ||
max_capacity = var.max_capacity | ||
min_capacity = var.min_capacity | ||
resource_id = format("service/%s/%s", var.ecs_cluster, var.service_name) | ||
role_arn = aws_iam_role.ecs-autoscale-role.arn | ||
scalable_dimension = "ecs:service:DesiredCount" | ||
service_namespace = "ecs" | ||
} | ||
|
||
# Target tracking policy | ||
resource "aws_appautoscaling_policy" "default" { | ||
for_each = var.metrics | ||
|
||
name = format("ecs-target-%s-%s", var.service_name, lower(each.key)) | ||
policy_type = "TargetTrackingScaling" | ||
resource_id = aws_appautoscaling_target.default.resource_id | ||
scalable_dimension = aws_appautoscaling_target.default.scalable_dimension | ||
service_namespace = aws_appautoscaling_target.default.service_namespace | ||
|
||
target_tracking_scaling_policy_configuration { | ||
target_value = each.value.target | ||
disable_scale_in = each.value.disable_scale_in | ||
scale_in_cooldown = each.value.scale_in_cooldown | ||
scale_out_cooldown = each.value.scale_out_cooldown | ||
|
||
dynamic predefined_metric_specification { | ||
for_each = each.value.predefined_metric | ||
iterator = metric | ||
|
||
content { | ||
predefined_metric_type = metric.value.type | ||
} | ||
} | ||
|
||
dynamic customized_metric_specification { | ||
for_each = each.value.customized_metric | ||
iterator = metric | ||
|
||
content { | ||
metric_name = metric.value.metric_name | ||
namespace = metric.value.namespace | ||
statistic = metric.value.statistic | ||
unit = metric.value.unit | ||
|
||
dynamic dimensions { | ||
for_each = metric.value.dimensions | ||
iterator = dim | ||
|
||
content { | ||
name = dim.key | ||
value = dim.value | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
variable env { | ||
type = string | ||
description = "Environment name" | ||
} | ||
|
||
variable ecs_cluster { | ||
type = string | ||
} | ||
|
||
variable service_name { | ||
type = string | ||
} | ||
|
||
variable min_capacity { | ||
type = number | ||
default = 1 | ||
} | ||
|
||
variable max_capacity { | ||
type = number | ||
default = 1 | ||
} | ||
|
||
variable "metrics" { | ||
description = "Autoscaling metrics configuration" | ||
type = map( | ||
object({ | ||
target = string | ||
disable_scale_in = optional(bool, false) | ||
scale_in_cooldown = optional(number, 120) | ||
scale_out_cooldown = optional(number, 120) | ||
|
||
predefined_metric = optional(list(object({ | ||
type = string | ||
resource_label = optional(string, null) | ||
})), []) | ||
customized_metric = optional(list(object({ | ||
metric_name = string | ||
namespace = string | ||
statistic = optional(string, "Average") | ||
unit = optional(string, null) | ||
dimensions = optional(map(string), {}) | ||
})), []) | ||
}) | ||
) | ||
default = null | ||
validation { | ||
condition = var.metrics == null || try(length(var.metrics) > 0, true) | ||
error_message = "The 'metrics' block must have one or more metrics" | ||
} | ||
} |