Skip to content

Commit f69efb5

Browse files
authored
Merge pull request #15 from druids/feat/custom-autoscaling-support
SQS autoscaling support
2 parents d9df8a9 + 4534e2f commit f69efb5

File tree

4 files changed

+124
-18
lines changed

4 files changed

+124
-18
lines changed

autoscaling.tf

Lines changed: 91 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ resource "aws_appautoscaling_target" "ecs_target" {
55
resource_id = "service/${var.cluster_name}/${var.name}"
66
scalable_dimension = "ecs:service:DesiredCount"
77
service_namespace = "ecs"
8-
depends_on = [aws_ecs_service.application]
98

109
lifecycle {
1110
replace_triggered_by = [
@@ -14,17 +13,18 @@ resource "aws_appautoscaling_target" "ecs_target" {
1413
aws_ecs_service.application.id
1514
]
1615
}
16+
17+
depends_on = [aws_ecs_service.application]
1718
}
1819

19-
// metric used for auto scale
2020
resource "aws_cloudwatch_metric_alarm" "service_cpu_high" {
2121
count = var.scheduling_strategy == "REPLICA" ? 1 : 0
2222
alarm_name = "${local.name_underscore}_cpu_utilization_high"
2323
comparison_operator = "GreaterThanOrEqualToThreshold"
24-
evaluation_periods = "1"
24+
evaluation_periods = 1
2525
metric_name = "CPUUtilization"
2626
namespace = "AWS/ECS"
27-
period = "60"
27+
period = 60
2828
statistic = "Average"
2929
threshold = var.scale_up
3030

@@ -34,17 +34,16 @@ resource "aws_cloudwatch_metric_alarm" "service_cpu_high" {
3434
}
3535

3636
alarm_actions = [aws_appautoscaling_policy.up[0].arn]
37-
depends_on = [aws_appautoscaling_policy.up[0]]
3837
}
3938

4039
resource "aws_cloudwatch_metric_alarm" "service_cpu_low" {
4140
count = var.scheduling_strategy == "REPLICA" ? 1 : 0
4241
alarm_name = "${local.name_underscore}_cpu_utilization_low"
4342
comparison_operator = "LessThanThreshold"
44-
evaluation_periods = "1"
43+
evaluation_periods = 1
4544
metric_name = "CPUUtilization"
4645
namespace = "AWS/ECS"
47-
period = "60"
46+
period = 60
4847
statistic = "Average"
4948
threshold = var.scale_down
5049

@@ -54,15 +53,54 @@ resource "aws_cloudwatch_metric_alarm" "service_cpu_low" {
5453
}
5554

5655
alarm_actions = [aws_appautoscaling_policy.down[0].arn]
57-
depends_on = [aws_appautoscaling_policy.down[0]]
56+
}
57+
58+
resource "aws_cloudwatch_metric_alarm" "sqs_up" {
59+
count = var.scheduling_strategy == "REPLICA" && var.sqs_scaling_enabled ? 1 : 0
60+
61+
alarm_name = "${local.name}-up"
62+
comparison_operator = "GreaterThanOrEqualToThreshold"
63+
evaluation_periods = var.sqs_scaling_evaluation_periods
64+
metric_name = var.sqs_scaling_metric_name
65+
namespace = "AWS/SQS"
66+
period = var.sqs_scaling_period
67+
statistic = "Maximum"
68+
threshold = var.sqs_scaling_threshold
69+
70+
dimensions = {
71+
QueueName = var.sqs_scaling_queue_name
72+
}
73+
74+
alarm_actions = [aws_appautoscaling_policy.sqs_up[0].arn]
75+
alarm_description = "Monitors ApproximateAgeOfOldestMessage in SQS queue"
76+
}
77+
78+
resource "aws_cloudwatch_metric_alarm" "sqs_down" {
79+
count = var.scheduling_strategy == "REPLICA" && var.sqs_scaling_enabled ? 1 : 0
80+
81+
alarm_name = "${local.name}-down"
82+
comparison_operator = "LessThanThreshold"
83+
evaluation_periods = var.sqs_scaling_evaluation_periods
84+
metric_name = var.sqs_scaling_metric_name
85+
namespace = "AWS/SQS"
86+
period = var.sqs_scaling_period
87+
statistic = "Maximum"
88+
threshold = var.sqs_scaling_threshold
89+
90+
dimensions = {
91+
QueueName = var.sqs_scaling_queue_name
92+
}
93+
94+
alarm_actions = [aws_appautoscaling_policy.sqs_down[0].arn]
95+
alarm_description = "Monitors ApproximateAgeOfOldestMessage in SQS queue"
5896
}
5997

6098
resource "aws_appautoscaling_policy" "up" {
6199
count = var.scheduling_strategy == "REPLICA" ? 1 : 0
62100
name = "${local.name_underscore}_scale_up"
63-
service_namespace = "ecs"
64-
resource_id = "service/${var.cluster_name}/${var.name}"
65-
scalable_dimension = "ecs:service:DesiredCount"
101+
resource_id = aws_appautoscaling_target.ecs_target[0].resource_id
102+
scalable_dimension = aws_appautoscaling_target.ecs_target[0].scalable_dimension
103+
service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace
66104

67105
step_scaling_policy_configuration {
68106
adjustment_type = "ChangeInCapacity"
@@ -74,16 +112,14 @@ resource "aws_appautoscaling_policy" "up" {
74112
scaling_adjustment = 1
75113
}
76114
}
77-
78-
depends_on = [aws_appautoscaling_target.ecs_target]
79115
}
80116

81117
resource "aws_appautoscaling_policy" "down" {
82118
count = var.scheduling_strategy == "REPLICA" ? 1 : 0
83119
name = "${local.name_underscore}_scale_down"
84-
service_namespace = "ecs"
85-
resource_id = "service/${var.cluster_name}/${var.name}"
86-
scalable_dimension = "ecs:service:DesiredCount"
120+
resource_id = aws_appautoscaling_target.ecs_target[0].resource_id
121+
scalable_dimension = aws_appautoscaling_target.ecs_target[0].scalable_dimension
122+
service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace
87123

88124
step_scaling_policy_configuration {
89125
adjustment_type = "ChangeInCapacity"
@@ -95,6 +131,44 @@ resource "aws_appautoscaling_policy" "down" {
95131
scaling_adjustment = -1
96132
}
97133
}
134+
}
98135

99-
depends_on = [aws_appautoscaling_target.ecs_target]
136+
resource "aws_appautoscaling_policy" "sqs_up" {
137+
count = var.scheduling_strategy == "REPLICA" && var.sqs_scaling_enabled ? 1 : 0
138+
139+
name = "${local.name_underscore}_sqs_up"
140+
resource_id = aws_appautoscaling_target.ecs_target[0].resource_id
141+
scalable_dimension = aws_appautoscaling_target.ecs_target[0].scalable_dimension
142+
service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace
143+
144+
step_scaling_policy_configuration {
145+
adjustment_type = "ChangeInCapacity"
146+
cooldown = var.cooldown
147+
metric_aggregation_type = "Maximum"
148+
149+
step_adjustment {
150+
metric_interval_lower_bound = 0
151+
scaling_adjustment = 1
152+
}
153+
}
154+
}
155+
156+
resource "aws_appautoscaling_policy" "sqs_down" {
157+
count = var.scheduling_strategy == "REPLICA" && var.sqs_scaling_enabled ? 1 : 0
158+
159+
name = "${local.name_underscore}_sqs_down"
160+
resource_id = aws_appautoscaling_target.ecs_target[0].resource_id
161+
scalable_dimension = aws_appautoscaling_target.ecs_target[0].scalable_dimension
162+
service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace
163+
164+
step_scaling_policy_configuration {
165+
adjustment_type = "ChangeInCapacity"
166+
cooldown = var.cooldown
167+
metric_aggregation_type = "Maximum"
168+
169+
step_adjustment {
170+
metric_interval_lower_bound = 0
171+
scaling_adjustment = -1
172+
}
173+
}
100174
}

container.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,7 @@ module "container_definition_nginx" { // Nginx task
121121
awslogs-stream-prefix = "ecs"
122122
}
123123
}
124+
125+
environment = []
126+
secrets = []
124127
}

service.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ resource "aws_ecs_service" "application" {
3737
}
3838
}
3939

40-
4140
lifecycle {
4241
ignore_changes = [desired_count]
4342
}

variables.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,33 @@ variable "image_scanning" {
267267
default = false
268268
type = bool
269269
}
270+
271+
variable "sqs_scaling_enabled" {
272+
default = false
273+
type = bool
274+
}
275+
276+
variable "sqs_scaling_evaluation_periods" {
277+
default = 60
278+
type = number
279+
}
280+
281+
variable "sqs_scaling_metric_name" {
282+
default = "ApproximateAgeOfOldestMessage"
283+
type = string
284+
}
285+
286+
variable "sqs_scaling_period" {
287+
default = 120
288+
type = number
289+
}
290+
291+
variable "sqs_scaling_queue_name" {
292+
default = null
293+
type = string
294+
}
295+
296+
variable "sqs_scaling_threshold" {
297+
default = 60
298+
type = number
299+
}

0 commit comments

Comments
 (0)