@@ -5,7 +5,6 @@ resource "aws_appautoscaling_target" "ecs_target" {
5
5
resource_id = " service/${ var . cluster_name } /${ var . name } "
6
6
scalable_dimension = " ecs:service:DesiredCount"
7
7
service_namespace = " ecs"
8
- depends_on = [aws_ecs_service . application ]
9
8
10
9
lifecycle {
11
10
replace_triggered_by = [
@@ -14,17 +13,18 @@ resource "aws_appautoscaling_target" "ecs_target" {
14
13
aws_ecs_service . application . id
15
14
]
16
15
}
16
+
17
+ depends_on = [aws_ecs_service . application ]
17
18
}
18
19
19
- // metric used for auto scale
20
20
resource "aws_cloudwatch_metric_alarm" "service_cpu_high" {
21
21
count = var. scheduling_strategy == " REPLICA" ? 1 : 0
22
22
alarm_name = " ${ local . name_underscore } _cpu_utilization_high"
23
23
comparison_operator = " GreaterThanOrEqualToThreshold"
24
- evaluation_periods = " 1 "
24
+ evaluation_periods = 1
25
25
metric_name = " CPUUtilization"
26
26
namespace = " AWS/ECS"
27
- period = " 60 "
27
+ period = 60
28
28
statistic = " Average"
29
29
threshold = var. scale_up
30
30
@@ -34,17 +34,16 @@ resource "aws_cloudwatch_metric_alarm" "service_cpu_high" {
34
34
}
35
35
36
36
alarm_actions = [aws_appautoscaling_policy . up [0 ]. arn ]
37
- depends_on = [aws_appautoscaling_policy . up [0 ]]
38
37
}
39
38
40
39
resource "aws_cloudwatch_metric_alarm" "service_cpu_low" {
41
40
count = var. scheduling_strategy == " REPLICA" ? 1 : 0
42
41
alarm_name = " ${ local . name_underscore } _cpu_utilization_low"
43
42
comparison_operator = " LessThanThreshold"
44
- evaluation_periods = " 1 "
43
+ evaluation_periods = 1
45
44
metric_name = " CPUUtilization"
46
45
namespace = " AWS/ECS"
47
- period = " 60 "
46
+ period = 60
48
47
statistic = " Average"
49
48
threshold = var. scale_down
50
49
@@ -54,15 +53,54 @@ resource "aws_cloudwatch_metric_alarm" "service_cpu_low" {
54
53
}
55
54
56
55
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"
58
96
}
59
97
60
98
resource "aws_appautoscaling_policy" "up" {
61
99
count = var. scheduling_strategy == " REPLICA" ? 1 : 0
62
100
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
66
104
67
105
step_scaling_policy_configuration {
68
106
adjustment_type = " ChangeInCapacity"
@@ -74,16 +112,14 @@ resource "aws_appautoscaling_policy" "up" {
74
112
scaling_adjustment = 1
75
113
}
76
114
}
77
-
78
- depends_on = [aws_appautoscaling_target . ecs_target ]
79
115
}
80
116
81
117
resource "aws_appautoscaling_policy" "down" {
82
118
count = var. scheduling_strategy == " REPLICA" ? 1 : 0
83
119
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
87
123
88
124
step_scaling_policy_configuration {
89
125
adjustment_type = " ChangeInCapacity"
@@ -95,6 +131,44 @@ resource "aws_appautoscaling_policy" "down" {
95
131
scaling_adjustment = - 1
96
132
}
97
133
}
134
+ }
98
135
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
+ }
100
174
}
0 commit comments