-
Notifications
You must be signed in to change notification settings - Fork 34
/
autoscale.tf
114 lines (92 loc) · 4.63 KB
/
autoscale.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
locals {
autoscale_metrics = { for k in try(keys(var.autoscale.metrics), {}) : k => var.autoscale.metrics[k] }
}
# Autoscaling target.
# TODO: Study (and document) how min_capacity and max_capacity interact with scaleable_dimension.
resource "aws_appautoscaling_target" "default" {
for_each = var.autoscale != null ? { autoscale = var.autoscale } : {}
max_capacity = each.value.max_capacity
min_capacity = each.value.min_capacity
resource_id = format("service/%s/%s", data.aws_ecs_cluster.selected.cluster_name, aws_ecs_service.default.name)
role_arn = format("arn:aws:iam::%s:role/aws-service-role/ecs.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_ECSService", data.aws_caller_identity.current.account_id)
scalable_dimension = "ecs:service:DesiredCount"
service_namespace = "ecs"
}
# Scale-down alarm for each metric.
resource "aws_cloudwatch_metric_alarm" "down" {
for_each = local.autoscale_metrics
actions_enabled = each.value.actions_enabled
alarm_actions = [aws_appautoscaling_policy.down[each.key].arn]
alarm_description = format("scale-down alarm for %s on %s metric", aws_ecs_service.default.name, each.key)
alarm_name = format("ecs-%s-%s-down", aws_ecs_service.default.name, lower(each.key))
comparison_operator = each.value.down.comparison_operator
datapoints_to_alarm = each.value.datapoints_to_alarm
evaluation_periods = each.value.evaluation_periods
metric_name = each.key
namespace = "AWS/ECS"
period = each.value.period
statistic = each.value.statistic
tags = local.tags
threshold = each.value.down.threshold
dimensions = {
ClusterName = data.aws_ecs_cluster.selected.cluster_name
ServiceName = aws_ecs_service.default.name
}
}
# Scale-up alarm for each metric.
resource "aws_cloudwatch_metric_alarm" "up" {
for_each = local.autoscale_metrics
actions_enabled = each.value.actions_enabled
alarm_actions = [aws_appautoscaling_policy.up[each.key].arn]
alarm_description = format("scale-up alarm for %s on %s metric", aws_ecs_service.default.name, each.key)
alarm_name = format("ecs-%s-%s-up", aws_ecs_service.default.name, lower(each.key))
comparison_operator = each.value.up.comparison_operator
datapoints_to_alarm = each.value.datapoints_to_alarm
evaluation_periods = each.value.evaluation_periods
metric_name = each.key
namespace = "AWS/ECS"
period = each.value.period
statistic = each.value.statistic
tags = local.tags
threshold = each.value.up.threshold
dimensions = {
ClusterName = data.aws_ecs_cluster.selected.cluster_name
ServiceName = aws_ecs_service.default.name
}
}
# Scale-down policy for each metric.
resource "aws_appautoscaling_policy" "down" {
for_each = local.autoscale_metrics
name = format("ecs-%s-%s-down", aws_ecs_service.default.name, lower(each.key))
resource_id = aws_appautoscaling_target.default["autoscale"].resource_id
scalable_dimension = aws_appautoscaling_target.default["autoscale"].scalable_dimension
service_namespace = aws_appautoscaling_target.default["autoscale"].service_namespace
step_scaling_policy_configuration {
adjustment_type = each.value.adjustment_type
cooldown = each.value.down.cooldown
metric_aggregation_type = each.value.metric_aggregation_type
step_adjustment {
metric_interval_lower_bound = each.value.down.metric_interval_lower_bound
metric_interval_upper_bound = each.value.down.metric_interval_upper_bound
scaling_adjustment = each.value.down.scaling_adjustment
}
}
}
# Scale-up policy for each metric.
resource "aws_appautoscaling_policy" "up" {
for_each = local.autoscale_metrics
name = format("ecs-%s-%s-up", aws_ecs_service.default.name, lower(each.key))
resource_id = aws_appautoscaling_target.default["autoscale"].resource_id
scalable_dimension = aws_appautoscaling_target.default["autoscale"].scalable_dimension
service_namespace = aws_appautoscaling_target.default["autoscale"].service_namespace
step_scaling_policy_configuration {
adjustment_type = each.value.adjustment_type
cooldown = each.value.up.cooldown
metric_aggregation_type = each.value.metric_aggregation_type
step_adjustment {
metric_interval_lower_bound = each.value.up.metric_interval_lower_bound
metric_interval_upper_bound = each.value.up.metric_interval_upper_bound
scaling_adjustment = each.value.up.scaling_adjustment
}
}
}