-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi-autoscaling.tf
92 lines (79 loc) · 3.02 KB
/
api-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
86
87
88
89
90
91
92
# Scale the API service up and down depending on usage.
#
# The target defines the resource to be scaled and its limits, while the
# policies do the actual scaling (and define how much and how often to do so),
# and the alarms ultimately define the conditions under which to scale and call
# the policies when those conditions are met.
resource "aws_appautoscaling_target" "target" {
service_namespace = "ecs"
resource_id = "service/${aws_ecs_cluster.main.name}/${aws_ecs_service.api_service.name}"
scalable_dimension = "ecs:service:DesiredCount"
min_capacity = 2
max_capacity = 4
}
# Automatically scale capacity up by one
resource "aws_appautoscaling_policy" "up" {
name = "api_scale_up"
service_namespace = aws_appautoscaling_target.target.service_namespace
resource_id = aws_appautoscaling_target.target.resource_id
scalable_dimension = aws_appautoscaling_target.target.scalable_dimension
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 60
metric_aggregation_type = "Maximum"
step_adjustment {
metric_interval_lower_bound = 0
scaling_adjustment = 1
}
}
}
# Automatically scale capacity down by one
resource "aws_appautoscaling_policy" "down" {
name = "api_scale_down"
service_namespace = aws_appautoscaling_target.target.service_namespace
resource_id = aws_appautoscaling_target.target.resource_id
scalable_dimension = aws_appautoscaling_target.target.scalable_dimension
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = 60
metric_aggregation_type = "Maximum"
step_adjustment {
metric_interval_upper_bound = 0
scaling_adjustment = -1
}
}
}
# CloudWatch alarm that triggers the autoscaling up policy
resource "aws_cloudwatch_metric_alarm" "service_cpu_high" {
alarm_name = "api_cpu_utilization_high"
comparison_operator = "GreaterThanOrEqualToThreshold"
datapoints_to_alarm = "1"
evaluation_periods = "1"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = "55"
dimensions = {
ClusterName = aws_ecs_cluster.main.name
ServiceName = aws_ecs_service.api_service.name
}
alarm_actions = [aws_appautoscaling_policy.up.arn]
}
# CloudWatch alarm that triggers the autoscaling down policy
resource "aws_cloudwatch_metric_alarm" "service_cpu_low" {
alarm_name = "api_cpu_utilization_low"
comparison_operator = "LessThanOrEqualToThreshold"
datapoints_to_alarm = "2"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = "60"
statistic = "Average"
threshold = "15"
dimensions = {
ClusterName = aws_ecs_cluster.main.name
ServiceName = aws_ecs_service.api_service.name
}
alarm_actions = [aws_appautoscaling_policy.down.arn]
}