-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
376 lines (304 loc) · 9.57 KB
/
main.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#VPC
resource "aws_vpc" "And_vpc" {
cidr_block = var.vpc_cidr
instance_tenancy = "default"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "${var.environment}-igw"
Environment = var.environment
}
}
#SUBNETS
#IGW
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.And_vpc.id
tags = {
Name = "${var.environment}-igw"
Environment = var.environment
}
}
#NAT ELASTIC IP
resource "aws_eip" "nat_eip" {
vpc = true
depends_on = [aws_internet_gateway.igw]
}
#NAT GATEWAY
resource "aws_nat_gateway" "natgw" {
allocation_id = aws_eip.nat_eip.id
subnet_id = element(aws_subnet.public_subnet.*.id, 0)
tags = {
Name = "${var.environment}-natgw"
Environment = var.environment
}
}
#PUBLIC SUBNET
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.And_vpc.id
count = length(var.public_subnets_cidr)
cidr_block = element(var.public_subnets_cidr, count.index)
availability_zone = element(var.availability_zones, count.index)
map_public_ip_on_launch = true
tags = {
Name = "${var.environment} -${element(var.availability_zones, count.index)}-public-subnet"
Environment = var.environment
}
}
#PRIVATE SUBNET
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.And_vpc.id
count = length(var.private_subnets_cidr)
cidr_block = element(var.private_subnets_cidr, count.index)
availability_zone = element(var.availability_zones, count.index)
map_public_ip_on_launch = false
tags = {
Name = "${var.environment}-${element(var.availability_zones, count.index)}-private-subnet"
Environment = var.environment
}
}
#PUBLIC SUBNET ROUTE TABLE
resource "aws_route_table" "publicRT" {
vpc_id = aws_vpc.And_vpc.id
tags = {
Name = "${var.environment}-publicRT"
Environment = var.environment
}
}
resource "aws_route" "public_internet_gateway" {
route_table_id = aws_route_table.publicRT.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
resource "aws_route_table_association" "public" {
count = length(var.public_subnets_cidr)
subnet_id = element(aws_subnet.public_subnet.*.id, count.index)
route_table_id = aws_route_table.publicRT.id
}
#PRIVATE SUBNET ROUTE TABLE
resource "aws_route_table" "privateRT" {
vpc_id = aws_vpc.And_vpc.id
tags = {
Name = "${var.environment} -privateRT"
Environment = var.environment
}
}
resource "aws_route" "private_nat_gateway" {
route_table_id = aws_route_table.privateRT.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.natgw.id
}
resource "aws_route_table_association" "private" {
count = length(var.private_subnets_cidr)
subnet_id = element(aws_subnet.private_subnet.*.id, count.index)
route_table_id = aws_route_table.privateRT.id
}
#LOADBALANCER
resource "aws_security_group" "ElbSG" {
name = "ElbSG"
description = "Allow HTTP/HTTPS traffic to instances through Elastic Load Balancer"
vpc_id = aws_vpc.And_vpc.id
ingress {
from_port = 443
to_port = 443
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"]
}
ingress {
from_port = 80
to_port = 80
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 = "${var.environment}-ELBSG"
Environment = var.environment
}
}
resource "aws_lb" "WebserverELB" {
name = "WebserverELB"
load_balancer_type = "application"
security_groups = [aws_security_group.ElbSG.id]
subnets = aws_subnet.public_subnet.*.id
}
resource "aws_lb_listener" "ListenerELB" {
load_balancer_arn = aws_lb.WebserverELB.arn
port = 443
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = aws_acm_certificate.skyetag.arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.target_groupELB.arn
}
}
resource "aws_lb_listener" "https_redirect" {
load_balancer_arn = aws_lb.WebserverELB.arn
port = 80
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
resource "aws_lb_target_group" "target_groupELB" {
port = 443
protocol = "HTTPS"
vpc_id = aws_vpc.And_vpc.id
health_check {
healthy_threshold = 2
protocol = "HTTPS"
unhealthy_threshold = 2
}
lifecycle {
create_before_destroy = true
}
}
#SECURITY_GROUP
resource "aws_security_group" "WebserverSG" {
name = "${var.environment}-default-sg"
description = "Allows SSH,HTTP, HTTPS "
vpc_id = aws_vpc.And_vpc.id
ingress {
from_port = "0"
to_port = "0"
protocol = "tcp"
cidr_blocks = [aws_security_group.ElbSG.id]
}
egress {
from_port = "0"
to_port = "0"
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "${var.environment}-WebserverSG"
Environment = var.environment
}
}
#AUTOSCALING
#LAUNCH CONFIGURATION
resource "aws_launch_configuration" "WebserverLC" {
name_prefix = "test-"
image_id = var.ec2_ami
instance_type = var.ec2_instance_type
key_name = "Sysops"
security_groups = [aws_security_group.WebserverSG.id]
associate_public_ip_address = false
lifecycle {
create_before_destroy = true
}
}
#AUTO-SCALING GROUP
resource "aws_autoscaling_group" "WebserverASG" {
name = "${aws_launch_configuration.WebserverLC.name}-asg"
min_size = 2
desired_capacity = 2
max_size = 4
health_check_type = "ELB"
load_balancers = [aws_lb.WebserverELB.id]
launch_configuration = aws_launch_configuration.WebserverLC.name
vpc_zone_identifier = aws_subnet.private_subnet.*.id
target_group_arns = [aws_lb_target_group.target_groupELB.arn]
lifecycle {
create_before_destroy = true
}
}
#AUTOSCALING POLICY
resource "aws_autoscaling_policy" "scale_up_policy" {
name = "scale_up_policy"
scaling_adjustment = 2
adjustment_type = "ChangeInCapacity"
cooldown = var.cool_down
autoscaling_group_name = aws_autoscaling_group.WebserverASG.name
}
resource "aws_cloudwatch_metric_alarm" "scale_up_alarm" {
alarm_name = "scale_up_alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = var.scale_up_period
statistic = "Average"
threshold = var.GreaterThanOrEqualToThreshold
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.WebserverASG.name
}
alarm_description = "This metric monitor EC2 instance CPU utilization"
alarm_actions = [aws_autoscaling_policy.scale_up_policy.arn]
}
resource "aws_autoscaling_policy" "scale_down_policy" {
name = "scale_down_policy"
scaling_adjustment = -2
adjustment_type = "ChangeInCapacity"
cooldown = 300
autoscaling_group_name = aws_autoscaling_group.WebserverASG.name
}
resource "aws_cloudwatch_metric_alarm" "scale_down_alarm" {
alarm_name = "scale_down_alarm"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "CPUUtilization"
namespace = "AWS/EC2"
period = var.scale_down_period
statistic = "Average"
threshold = var.LessThanOrEqualToThreshold
dimensions = {
AutoScalingGroupName = aws_autoscaling_group.WebserverASG.name
}
alarm_description = "This metric monitor EC2 instance CPU utilization"
alarm_actions = [aws_autoscaling_policy.scale_down_policy.arn]
}
#CERTIFICATE_MANAGER
resource "aws_acm_certificate" "skyetag" {
domain_name = "*.skyetag.com"
subject_alternative_names = ["skyetag.com"]
validation_method = "DNS"
tags = {
Name = "${var.environment} -certificate"
Environment = var.environment
}
}
resource "aws_route53_zone" "dev" {
name = "www.skyetag.com"
tags = {
Name = "${var.environment}-route53"
Environment = var.environment
}
}
resource "aws_route53_record" "validation" {
for_each = {
for dvo in aws_acm_certificate.skyetag.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
zone_id = aws_route53_zone.dev.zone_id
name = each.value.name
type = each.value.type
ttl = 60
records = [
each.value.record,
]
allow_overwrite = true
}
resource "aws_acm_certificate_validation" "skyetag" {
certificate_arn = aws_acm_certificate.skyetag.arn
validation_record_fqdns = [for record in aws_route53_record.validation : record.fqdn]
}