Skip to content

Commit 094e6af

Browse files
author
Steven Nemetz
committed
Add autoscaling rules
1 parent a11d860 commit 094e6af

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

autoscaling-rules.tf

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
// CloudWatch Alarms
3+
resource "aws_cloudwatch_metric_alarm" "high_cpu" {
4+
count = "${var.enable_scaling_policies}"
5+
alarm_name = "${title("ec2-asg-${var.name}-high-cpu-utilization")}"
6+
comparison_operator = "GreaterThanOrEqualToThreshold"
7+
evaluation_periods = "${var.scaling_policy_high_cpu_evaluation_periods}"
8+
metric_name = "CPUUtilization"
9+
namespace = "AWS/EC2"
10+
period = "${var.scaling_policy_high_cpu_period}"
11+
statistic = "Average"
12+
threshold = "${var.scaling_policy_high_cpu_threshold}"
13+
alarm_description = "This metric monitor ec2 high cpu utilization"
14+
alarm_actions = ["${aws_autoscaling_policy.cpu_scaling_out.arn}"]
15+
dimensions {
16+
AutoScalingGroupName = "${aws_autoscaling_group.this.name}"
17+
}
18+
}
19+
20+
resource "aws_cloudwatch_metric_alarm" "low_cpu" {
21+
count = "${var.enable_scaling_policies}"
22+
alarm_name = "${title("ec2-asg-${var.name}-low-cpu-utilization")}"
23+
comparison_operator = "LessThanThreshold"
24+
evaluation_periods = "${var.scaling_policy_low_cpu_evaluation_periods}"
25+
metric_name = "CPUUtilization"
26+
namespace = "AWS/EC2"
27+
period = "${var.scaling_policy_low_cpu_period}"
28+
statistic = "Average"
29+
threshold = "${var.scaling_policy_low_cpu_threshold}"
30+
alarm_description = "This metric monitor ec2 low cpu utilization"
31+
alarm_actions = ["${aws_autoscaling_policy.cpu_scaling_in.arn}"]
32+
dimensions {
33+
AutoScalingGroupName = "${aws_autoscaling_group.this.name}"
34+
}
35+
}
36+
37+
// Auto Scaling Policy
38+
resource "aws_autoscaling_policy" "cpu_scaling_out" {
39+
count = "${var.enable_scaling_policies}"
40+
name = "cpu-scaling-out"
41+
scaling_adjustment = "${length(var.vpc_zone_identifier)}"
42+
adjustment_type = "ChangeInCapacity"
43+
cooldown = "${var.scaling_policy_scaling_out_cooldown}"
44+
autoscaling_group_name = "${aws_autoscaling_group.this.name}"
45+
}
46+
47+
resource "aws_autoscaling_policy" "cpu_scaling_in" {
48+
count = "${var.enable_scaling_policies}"
49+
name = "cpu-scaling-in"
50+
scaling_adjustment = -1
51+
adjustment_type = "ChangeInCapacity"
52+
cooldown = "${var.scaling_policy_scaling_in_cooldown}"
53+
autoscaling_group_name = "${aws_autoscaling_group.this.name}"
54+
}

main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ resource "aws_autoscaling_group" "this" {
8686
count = "${module.enabled.value}"
8787

8888
name_prefix = "${coalesce(var.asg_name, module.label.id)}-"
89-
launch_configuration = "${var.launch_configuration == "" ? element(aws_launch_configuration.this.*.name, 0) : var.launch_configuration}"
89+
launch_configuration = "${var.launch_configuration == "" ? element(concat(aws_launch_configuration.this.*.name, list("")), 0) : var.launch_configuration}"
9090
vpc_zone_identifier = ["${var.vpc_zone_identifier}"]
9191
max_size = "${var.max_size}"
9292
min_size = "${var.min_size}"

variables.tf

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,49 @@ variable "wait_for_elb_capacity" {
240240
description = "Setting this will cause Terraform to wait for exactly this number of healthy instances in all attached load balancers on both create and update operations. Takes precedence over min_elb_capacity behavior."
241241
default = false
242242
}
243+
244+
// Autoscaling rules
245+
variable "enable_scaling_policies" {
246+
description = "Enable default scale-in and scale-out policies based on CPU Utilization"
247+
default = false
248+
}
249+
250+
variable "scaling_policy_high_cpu_evaluation_periods" {
251+
description = "The number of periods over which data is compared to the specified threshold"
252+
default = 5
253+
}
254+
255+
variable "scaling_policy_high_cpu_period" {
256+
description = "The period in seconds over which the specified statistic is applied"
257+
default = 60
258+
}
259+
260+
variable "scaling_policy_high_cpu_threshold" {
261+
description = "The value against which the specified statistic is compared"
262+
default = 60
263+
}
264+
265+
variable "scaling_policy_scaling_out_cooldown" {
266+
description = "The amount of time, in seconds, after a scaling activity completes and before the next scaling activity can start"
267+
default = 300
268+
}
269+
270+
variable "scaling_policy_low_cpu_evaluation_periods" {
271+
description = "The number of periods over which data is compared to the specified threshold"
272+
default = 3
273+
}
274+
275+
variable "scaling_policy_low_cpu_period" {
276+
description = "The period in seconds over which the specified statistic is applied"
277+
default = 900
278+
}
279+
280+
variable "scaling_policy_low_cpu_threshold" {
281+
description = "The value against which the specified statistic is compared"
282+
default = 20
283+
}
284+
285+
variable "scaling_policy_scaling_in_cooldown" {
286+
description = "The amount of time, in seconds, after a scaling activity completes and before the next scaling activity can start"
287+
default = 3000
288+
}

0 commit comments

Comments
 (0)