-
Notifications
You must be signed in to change notification settings - Fork 4
/
autoscaling.tf
192 lines (170 loc) · 9.43 KB
/
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
locals {
autoscaling_enabled = var.enabled && var.on_demand_enabled ? true : false
spot_autoscaling_enabled = var.enabled && var.spot_enabled ? true : false
autoscaling_enabled_schedule = var.enabled && var.on_demand_enabled && var.schedule_enabled ? true : false
autoscaling_enabled_spot_schedule = var.enabled && var.spot_enabled && var.spot_schedule_enabled ? true : false
}
#Module : AUTOSCALING POLICY UP
#Description : Provides an AutoScaling Scaling Policy resource.
resource "aws_autoscaling_policy" "scale_up" {
count = local.autoscaling_enabled ? 1 : 0
name = format("%s%sscale%sup", module.labels.id, var.delimiter, var.delimiter)
scaling_adjustment = var.scale_up_scaling_adjustment
adjustment_type = var.scale_up_adjustment_type
policy_type = var.scale_up_policy_type
cooldown = var.scale_up_cooldown_seconds
autoscaling_group_name = join("", aws_autoscaling_group.on_demand[*].name)
}
#Module : AUTOSCALING POLICY UP
#Description : Provides an AutoScaling Scaling Policy resource.
resource "aws_autoscaling_policy" "scale_up_spot" {
count = local.spot_autoscaling_enabled ? 1 : 0
name = format("%s%sscale%sup-spot", module.labels.id, var.delimiter, var.delimiter)
scaling_adjustment = var.scale_up_scaling_adjustment
adjustment_type = var.scale_up_adjustment_type
policy_type = var.scale_up_policy_type
cooldown = var.scale_up_cooldown_seconds
autoscaling_group_name = join("", aws_autoscaling_group.spot[*].name)
}
#Module : AUTOSCALING POLICY DOWN
#Description : Provides an AutoScaling Scaling Policy resource.
resource "aws_autoscaling_policy" "scale_down" {
count = local.autoscaling_enabled ? 1 : 0
name = format("%s%sscale%sdown", module.labels.id, var.delimiter, var.delimiter)
scaling_adjustment = var.scale_down_scaling_adjustment
adjustment_type = var.scale_down_adjustment_type
policy_type = var.scale_down_policy_type
cooldown = var.scale_down_cooldown_seconds
autoscaling_group_name = join("", aws_autoscaling_group.on_demand[*].name)
}
#Module : AUTOSCALING POLICY DOWN
#Description : Provides an AutoScaling Scaling Policy resource.
resource "aws_autoscaling_policy" "scale_down_spot" {
count = local.spot_autoscaling_enabled ? 1 : 0
name = format("%s%sscale%sdown-spot", module.labels.id, var.delimiter, var.delimiter)
scaling_adjustment = var.scale_down_scaling_adjustment
adjustment_type = var.scale_down_adjustment_type
policy_type = var.scale_down_policy_type
cooldown = var.scale_down_cooldown_seconds
autoscaling_group_name = join("", aws_autoscaling_group.spot[*].name)
}
#Module : CLOUDWATCH METRIC ALARM CPU HIGH
#Description : Provides a CloudWatch Metric Alarm resource.
resource "aws_cloudwatch_metric_alarm" "cpu_high" {
count = local.autoscaling_enabled ? 1 : 0
alarm_name = format("%s%scpu%sutilization%shigh", module.labels.id, var.delimiter, var.delimiter, var.delimiter)
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = var.cpu_utilization_high_evaluation_periods
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = var.cpu_utilization_high_period_seconds
statistic = var.cpu_utilization_high_statistic
threshold = var.cpu_utilization_high_threshold_percent
tags = module.labels.tags
dimensions = {
AutoScalingGroupName = join("", aws_autoscaling_group.on_demand[*].name)
}
alarm_description = format("Scale up if CPU utilization is above%s for %s seconds", var.cpu_utilization_high_threshold_percent, var.cpu_utilization_high_period_seconds)
alarm_actions = [join("", aws_autoscaling_policy.scale_up[*].arn)]
}
#Module : CLOUDWATCH METRIC ALARM CPU HIGH
#Description : Provides a CloudWatch Metric Alarm resource.
resource "aws_cloudwatch_metric_alarm" "cpu_high_spot" {
count = local.spot_autoscaling_enabled ? 1 : 0
alarm_name = format("%s%scpu%sutilization%shigh-spot", module.labels.id, var.delimiter, var.delimiter, var.delimiter)
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = var.cpu_utilization_high_evaluation_periods
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = var.cpu_utilization_high_period_seconds
statistic = var.cpu_utilization_high_statistic
threshold = var.cpu_utilization_high_threshold_percent
tags = module.labels.tags
dimensions = {
AutoScalingGroupName = join("", aws_autoscaling_group.spot[*].name)
}
alarm_description = format("Scale up if CPU utilization is above%s for %s seconds", var.cpu_utilization_high_threshold_percent, var.cpu_utilization_high_period_seconds)
alarm_actions = [join("", aws_autoscaling_policy.scale_up_spot[*].arn)]
}
#Module : CLOUDWATCH METRIC ALARM CPU LOW
#Description : Provides a CloudWatch Metric Alarm resource.
resource "aws_cloudwatch_metric_alarm" "cpu_low" {
count = local.autoscaling_enabled ? 1 : 0
alarm_name = format("%s%scpu%sutilization%slow", module.labels.id, var.delimiter, var.delimiter, var.delimiter)
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = var.cpu_utilization_low_evaluation_periods
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = var.cpu_utilization_low_period_seconds
statistic = var.cpu_utilization_low_statistic
threshold = var.cpu_utilization_low_threshold_percent
tags = module.labels.tags
dimensions = {
AutoScalingGroupName = join("", aws_autoscaling_group.on_demand[*].name)
}
alarm_description = format("Scale down if CPU utilization is above%s for %s seconds", var.cpu_utilization_high_threshold_percent, var.cpu_utilization_high_period_seconds)
alarm_actions = [join("", aws_autoscaling_policy.scale_down[*].arn)]
}
#Module : CLOUDWATCH METRIC ALARM CPU LOW
#Description : Provides a CloudWatch Metric Alarm resource.
resource "aws_cloudwatch_metric_alarm" "cpu_low_spot" {
count = local.spot_autoscaling_enabled ? 1 : 0
alarm_name = format("%s%scpu%sutilization%slow-spot", module.labels.id, var.delimiter, var.delimiter, var.delimiter)
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = var.cpu_utilization_low_evaluation_periods
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = var.cpu_utilization_low_period_seconds
statistic = var.cpu_utilization_low_statistic
threshold = var.cpu_utilization_low_threshold_percent
tags = module.labels.tags
dimensions = {
AutoScalingGroupName = join("", aws_autoscaling_group.spot[*].name)
}
alarm_description = format("Scale down if CPU utilization is above%s for %s seconds", var.cpu_utilization_high_threshold_percent, var.cpu_utilization_high_period_seconds)
alarm_actions = [join("", aws_autoscaling_policy.scale_down_spot[*].arn)]
}
#Module : AWS AUTOSCALING SCHEDULE
#Description : Provides an AutoScaling Schedule resource.
resource "aws_autoscaling_schedule" "scaledown" {
count = local.autoscaling_enabled_schedule ? 1 : 0
autoscaling_group_name = join("", aws_autoscaling_group.on_demand[*].name)
scheduled_action_name = format("%s-scheduler-down", module.labels.id)
min_size = var.min_size_scaledown
max_size = var.max_size_scaledown
desired_capacity = var.scale_down_desired
recurrence = var.scheduler_down
}
#Module : AWS AUTOSCALING SCHEDULE
#Description : Provides an AutoScaling Schedule resource.
resource "aws_autoscaling_schedule" "scaleup" {
count = local.autoscaling_enabled_schedule ? 1 : 0
autoscaling_group_name = join("", aws_autoscaling_group.on_demand[*].name)
scheduled_action_name = format("%s-scheduler-up", module.labels.id)
max_size = var.max_size_scaleup
min_size = var.min_size_scaleup
desired_capacity = var.scale_up_desired
recurrence = var.scheduler_up
}
#Module : AWS AUTOSCALING SCHEDULE
#Description : Provides an AutoScaling Schedule resource.
resource "aws_autoscaling_schedule" "spot_scaledown" {
count = local.autoscaling_enabled_spot_schedule ? 1 : 0
autoscaling_group_name = join("", aws_autoscaling_group.spot[*].name)
scheduled_action_name = format("spot-%s-scheduler-down", module.labels.id)
min_size = var.spot_min_size_scaledown
max_size = var.spot_max_size_scaledown
desired_capacity = var.spot_scale_down_desired
recurrence = var.scheduler_down
}
#Module : AWS AUTOSCALING SCHEDULE
#Description : Provides an AutoScaling Schedule resource.
resource "aws_autoscaling_schedule" "spot_scaleup" {
count = local.autoscaling_enabled_spot_schedule ? 1 : 0
autoscaling_group_name = join("", aws_autoscaling_group.spot[*].name)
scheduled_action_name = format("spot-%s-scheduler-up", module.labels.id)
max_size = var.spot_max_size
min_size = var.spot_min_size
desired_capacity = var.spot_scale_up_desired
recurrence = var.scheduler_up
}