-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathautoscaling.tf
85 lines (73 loc) · 2.83 KB
/
autoscaling.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
resource "aws_cloudwatch_metric_alarm" "service_cpu_high" {
alarm_name = "${var.project}-${var.environment}-service-CPU-High"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "${var.cpu_up_evaluation_periods}"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "${var.cpu_up_time_period}"
statistic = "Average"
threshold = "${var.cpu_threshold_up}"
dimensions {
ClusterName = "${var.cluster_name}"
ServiceName = "${aws_ecs_service.main.name}"
}
alarm_actions = ["${aws_appautoscaling_policy.cpu_up.arn}"]
}
resource "aws_cloudwatch_metric_alarm" "service_cpu_low" {
alarm_name = "${var.project}-${var.environment}-service-CPU-Low"
comparison_operator = "LessThanThreshold"
evaluation_periods = "${var.cpu_down_evaluation_periods}"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "${var.cpu_down_time_period}"
statistic = "Average"
threshold = "${var.cpu_threshold_down}"
dimensions {
ClusterName = "${var.cluster_name}"
ServiceName = "${aws_ecs_service.main.name}"
}
alarm_actions = ["${aws_appautoscaling_policy.cpu_down.arn}"]
}
resource "aws_appautoscaling_target" "scale_target" {
service_namespace = "ecs"
resource_id = "service/${var.cluster_name}/${aws_ecs_service.main.name}"
scalable_dimension = "ecs:service:DesiredCount"
role_arn = "${aws_iam_role.ecs_autoscale_role.arn}"
min_capacity = "${var.service_asg_min_cap}"
max_capacity = "${var.service_asg_max_cap}"
depends_on = [
"aws_ecs_service.main"
]
}
resource "aws_appautoscaling_policy" "cpu_up" {
name = "${var.project}-${var.environment}-scale-up"
service_namespace = "ecs"
resource_id = "service/${var.cluster_name}/${aws_ecs_service.main.name}"
scalable_dimension = "ecs:service:DesiredCount"
adjustment_type = "ChangeInCapacity"
cooldown = "${var.cpu_up_cooldown}"
metric_aggregation_type = "Average"
step_adjustment {
metric_interval_lower_bound = 0
scaling_adjustment = 1
}
depends_on = [
"aws_appautoscaling_target.scale_target"
]
}
resource "aws_appautoscaling_policy" "cpu_down" {
name = "${var.project}-${var.environment}-scale-down"
service_namespace = "ecs"
resource_id = "service/${var.cluster_name}/${aws_ecs_service.main.name}"
scalable_dimension = "ecs:service:DesiredCount"
adjustment_type = "ChangeInCapacity"
cooldown = "${var.cpu_down_cooldown}"
metric_aggregation_type = "Average"
step_adjustment {
metric_interval_lower_bound = 0
scaling_adjustment = -1
}
depends_on = [
"aws_appautoscaling_target.scale_target"
]
}