-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoscaling-group.tf
94 lines (77 loc) · 2.26 KB
/
autoscaling-group.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
resource "aws_autoscaling_group" "web" {
name = "${aws_launch_configuration.web.name}-asg"
min_size = 1
desired_capacity = 2
max_size = 4
force_delete = true
health_check_type = "ELB"
load_balancers = [
aws_elb.web_elb.id
]
launch_configuration = aws_launch_configuration.web.name
enabled_metrics = [
"GroupMinSize",
"GroupMaxSize",
"GroupDesiredCapacity",
"GroupInServiceInstances",
"GroupTotalInstances"
]
metrics_granularity = "1Minute"
vpc_zone_identifier = [
aws_subnet.public_us_east_1a.id,
aws_subnet.public_us_east_1b.id
]
# Required to redeploy without an outage.
lifecycle {
create_before_destroy = true
}
tag {
key = "Name"
value = "web"
propagate_at_launch = true
}
}
resource "aws_autoscaling_policy" "web_policy_up" {
name = "web_policy_up"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.web.name
}
resource "aws_cloudwatch_metric_alarm" "web_cpu_alarm_up" {
alarm_name = "web_cpu_alarm_up"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "60"
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.web.name
}
alarm_description = "This metric monitor EC2 instance CPU utilization"
alarm_actions = [ aws_autoscaling_policy.web_policy_up.arn ]
}
resource "aws_autoscaling_policy" "web_policy_down" {
name = "web_policy_down"
scaling_adjustment = -1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.web.name
}
resource "aws_cloudwatch_metric_alarm" "web_cpu_alarm_down" {
alarm_name = "web_cpu_alarm_down"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "10"
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.web.name
}
alarm_description = "This metric monitor EC2 instance CPU utilization"
alarm_actions = [ aws_autoscaling_policy.web_policy_down.arn ]
}