-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.tf
96 lines (92 loc) · 3.83 KB
/
main.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
#------------------------------------------------------------------------------
# AWS Auto Scaling - CloudWatch Alarm CPU High
#------------------------------------------------------------------------------
resource "aws_cloudwatch_metric_alarm" "cpu_high" {
alarm_name = "${var.name_prefix}-cpu-high"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = var.max_cpu_evaluation_period
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = var.max_cpu_period
statistic = "Maximum"
threshold = var.max_cpu_threshold
dimensions = {
ClusterName = var.ecs_cluster_name
ServiceName = var.ecs_service_name
}
alarm_actions = compact([
aws_appautoscaling_policy.scale_up_policy.arn,
var.sns_topic_arn != "" ? var.sns_topic_arn : ""
])
tags = var.tags
}
#------------------------------------------------------------------------------
# AWS Auto Scaling - CloudWatch Alarm CPU Low
#------------------------------------------------------------------------------
resource "aws_cloudwatch_metric_alarm" "cpu_low" {
alarm_name = "${var.name_prefix}-cpu-low"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = var.min_cpu_evaluation_period
metric_name = "CPUUtilization"
namespace = "AWS/ECS"
period = var.min_cpu_period
statistic = "Average"
threshold = var.min_cpu_threshold
dimensions = {
ClusterName = var.ecs_cluster_name
ServiceName = var.ecs_service_name
}
alarm_actions = compact([
aws_appautoscaling_policy.scale_down_policy.arn,
var.sns_topic_arn != "" ? var.sns_topic_arn : ""
])
tags = var.tags
}
#------------------------------------------------------------------------------
# AWS Auto Scaling - Scaling Up Policy
#------------------------------------------------------------------------------
resource "aws_appautoscaling_policy" "scale_up_policy" {
name = "${var.name_prefix}-scale-up-policy"
depends_on = [aws_appautoscaling_target.scale_target]
service_namespace = "ecs"
resource_id = "service/${var.ecs_cluster_name}/${var.ecs_service_name}"
scalable_dimension = "ecs:service:DesiredCount"
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = var.cooldown
metric_aggregation_type = "Maximum"
step_adjustment {
metric_interval_lower_bound = 0
scaling_adjustment = 1
}
}
}
#------------------------------------------------------------------------------
# AWS Auto Scaling - Scaling Down Policy
#------------------------------------------------------------------------------
resource "aws_appautoscaling_policy" "scale_down_policy" {
name = "${var.name_prefix}-scale-down-policy"
depends_on = [aws_appautoscaling_target.scale_target]
service_namespace = "ecs"
resource_id = "service/${var.ecs_cluster_name}/${var.ecs_service_name}"
scalable_dimension = "ecs:service:DesiredCount"
step_scaling_policy_configuration {
adjustment_type = "ChangeInCapacity"
cooldown = var.cooldown
metric_aggregation_type = "Maximum"
step_adjustment {
metric_interval_upper_bound = 0
scaling_adjustment = -1
}
}
}
#------------------------------------------------------------------------------
# AWS Auto Scaling - Scaling Target
#------------------------------------------------------------------------------
resource "aws_appautoscaling_target" "scale_target" {
service_namespace = "ecs"
resource_id = "service/${var.ecs_cluster_name}/${var.ecs_service_name}"
scalable_dimension = "ecs:service:DesiredCount"
min_capacity = var.scale_target_min_capacity
max_capacity = var.scale_target_max_capacity
}