Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit eb58cda

Browse files
Magicloudketzacoatl
authored andcommitted
New: module autoscaling-policy-metric-alarm-pair
Abstract usage of autoscaling policy and cloudwatch alarm to autoscale ASG. By moving the code from load-asg example. Example load-asg is using the new module now.
1 parent 79998ca commit eb58cda

File tree

4 files changed

+143
-103
lines changed

4 files changed

+143
-103
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# (Unreleased)
22

3+
### Modules
4+
5+
* `autoscaling-policy-metric-alarm-pair`: New module abstracts the autoscaling function from example load-asg. The example now is using this module.
6+
37
# v0.9.0
48

59
### Summary

examples/load-asg/main.tf

+12-103
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ module "web-asg" {
122122
name_prefix = "${var.name}-${var.web_app_name}"
123123
elb_names = [aws_elb.web.name]
124124
instance_type = var.instance_type
125-
max_nodes = "10"
126-
min_nodes = "2"
125+
max_nodes = 10
126+
min_nodes = 2
127127
public_ip = false
128128
key_name = aws_key_pair.main.key_name
129129
subnet_ids = module.vpc.public_subnet_ids
@@ -167,108 +167,17 @@ END_INIT
167167

168168
}
169169

170-
## Autoscaling Policies
171-
172-
# ASG Policy Up
173-
174-
resource "aws_autoscaling_policy" "cpu_up" {
175-
name = "${var.name}-asg-policy-cpu-up"
176-
autoscaling_group_name = module.web-asg.name
177-
adjustment_type = "ChangeInCapacity"
178-
cooldown = 300
179-
scaling_adjustment = 3
180-
policy_type = "SimpleScaling"
181-
}
182-
183-
resource "aws_autoscaling_policy" "mem_up" {
184-
name = "${var.name}-asg-policy-mem-up"
185-
autoscaling_group_name = module.web-asg.name
186-
adjustment_type = "ChangeInCapacity"
187-
cooldown = 300
188-
scaling_adjustment = 3
189-
policy_type = "SimpleScaling"
190-
}
191-
192-
# Cloudwatch Monitor CPU Up
193-
resource "aws_cloudwatch_metric_alarm" "web_cpu_scale_up" {
194-
alarm_name = "${var.name}-cpu-scale-up-alarm"
195-
comparison_operator = "GreaterThanOrEqualToThreshold"
196-
evaluation_periods = "3"
197-
metric_name = "CPUUtilization"
198-
namespace = "AWS/EC2"
199-
period = "60"
200-
statistic = "Average"
201-
threshold = "60"
202-
dimensions = {
203-
AutoScalingGroupName = module.web-asg.name
204-
}
205-
alarm_actions = [aws_autoscaling_policy.cpu_up.arn]
206-
}
207-
208-
# Cloudwatch Monitor Memory Up
209-
resource "aws_cloudwatch_metric_alarm" "web_mem_scale_up" {
210-
alarm_name = "${var.name}-mem-scale-up-alarm"
211-
comparison_operator = "GreaterThanOrEqualToThreshold"
212-
evaluation_periods = "3"
213-
metric_name = "MemoryUtilization"
214-
namespace = "AWS/EC2"
215-
period = "60"
216-
statistic = "Average"
217-
threshold = "60"
218-
dimensions = {
219-
AutoScalingGroupName = module.web-asg.name
220-
}
221-
alarm_actions = [aws_autoscaling_policy.mem_up.arn]
222-
}
223-
224-
# ASG Policy Down
225-
resource "aws_autoscaling_policy" "cpu_down" {
226-
name = "${var.name}-asg-policy-cpu-down"
227-
autoscaling_group_name = module.web-asg.name
228-
adjustment_type = "ChangeInCapacity"
229-
cooldown = 300
230-
scaling_adjustment = -2
231-
policy_type = "SimpleScaling"
232-
}
233-
234-
resource "aws_autoscaling_policy" "mem_down" {
235-
name = "${var.name}-asg-policy-mem-down"
236-
autoscaling_group_name = module.web-asg.name
237-
adjustment_type = "ChangeInCapacity"
238-
cooldown = 300
239-
scaling_adjustment = -2
240-
policy_type = "SimpleScaling"
170+
module "web_cpu_autoscaling" {
171+
source = "../../modules/autoscaling-policy-metric-alarm-pair"
172+
name = var.name
173+
asg_name = module.web-asg.name
174+
metric = "CPUUtilization"
241175
}
242176

243-
# Cloudwatch CPU Monitor Down
244-
resource "aws_cloudwatch_metric_alarm" "web_cpu_scale_down" {
245-
alarm_name = "${var.name}-cpu-scale-down-alarm"
246-
comparison_operator = "LessThanOrEqualToThreshold"
247-
evaluation_periods = "3"
248-
metric_name = "CPUUtilization"
249-
namespace = "AWS/EC2"
250-
period = "60"
251-
statistic = "Average"
252-
threshold = "30"
253-
dimensions = {
254-
AutoScalingGroupName = module.web-asg.name
255-
}
256-
alarm_actions = [aws_autoscaling_policy.cpu_down.arn]
257-
}
258-
259-
# Cloudwatch Memory Monitor Down
260-
resource "aws_cloudwatch_metric_alarm" "web_mem_scale_down" {
261-
alarm_name = "${var.name}-mem-scale-down-alarm"
262-
comparison_operator = "LessThanOrEqualToThreshold"
263-
evaluation_periods = "3"
264-
metric_name = "MemoryUtilization"
265-
namespace = "AWS/EC2"
266-
period = "60"
267-
statistic = "Average"
268-
threshold = "30"
269-
dimensions = {
270-
AutoScalingGroupName = module.web-asg.name
271-
}
272-
alarm_actions = [aws_autoscaling_policy.mem_down.arn]
177+
module "web_mem_autoscaling" {
178+
source = "../../modules/autoscaling-policy-metric-alarm-pair"
179+
name = var.name
180+
asg_name = module.web-asg.name
181+
metric = "MemoryUtilization"
273182
}
274183

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
variable "name" {
2+
description = "Prefix of the AutoScaling Policy."
3+
type = string
4+
}
5+
6+
variable "asg_name" {
7+
description = "Name of the AutoScaling Group that this policy would apply to."
8+
type = string
9+
}
10+
11+
variable "metric" {
12+
description = "CPUUtilization or MemoryUtilization"
13+
type = string
14+
}
15+
16+
variable "up_evaluation_periods" {
17+
default = 3
18+
description = ""
19+
type = number
20+
}
21+
22+
variable "up_period" {
23+
default = 60
24+
description = ""
25+
type = number
26+
}
27+
28+
variable "up_threshold" {
29+
default = 60
30+
description = ""
31+
type = number
32+
}
33+
34+
variable "up_scaling_adjustment" {
35+
default = 3
36+
description = ""
37+
type = number
38+
}
39+
40+
variable "up_cooldown" {
41+
default = 300
42+
description = ""
43+
type = number
44+
}
45+
46+
variable "down_evaluation_periods" {
47+
default = 3
48+
description = ""
49+
type = number
50+
}
51+
52+
variable "down_period" {
53+
default = 60
54+
description = ""
55+
type = number
56+
}
57+
58+
variable "down_threshold" {
59+
default = 30
60+
description = ""
61+
type = number
62+
}
63+
64+
variable "down_scaling_adjustment" {
65+
default = 2
66+
description = ""
67+
type = number
68+
}
69+
70+
variable "down_cooldown" {
71+
default = 300
72+
description = ""
73+
type = number
74+
}
75+
76+
resource "aws_autoscaling_policy" "scale_up" {
77+
name = "${var.name}-asp-up"
78+
autoscaling_group_name = var.asg_name
79+
adjustment_type = "ChangeInCapacity"
80+
cooldown = var.up_cooldown
81+
scaling_adjustment = var.up_scaling_adjustment
82+
policy_type = "SimpleScaling"
83+
}
84+
85+
resource "aws_cloudwatch_metric_alarm" "scale_up" {
86+
alarm_name = "${var.name}-scale-up-alarm"
87+
comparison_operator = "GreaterThanOrEqualToThreshold"
88+
evaluation_periods = var.up_evaluation_periods
89+
metric_name = var.metric
90+
namespace = "AWS/EC2"
91+
period = var.up_period
92+
statistic = "Average"
93+
threshold = var.up_threshold
94+
dimensions = {
95+
AutoScalingGroupName = var.asg_name
96+
}
97+
alarm_actions = [aws_autoscaling_policy.scale_up.arn]
98+
}
99+
100+
resource "aws_autoscaling_policy" "scale_down" {
101+
name = "${var.name}-asp-down"
102+
autoscaling_group_name = var.asg_name
103+
adjustment_type = "ChangeInCapacity"
104+
cooldown = var.down_cooldown
105+
scaling_adjustment = "-${var.down_scaling_adjustment}"
106+
policy_type = "SimpleScaling"
107+
}
108+
109+
resource "aws_cloudwatch_metric_alarm" "scale_down" {
110+
alarm_name = "${var.name}-scale-down-alarm"
111+
comparison_operator = "LessThanOrEqualToThreshold"
112+
evaluation_periods = var.down_evaluation_periods
113+
metric_name = var.metric
114+
namespace = "AWS/EC2"
115+
period = var.down_period
116+
statistic = "Average"
117+
threshold = var.down_threshold
118+
dimensions = {
119+
AutoScalingGroupName = var.asg_name
120+
}
121+
alarm_actions = [aws_autoscaling_policy.scale_down.arn]
122+
}
123+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
terraform {
3+
required_version = ">= 0.12"
4+
}

0 commit comments

Comments
 (0)