-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoscaling.tf
282 lines (249 loc) · 7.75 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#Auto Scaling Group and Launch Template with IAM Role
resource "aws_iam_role" "ssm_role" {
name = "${var.env}-ec2-ssm-role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Principal = {
Service = "ec2.amazonaws.com"
}
Effect = "Allow"
Sid = ""
},
]
})
tags = {
Name = "${var.env}-SSMRole"
}
}
resource "aws_iam_instance_profile" "ssm_instance_profile" {
name = "${var.env}-ec2-ssm-instance-profile"
role = aws_iam_role.ssm_role.name
}
resource "aws_iam_role_policy_attachment" "ssm_policies" {
for_each = local.ssm_policies
role = aws_iam_role.ssm_role.name
policy_arn = each.value
# Each key is used to create a unique name context for the resource
# This helps in identifying the policy attached clearly in the Terraform state file.
}
resource "aws_launch_template" "app" {
name_prefix = "${var.env}-lt-"
image_id = var.ami_id
instance_type = var.instance_type
iam_instance_profile {
arn = aws_iam_instance_profile.ssm_instance_profile.arn
}
monitoring {
enabled = true
}
network_interfaces {
associate_public_ip_address = false
subnet_id = tolist(values(aws_subnet.private))[0].id
security_groups = [aws_security_group.ec2_sg.id]
}
user_data = base64encode(data.template_file.user_data.rendered)
tag_specifications {
resource_type = "instance"
tags = {
Name = "${var.env}-instance"
}
}
lifecycle {
create_before_destroy = true
}
update_default_version = true
}
data "template_file" "user_data" {
template = file("templates/user_data.tpl")
vars = {
efs_file_system_id = aws_efs_file_system.app_efs.id
}
}
resource "aws_lb" "app_alb" {
name = "${var.env}-alb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.alb_sg.id]
subnets = values(aws_subnet.public).*.id
tags = {
Name = "${var.env}-ApplicationLB"
}
}
resource "aws_lb_target_group" "app_tg" {
name = "${var.env}-tg"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.main.id
health_check {
enabled = true
interval = 30
path = "/"
protocol = "HTTP"
timeout = 5
healthy_threshold = 2
unhealthy_threshold = 2
matcher = "200-299"
}
tags = {
Name = "${var.env}-TargetGroup"
}
}
resource "aws_lb_listener" "front_end" {
load_balancer_arn = aws_lb.app_alb.arn
port = 80
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
resource "aws_lb_listener" "https" {
load_balancer_arn = aws_lb.app_alb.arn
port = 443
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08" # Update as needed
certificate_arn = aws_acm_certificate.example_cert.arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.app_tg.arn
}
}
resource "aws_security_group" "alb_sg" {
name = "${var.env}-alb-sg"
description = "Security group for the application load balancer"
vpc_id = aws_vpc.main.id
dynamic "ingress" {
for_each = var.alb_sg_ingress_rules
content {
description = format("Allow access for %s", ingress.key)
from_port = ingress.value.from_port
to_port = ingress.value.to_port
protocol = lookup(ingress.value, "protocol", "tcp")
cidr_blocks = lookup(ingress.value, "cidr_blocks", [])
security_groups = lookup(ingress.value, "security_groups", [])
}
}
dynamic "egress" {
for_each = var.alb_sg_egress_rules
content {
description = format("Allow access for %s", egress.key)
from_port = egress.value.from_port
to_port = egress.value.to_port
protocol = lookup(egress.value, "protocol", "tcp")
cidr_blocks = lookup(egress.value, "cidr_blocks", [])
security_groups = lookup(egress.value, "security_groups", [])
}
}
tags = {
Name = "${var.env}-ALBSecurityGroup"
}
}
resource "aws_autoscaling_group" "app_asg" {
launch_template {
id = aws_launch_template.app.id
version = aws_launch_template.app.latest_version
}
min_size = 2
max_size = 3
desired_capacity = 3
vpc_zone_identifier = values(aws_subnet.private).*.id
health_check_type = "ELB"
target_group_arns = [aws_lb_target_group.app_tg.arn]
lifecycle {
create_before_destroy = true
}
enabled_metrics = [
"GroupMinSize",
"GroupMaxSize",
"GroupDesiredCapacity",
"GroupInServiceInstances",
"GroupTotalInstances"
]
metrics_granularity = "1Minute"
tag {
key = "Name"
value = "${var.env}-asg-instance"
propagate_at_launch = true
}
}
resource "aws_security_group" "ec2_sg" {
name = "${var.env}-ec2-sg"
description = "Security group for EC2 instances in the ASG"
vpc_id = aws_vpc.main.id
dynamic "ingress" {
for_each = var.ec2_sg_ingress_rules
content {
description = format("Allow access for %s", ingress.key)
from_port = ingress.value.from_port
to_port = ingress.value.to_port
protocol = lookup(ingress.value, "protocol", "tcp")
cidr_blocks = lookup(ingress.value, "cidr_blocks", [])
security_groups = lookup(ingress.value, "security_groups", [])
}
}
dynamic "egress" {
for_each = var.ec2_sg_egress_rules
content {
description = format("Allow access for %s", egress.key)
from_port = egress.value.from_port
to_port = egress.value.to_port
protocol = lookup(egress.value, "protocol", "tcp")
cidr_blocks = lookup(egress.value, "cidr_blocks", [])
security_groups = lookup(egress.value, "security_groups", [])
}
}
tags = {
Name = "${var.env}-EC2SecurityGroup"
}
}
resource "aws_autoscaling_policy" "web_policy_up" {
name = "web_policy_up"
scaling_adjustment = 1
adjustment_type = "ChangeInCapacity"
cooldown = 30
autoscaling_group_name = aws_autoscaling_group.app_asg.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 = "20"
dimensions = {
AutoScalingGroupName = "${aws_autoscaling_group.app_asg.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 = 30
autoscaling_group_name = aws_autoscaling_group.app_asg.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.app_asg.name}"
}
alarm_description = "This metric monitor EC2 instance CPU utilization"
alarm_actions = ["${aws_autoscaling_policy.web_policy_down.arn}"]
}