-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
293 lines (260 loc) · 8 KB
/
main.tf
File metadata and controls
293 lines (260 loc) · 8 KB
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
# Creatin VPC
resource "aws_vpc" "projectvpc" {
cidr_block = var.vpc_subnet
enable_dns_hostnames = true
tags = {
Name = var.projectname
}
}
#Public Subnet 1
resource "aws_subnet" "projectvpc_publicsub_1" {
vpc_id = aws_vpc.projectvpc.id
cidr_block = var.publicsubnet1
map_public_ip_on_launch = true
availability_zone = "${var.region}a"
tags = {
Name = "${var.projectname}-public_1"
}
}
#Public Subnet 2
resource "aws_subnet" "projectvpc_publicsub_2" {
vpc_id = aws_vpc.projectvpc.id
cidr_block = var.publicsubnet2
map_public_ip_on_launch = true
availability_zone = "${var.region}b"
tags = {
Name = "${var.projectname}-public_2"
}
}
# Private Subnet 1
resource "aws_subnet" "projectvpc_privatesub_1" {
vpc_id = aws_vpc.projectvpc.id
cidr_block = var.privatesubnet1
availability_zone = "ap-south-1a"
tags = {
Name = "${var.projectname}-private_1"
}
}
# Private Subnet 2
resource "aws_subnet" "projectvpc_privatesub_2" {
vpc_id = aws_vpc.projectvpc.id
cidr_block = var.privatesubnet2
availability_zone = "ap-south-1b"
tags = {
Name = "${var.projectname}-private_2"
}
}
# igw creation
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.projectvpc.id
tags = {
Name = "${var.projectname}-igw"
}
}
# Creating Elastic IP for Nat Gateway
resource "aws_eip" "natip1" {
tags = {
Name = "${var.projectname}-eip-1"
}
}
# Creating Elastic IP for Nat Gateway
resource "aws_eip" "natip2" {
tags = {
Name = "${var.projectname}-eip-2"
}
}
# Creating Nat Gateway 1
resource "aws_nat_gateway" "ngw1" {
allocation_id = aws_eip.natip1.id
subnet_id = aws_subnet.projectvpc_publicsub_1.id
tags = {
Name = "${var.projectname}-ngw-1"
}
}
# Creating Nat Gateway 2
resource "aws_nat_gateway" "ngw2" {
allocation_id = aws_eip.natip2.id
subnet_id = aws_subnet.projectvpc_publicsub_2.id
tags = {
Name = "${var.projectname}-ngw-2"
}
}
################ ROUTE TABLE FOR PUBLIC SUBNET #######################
# Route Table for Public Subnet - 1 and 2
resource "aws_route_table" "public1" {
vpc_id = aws_vpc.projectvpc.id
tags = {
Name = "${var.projectname}-PUBLIC-RTB-1"
}
}
# Route for Public Subnet -1
resource "aws_route" "personalinetroute" {
route_table_id = aws_route_table.public1.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
# Assoicate Public subnet 1 to Route table
resource "aws_route_table_association" "publicsubnet1" {
subnet_id = aws_subnet.projectvpc_publicsub_1.id
route_table_id = aws_route_table.public1.id
}
# Assoicate Public subnet 2 to Route table
resource "aws_route_table_association" "publicsubnet2" {
subnet_id = aws_subnet.projectvpc_publicsub_2.id
route_table_id = aws_route_table.public1.id
}
############################## ROUTE TABLE FOR PRIVATE SUBNET - 1 ###############
# Route Table for Private Subnet
resource "aws_route_table" "pvtrtb1" {
vpc_id = aws_vpc.projectvpc.id
tags = {
Name = "${var.projectname}-PVTRTB-1"
}
}
# Route table for Private subnet-1
resource "aws_route" "personalpvtroute1" {
route_table_id = aws_route_table.pvtrtb1.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.ngw1.id
}
# Associate Association Private - Subnet -1
resource "aws_route_table_association" "pvtsubnet1" {
subnet_id = aws_subnet.projectvpc_privatesub_1.id
route_table_id = aws_route_table.pvtrtb1.id
}
####################### ROUTE TABLE FOR PRIVATE SUBNET - 2 ####################
# Route Table for Private Subnet
resource "aws_route_table" "pvtrtb2" {
vpc_id = aws_vpc.projectvpc.id
tags = {
Name = "${var.projectname}-PVTRTB-2"
}
}
# Associate Association Private - Subnet -2
resource "aws_route_table_association" "pvtsubnet2" {
subnet_id = aws_subnet.projectvpc_privatesub_2.id
route_table_id = aws_route_table.pvtrtb2.id
}
# Route table for Private subnet-2
resource "aws_route" "personalpvtroute2" {
route_table_id = aws_route_table.pvtrtb2.id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.ngw2.id
}
########################################Creating Launch Template ###############################
#Fetching Ubuntu Image Regardless Region
data "aws_ami" "ubuntult" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
# Creating Launch Template
resource "aws_launch_template" "personallt" {
name = "${var.projectname}-launchtemplate"
description = "${var.projectname}-launchtemplate"
image_id = data.aws_ami.ubuntult.id
instance_type = var.instance_type
key_name = var.keyname
vpc_security_group_ids = ["${aws_security_group.tmplsg.id}"]
tag_specifications {
resource_type = "instance"
tags = {
Name = "${var.projectname}-template-instance"
}
}
}
# Creating a Auto Scaling Group
resource "aws_autoscaling_group" "personalAG" {
name = "${var.projectname}-AG"
max_size = 4
min_size = 2
desired_capacity = 2
health_check_grace_period = 20
force_delete = true
launch_template {
id = aws_launch_template.personallt.id
version = "$Latest"
}
vpc_zone_identifier = [aws_subnet.projectvpc_privatesub_1.id, aws_subnet.projectvpc_privatesub_2.id]
lifecycle {
create_before_destroy = true
ignore_changes = [load_balancers, target_group_arns]
}
}
##################### LOAD BALANCING ######################
# Creating Target Group
resource "aws_lb_target_group" "personalprojecttg" {
name = "Front-End"
target_type = "instance"
port = "80"
protocol = "HTTP"
vpc_id = aws_vpc.projectvpc.id
deregistration_delay = 20
health_check {
enabled = true
interval = 10
path = "/"
timeout = 3
unhealthy_threshold = 2
healthy_threshold = 5
port = "traffic-port"
protocol = "HTTP"
matcher = 200
}
}
# Creating Load Balancer
resource "aws_lb" "personallbpublic" {
name = "Personal-LB-Public"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.tmplsg.id]
subnets = [aws_subnet.projectvpc_publicsub_1.id, aws_subnet.projectvpc_publicsub_2.id]
enable_deletion_protection = false
}
# Creating Listner
resource "aws_lb_listener" "personalblistner" {
load_balancer_arn = aws_lb.personallbpublic.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.personalprojecttg.arn
}
}
# Attaching Instance to AutoScaling Group
resource "aws_autoscaling_attachment" "personal-attach" {
autoscaling_group_name = aws_autoscaling_group.personalAG.name
lb_target_group_arn = aws_lb_target_group.personalprojecttg.arn
}
# Creating A Baston Instance pulling Image details with filter
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
# Creating ec2 instance Bastion
resource "aws_instance" "bastion" {
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
subnet_id = aws_subnet.projectvpc_publicsub_1.id
vpc_security_group_ids = [aws_security_group.bastion.id]
key_name = var.keyname
tags = {
Name = "Bastion"
project = "${var.projectname}"
}
}