-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoscale-perf.tf
115 lines (99 loc) · 4.21 KB
/
autoscale-perf.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
/**
* This module sets up CPU-based autoscaling. The number of containers
* is kept strictly within the range you specify. Within that range,
* the number is gradually increased or decreased to keep CPU utilization
* within its own range. If your app is CPU-bound, the number of
* instances will autoscale with your traffic.
*
* The adjustments are gradual. If you expect a sudden surge of
* traffic for a scheduled event (such as sporting events or elections),
* set the `ecs_autoscale_min_instances` variable to a higher number.
* `ecs_autoscale_max_instances` might also need to be increased, because
* it should never be below ecs_autoscale_min_instances.
*
* To effectively disable autoscaling, set `ecs_autoscale_min_instances`
* and `ecs_autoscale_max_instances` to the same number (your desired
* number of containers).
*
* Note the default value of `ecs_autoscale_min_instances` is 1. For
* production, consider using a higher number.
*
* There should be a [considerable gap](https://en.wikipedia.org/wiki/Deadband) between
* `scaling_cpu_low_threshold` and
* `scaling_cpu_high_threshold` so that the number of
* containers is not continually being autoscaled up and down. If
* `ecs_autoscale_min_instances==1`, then
* `scaling_cpu_high_threshold` should be more than
* twice scaling_cpu_low_threshold`.
*
* In the CloudWatch section of the AWS Console, you will often see the
* alarms created by this module in an ALARM state, which are displayed in
* red. This is normal and does not indicate a problem.
* On the page listing all the alarms, click the checkbox labelled
* "Hide all AutoScaling alarms".
*
*/
resource "aws_cloudwatch_metric_alarm" "cpu_utilization_high" {
count = var.do_performance_autoscaling ? 1 : 0
alarm_name = "${var.app}-${var.environment}-CPU-Utilization-High-${var.scaling_cpu_high_threshold}"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = var.scaling_cpu_high_threshold
dimensions = {
ClusterName = local.ecs_cluster_name
ServiceName = aws_ecs_service.app.name
}
alarm_actions = [aws_appautoscaling_policy.app_up[0].arn]
}
resource "aws_cloudwatch_metric_alarm" "cpu_utilization_low" {
count = var.do_performance_autoscaling ? 1 : 0
alarm_name = "${var.app}-${var.environment}-CPU-Utilization-Low-${var.scaling_cpu_low_threshold}"
comparison_operator = "LessThanThreshold"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = var.scaling_cpu_low_threshold
dimensions = {
ClusterName = local.ecs_cluster_name
ServiceName = aws_ecs_service.app.name
}
alarm_actions = [aws_appautoscaling_policy.app_down[0].arn]
}
resource "aws_appautoscaling_policy" "app_up" {
count = var.do_performance_autoscaling ? 1 : 0
name = "app-scale-up"
service_namespace = aws_appautoscaling_target.app_scale_target.service_namespace
resource_id = aws_appautoscaling_target.app_scale_target.resource_id
scalable_dimension = aws_appautoscaling_target.app_scale_target.scalable_dimension
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 60
metric_aggregation_type = "Average"
step_adjustment {
metric_interval_lower_bound = 0
scaling_adjustment = 1
}
}
}
resource "aws_appautoscaling_policy" "app_down" {
count = var.do_performance_autoscaling ? 1 : 0
name = "app-scale-down"
service_namespace = aws_appautoscaling_target.app_scale_target.service_namespace
resource_id = aws_appautoscaling_target.app_scale_target.resource_id
scalable_dimension = aws_appautoscaling_target.app_scale_target.scalable_dimension
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 300
metric_aggregation_type = "Average"
step_adjustment {
metric_interval_upper_bound = 0
scaling_adjustment = -1
}
}
}