-
Notifications
You must be signed in to change notification settings - Fork 1
/
autoscaling.tf
112 lines (107 loc) · 3.76 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
data "aws_ami" "amazon_ami" {
filter {
name = "name"
values = var.ami_name
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
most_recent = true
owners = ["amazon"]
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template
resource "aws_launch_template" "application" {
image_id = data.aws_ami.amazon_ami.id
instance_type = var.instance_type
user_data = filebase64("./user_data/user_data.tpl")
network_interfaces {
security_groups = [aws_security_group.ec2_security_group.id]
}
iam_instance_profile {
name = "${var.name}-ec2-profile"
}
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group
resource "aws_autoscaling_group" "application" {
name = var.name
min_size = 3
max_size = 6
desired_capacity = 3
health_check_grace_period = 480
launch_template {
id = aws_launch_template.application.id
version = aws_launch_template.application.latest_version
}
vpc_zone_identifier = aws_subnet.private.*.id
health_check_type = "ELB"
lifecycle {
ignore_changes = [desired_capacity, target_group_arns]
}
instance_refresh {
strategy = "Rolling"
preferences {
min_healthy_percentage = 50
skip_matching = true
}
triggers = ["launch_template"]
}
tag {
key = "Name"
value = var.name
propagate_at_launch = true
}
tag {
key = "Source"
value = "https://github.com/kunduso/add-asg-elb-terraform"
propagate_at_launch = true
}
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_policy
resource "aws_autoscaling_policy" "asg_policy_up" {
name = "${var.name}-asg-policy-up"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.application.name
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm
resource "aws_cloudwatch_metric_alarm" "asg_cpu_alarm_up" {
alarm_name = "${var.name}-asg-cpu-alarm-up"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "60"
statistic = "Average"
threshold = "70"
dimensions = {
AutoScalingGroupName = "${aws_autoscaling_group.application.name}"
}
alarm_description = "This metric monitors ec2 cpu utilization"
alarm_actions = [aws_autoscaling_policy.asg_policy_up.arn]
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_policy
resource "aws_autoscaling_policy" "asg_policy_down" {
name = "${var.name}-asg-policy-down"
scaling_adjustment = -1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.application.name
}
#https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm
resource "aws_cloudwatch_metric_alarm" "asg_cpu_alarm_down" {
alarm_name = "${var.name}-asg-cpu-alarm-down"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "60"
statistic = "Average"
threshold = "30"
dimensions = {
AutoScalingGroupName = "${aws_autoscaling_group.application.name}"
}
alarm_description = "This metric monitors ec2 cpu utilization"
alarm_actions = [aws_autoscaling_policy.asg_policy_down.arn]
}