forked from dominik-dabrowski/terraform-aws-ecs-service
-
Notifications
You must be signed in to change notification settings - Fork 1
/
autoscale.tf
102 lines (88 loc) · 4.53 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
######################################################################################
####################### AUTO SCALING & CLOUD WATCH ALARMS ############################
########## The autoscaling policies to scale the ecs service up and down. ############
######################################################################################
# CLOUDWATCH ALARM to monitor the memory utilization of a service
resource "aws_cloudwatch_metric_alarm" "alarm_scale_down" {
count = length(var.autoscale) > 0 ? 1 : 0
alarm_description = "Scale down alarm for ${var.name}"
namespace = "AWS/ECS"
alarm_name = local.scale_down_name
alarm_actions = [aws_appautoscaling_policy.policy_scale_down[0].arn]
comparison_operator = var.autoscale["scale_down_comparison_operator"]
threshold = var.autoscale["scale_down_threshold"]
evaluation_periods = var.autoscale["evaluation_periods"]
metric_name = var.autoscale["metric_name"]
period = lookup(var.autoscale, "period", 180)
statistic = lookup(var.autoscale, "statistic", "Average")
datapoints_to_alarm = lookup(var.autoscale, "datapoints_to_alarm", 3)
dimensions = {
ClusterName = var.cluster
ServiceName = var.name
}
}
# CLOUDWATCH ALARM to monitor memory utilization of a service
resource "aws_cloudwatch_metric_alarm" "alarm_scale_up" {
count = length(var.autoscale) > 0 ? 1 : 0
alarm_description = "Scale up alarm for ${var.name}"
namespace = "AWS/ECS"
alarm_name = local.scale_up_name
alarm_actions = [aws_appautoscaling_policy.policy_scale_up[0].arn]
comparison_operator = var.autoscale["scale_up_comparison_operator"]
threshold = var.autoscale["scale_up_threshold"]
evaluation_periods = var.autoscale["evaluation_periods"]
metric_name = var.autoscale["metric_name"]
period = lookup(var.autoscale, "period", 180)
statistic = lookup(var.autoscale, "statistic", "Average")
datapoints_to_alarm = lookup(var.autoscale, "datapoints_to_alarm", 3)
dimensions = {
ClusterName = var.cluster
ServiceName = var.name
}
}
resource "aws_appautoscaling_target" "ecs_target" {
count = length(var.autoscale) > 0 ? 1 : 0
max_capacity = lookup(var.autoscale, "autoscale_max_capacity", 5)
min_capacity = lookup(var.autoscale, "service_desired_count", 1)
resource_id = "service/${var.cluster}/${var.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"
}
#Set up the memory utilization policy for scale down when the cloudwatch alarm gets triggered.
resource "aws_appautoscaling_policy" "policy_scale_down" {
count = length(var.autoscale) > 0 ? 1 : 0
name = local.scale_down_name
resource_id = aws_appautoscaling_target.ecs_target[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target[0].scalable_dimension
service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace
step_scaling_policy_configuration {
adjustment_type = var.autoscale["adjustment_type"]
cooldown = var.autoscale["cooldown"]
metric_aggregation_type = lookup(var.autoscale, "aggregation_type", "Average")
step_adjustment {
metric_interval_upper_bound = lookup(var.autoscale, "scale_down_interval_lower_bound", 0)
scaling_adjustment = var.autoscale["scale_down_adjustment"]
}
}
}
#Set up the memory utilization policy for scale up when the cloudwatch alarm gets triggered.
resource "aws_appautoscaling_policy" "policy_scale_up" {
count = length(var.autoscale) > 0 ? 1 : 0
name = local.scale_up_name
resource_id = aws_appautoscaling_target.ecs_target[0].resource_id
scalable_dimension = aws_appautoscaling_target.ecs_target[0].scalable_dimension
service_namespace = aws_appautoscaling_target.ecs_target[0].service_namespace
step_scaling_policy_configuration {
adjustment_type = var.autoscale["adjustment_type"]
cooldown = var.autoscale["cooldown"]
metric_aggregation_type = lookup(var.autoscale, "aggregation_type", "Average")
step_adjustment {
metric_interval_lower_bound = lookup(var.autoscale, "scale_up_interval_lower_bound", 1)
scaling_adjustment = var.autoscale["scale_up_adjustment"]
}
}
}