-
Notifications
You must be signed in to change notification settings - Fork 16
/
launch_config_asg.tf
139 lines (115 loc) · 3.77 KB
/
launch_config_asg.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
resource "aws_launch_configuration" "web_launch_conf" {
name_prefix = "devops-"
image_id = var.ubuntu_18_sg
iam_instance_profile = aws_iam_instance_profile.ec2_cd_instance_profile.name
# image_id = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
key_name = var.key_name
security_groups = [aws_security_group.asg_web_sg.id]
associate_public_ip_address = true
user_data = file("codedeploy_agent_install.sh")
lifecycle {
create_before_destroy = true
}
}
resource "aws_autoscaling_group" "devops_web_asg" {
name = "devops_web_asg"
launch_configuration = aws_launch_configuration.web_launch_conf.name
min_size = 3
desired_capacity = 3
max_size = 5
health_check_type = "EC2"
#availability_zones = ["ap-southeast-1a", "ap-southeast-1b", "ap-southeast-1c"]
target_group_arns = [
aws_lb_target_group.external_alb_tg_app1.arn
]
enabled_metrics = [
"GroupMinSize",
"GroupMaxSize",
"GroupDesiredCapacity",
"GroupInServiceInstances",
"GroupTotalInstances"
]
metrics_granularity = "1Minute"
vpc_zone_identifier = [
aws_subnet.devops-pub-1a.id,
aws_subnet.devops-pub-1b.id,
aws_subnet.devops-pub-1c.id
]
lifecycle {
create_before_destroy = true
}
}
#ASG Scale-up Policy
resource "aws_autoscaling_policy" "devops_web_asg_policy_up" {
name = "devops_web_asg_policy_up"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.devops_web_asg.name
}
resource "aws_cloudwatch_metric_alarm" "devops_web_asg_cpu_alarm_up" {
alarm_name = "devops_web_asg_cpu_alarm_up"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "80"
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.devops_web_asg.name
}
alarm_description = "This metric monitor EC2 instance CPU utilization"
alarm_actions = ["${aws_autoscaling_policy.devops_web_asg_policy_up.arn}"]
}
#ASG Scale-down Policy
resource "aws_autoscaling_policy" "devops_web_asg_policy_down" {
name = "devops_web_asg_policy_down"
scaling_adjustment = -1
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.devops_web_asg.name
}
resource "aws_cloudwatch_metric_alarm" "devops_web_asg_cpu_alarm_down" {
alarm_name = "devops_web__asg_cpu_alarm_down"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = "120"
statistic = "Average"
threshold = "20"
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.devops_web_asg.name
}
alarm_description = "This metric monitor EC2 instance CPU utilization"
alarm_actions = [aws_autoscaling_policy.devops_web_asg_policy_down.arn]
}
#Security Group for ASG
resource "aws_security_group" "asg_web_sg" {
name = "asg_web_sg"
description = "Allow HTTP inbound connections"
vpc_id = aws_vpc.devops_vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "devops_SG_Web_Security_Group"
}
}