-
Notifications
You must be signed in to change notification settings - Fork 4
/
cloudwatch_metrics_ecs.tf
166 lines (143 loc) · 6.67 KB
/
cloudwatch_metrics_ecs.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#########################################
# ECS Cluster CloudWatch Alarm
#########################################
resource "aws_cloudwatch_metric_alarm" "cluster_instance_high_cpu" {
alarm_name = "alarm-${var.env}-${var.project_name}-cluster-CPUReservation-High"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "CPUReservation"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = "80"
dimensions {
ClusterName = "${aws_ecs_cluster.cluster.name}"
}
alarm_actions = ["${aws_autoscaling_policy.cluster_instance_scale_up.arn}", "${aws_sns_topic.cw-alert.arn}"]
ok_actions = ["${aws_sns_topic.cw-alert.arn}"]
depends_on = ["aws_cloudwatch_metric_alarm.cluster_instance_high_cpu"]
}
resource "aws_cloudwatch_metric_alarm" "cluster_instance_low_cpu" {
alarm_name = "alarm-${var.env}-${var.project_name}-cluster-CPUReservation-Low"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "CPUReservation"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = "10"
dimensions {
ClusterName = "${aws_ecs_cluster.cluster.name}"
}
alarm_actions = ["${aws_autoscaling_policy.cluster_instance_scale_down.arn}", "${aws_sns_topic.cw-alert.arn}"]
ok_actions = ["${aws_sns_topic.cw-alert.arn}"]
depends_on = ["aws_cloudwatch_metric_alarm.cluster_instance_low_cpu"]
}
resource "aws_cloudwatch_metric_alarm" "cluster_instance_high_mem" {
alarm_name = "alarm-${var.env}-${var.project_name}-cluster-MemoryReservation-High"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "MemoryReservation"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = "85"
dimensions {
ClusterName = "${aws_ecs_cluster.cluster.name}"
}
alarm_actions = ["${aws_autoscaling_policy.cluster_instance_scale_up.arn}", "${aws_sns_topic.cw-alert.arn}"]
ok_actions = ["${aws_sns_topic.cw-alert.arn}"]
depends_on = ["aws_cloudwatch_metric_alarm.cluster_instance_high_mem"]
}
resource "aws_cloudwatch_metric_alarm" "cluster_instance_low_mem" {
alarm_name = "alarm-${var.env}-${var.project_name}-cluster-MemoryReservation-Low"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "MemoryReservation"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = "10"
dimensions {
ClusterName = "${aws_ecs_cluster.cluster.name}"
}
alarm_actions = ["${aws_autoscaling_policy.cluster_instance_scale_down.arn}", "${aws_sns_topic.cw-alert.arn}"]
ok_actions = ["${aws_sns_topic.cw-alert.arn}"]
depends_on = ["aws_cloudwatch_metric_alarm.cluster_instance_low_mem"]
}
#########################################
# ECS Cluster Scaling Policy
#########################################
resource "aws_autoscaling_policy" "cluster_instance_scale_up" {
name = "asg-${var.env}-${var.project_name}-ScalingPolicyClusterScaleUp"
scaling_adjustment = 2
adjustment_type = "ChangeInCapacity"
cooldown = "300"
autoscaling_group_name = "${aws_autoscaling_group.ecs_cluster.name}"
}
resource "aws_autoscaling_policy" "cluster_instance_scale_down" {
name = "asg-${var.env}-${var.project_name}-ScalingPolicyClusterScaleDown"
scaling_adjustment = -2
adjustment_type = "ChangeInCapacity"
cooldown = "300"
autoscaling_group_name = "${aws_autoscaling_group.ecs_cluster.name}"
}
#########################################
# ECS Service Scaling Policy
#########################################
resource "aws_appautoscaling_target" "nginx_target" {
depends_on = ["aws_ecs_cluster.cluster", "aws_ecs_service.service-nginx"]
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.cluster.name}/${aws_ecs_service.service-nginx.name}"
scalable_dimension = "ecs:service:DesiredCount"
min_capacity = 2
max_capacity = 30
}
resource "aws_appautoscaling_policy" "nginx_scale_cpu" {
depends_on = ["aws_ecs_cluster.cluster", "aws_ecs_service.service-nginx", "aws_appautoscaling_target.nginx_target"]
name = "ECSServiceAverageCPUUtilization:${aws_appautoscaling_target.nginx_target.resource_id}"
resource_id = "${aws_appautoscaling_target.nginx_target.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.nginx_target.scalable_dimension}"
service_namespace = "${aws_appautoscaling_target.nginx_target.service_namespace}"
policy_type = "TargetTrackingScaling"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageCPUUtilization"
}
target_value = 70
scale_in_cooldown = 60
scale_out_cooldown = 300
}
}
resource "aws_appautoscaling_policy" "nginx_scale_memory" {
depends_on = ["aws_ecs_cluster.cluster", "aws_ecs_service.service-nginx", "aws_appautoscaling_target.nginx_target"]
name = "ECSServiceAverageMemoryUtilization:${aws_appautoscaling_target.nginx_target.resource_id}"
resource_id = "${aws_appautoscaling_target.nginx_target.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.nginx_target.scalable_dimension}"
service_namespace = "${aws_appautoscaling_target.nginx_target.service_namespace}"
policy_type = "TargetTrackingScaling"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ECSServiceAverageMemoryUtilization"
}
target_value = 70
scale_in_cooldown = 60
scale_out_cooldown = 300
}
}
resource "aws_appautoscaling_policy" "nginx_scale_count" {
depends_on = ["aws_ecs_cluster.cluster", "aws_ecs_service.service-nginx", "aws_appautoscaling_target.nginx_target"]
name = "ALBRequestCountPerTarget:${aws_appautoscaling_target.nginx_target.resource_id}"
resource_id = "${aws_appautoscaling_target.nginx_target.resource_id}"
scalable_dimension = "${aws_appautoscaling_target.nginx_target.scalable_dimension}"
service_namespace = "${aws_appautoscaling_target.nginx_target.service_namespace}"
policy_type = "TargetTrackingScaling"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = "ALBRequestCountPerTarget"
}
target_value = 100
scale_in_cooldown = 60
scale_out_cooldown = 300
}
}