-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
299 lines (263 loc) · 7.05 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
# This will launch an AMI on AWS
# What do we want to do and where would we like to create the instance
# Syntax for terraform is similar to json - we use { to write block of code }
provider "aws" {
# which region do we have the AMI available
region = var.region
}
# Creating the VPC
resource "aws_vpc" "Eng67_Anais_Terraform_VPC" {
cidr_block = var.vpcCIDRblock
tags = {
Name = "Eng67.Anais.Terraform.VPC"
}
} # end Creating VPC
# Creating APP instance - launch an EC2 Instance from the AMI
resource "aws_instance" "app_instance" {
ami = var.amiapp
# what type of ec2 instance we want to create = t2.micro
instance_type = "t2.micro"
// vpc_id = aws_vpc.Eng67_Anais_Terraform_VPC.id
subnet_id = aws_subnet.public_subnet.id
vpc_security_group_ids = [aws_security_group.app_sg.id]
# public IP
associate_public_ip_address = true
tags = {
Name = "Eng67.Anais.terraform.app"
}
} # end Creating APP instance
# Creating DB instance
resource "aws_instance" "db_instance" {
ami = var.amidb
# what type of ec2 instance we want to create = t2.micro
instance_type = "t2.micro"
// vpc_id = aws_vpc.Eng67_Anais_Terraform_VPC.id
subnet_id = aws_subnet.private_subnet.id
vpc_security_group_ids = [aws_security_group.db_sg.id]
associate_public_ip_address = true
tags = {
Name = "Eng67.Anais.terraform.db"
}
} # end Creating DB instance
# Creating public subnet block of code
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.Eng67_Anais_Terraform_VPC.id
cidr_block = var.subnetpublicCIDRblock
map_public_ip_on_launch = var.mapPublicIP
availability_zone = var.availabilityZone
tags = {
Name = "Eng67.Anais.Terraform.Subnet.Public"
}
} # end Creating Public Subnet
# Creating a private subnet block of code
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.Eng67_Anais_Terraform_VPC.id
cidr_block = var.subnetprivateCIDRblock
map_public_ip_on_launch = var.mapPublicIP
availability_zone = var.availabilityZone
tags = {
Name = "Eng67.Anais.Terraform.Subnet.Private"
}
} # end Creating Private Subnet
# Create a app security group and attach it to the VPC
resource "aws_security_group" "app_sg" {
vpc_id = aws_vpc.Eng67_Anais_Terraform_VPC.id
name = "Eng67.Anais.app.SG.terraform"
description = "Eng67.Anais.app.SG.terraform"
# Allow ingress of port 80
ingress {
cidr_blocks = var.ingressCIDRblock
from_port = 80
to_port = 80
protocol = "tcp"
}
# Allow egress of all ports
egress {
cidr_blocks = var.egressCIDRblock
from_port = 0
protocol = "-1"
to_port = 0
}
tags = {
Name = "Eng67.Anais.app.SG"
Description = "Security group for app"
}
} # end creating app security group
# Creating db security group
resource "aws_security_group" "db_sg" {
vpc_id = aws_vpc.Eng67_Anais_Terraform_VPC.id
name = "Eng67.Anais.DBSG.terraform"
description = "DB security group"
# Allow ingress from my IP through SSH
ingress {
description = "Allowing app access to DB"
from_port = 27017
to_port = 27017
protocol = "tcp"
security_groups = [aws_security_group.app_sg.id]
}
# Allow egress of all ports
egress {
cidr_blocks = var.egressCIDRblock
from_port = 0
protocol = "-1"
to_port = 0
}
tags = {
Name = "Eng67.Anais.DBSG.terraform"
Description = "Eng67.Anais.DBSG.terraform"
}
} # end db security group
# Create internet gateway
resource "aws_internet_gateway" "terraform_IGW" {
vpc_id = aws_vpc.Eng67_Anais_Terraform_VPC.id
tags = {
Name = "Eng67.Anais.IGW"
}
}
# Route table public
resource "aws_route_table" "public_route_table" {
vpc_id = aws_vpc.Eng67_Anais_Terraform_VPC.id
// subnet_id = aws_subnet.public_subnet.id
tags = {
Name = "Eng67.Anais.Terraform.Route.Public"
}
}
# Creating the Internet Access
resource "aws_route" "Internet_access" {
route_table_id = aws_route_table.public_route_table.id
destination_cidr_block = var.destinationCIDRblock
gateway_id = aws_internet_gateway.terraform_IGW.id
}
# Associating the route table with the subnet
resource "aws_route_table_association" "route_table_association" {
route_table_id = aws_route_table.public_route_table.id
subnet_id = aws_subnet.public_subnet.id
}
//# Creating Private subnet Network Access Control List NACL
//resource "aws_network_acl" "private_nacl" {
// vpc_id = aws_vpc.Eng67_Anais_Terraform_VPC.id
// subnet_ids = [aws_subnet.private_subnet.id]
//
// # ingress rules
// ingress {
// action = "allow"
// from_port = 27017
// protocol = "tcp"
// rule_no = 1
// to_port = 27017
// cidr_block = var.subnetpublicCIDRblock
//
// }
//
// ingress {
// action = "allow"
// from_port = 22
// protocol = "tcp"
// rule_no = 100
// to_port = 22
// cidr_block = var.subnetpublicCIDRblock
// }
//
// ingress {
// action = "allow"
// from_port = 0
// protocol = ""
// rule_no = 200
// to_port = 0
// cidr_block = var.ingressCIDRblock
// }
//
// # egress rule
// egress {
// action = "allow"
// from_port = 0
// protocol = "all"
// rule_no = 200
// to_port = 0
// }
//
// # egress rule
//
// egress {
// action = "allow"
// from_port = 0
// to_port = 0
// rule_no = 1
// protocol = "all"
// cidr_block = var.subnetpublicCIDRblock
// }
//
// tags = {
// Name = "Eng67.Anais.terraform.NACL.private"
// }
//}
//
//
//# Public NACL
//resource "aws_network_acl" "public_nacl" {
// vpc_id = aws_vpc.Eng67_Anais_Terraform_VPC.id
// subnet_id = aws_subnet.public_subnet.id
//
// # HTTP (80)
// ingress {
// rule_no = 100
// protocol = "tcp"
// from_port = 80
// to_port = 80
// action = "allow"
// cidr_block = var.ingressCIDRblock
// }
//
// # HTTPS (443)
// ingress {
// action = "allow"
// from_port = 443
// protocol = "tcp"
// rule_no = 110
// to_port = 443
// cidr_block = var.ingressCIDRblock
// }
//
//
// # Custom TCP Rule
// ingress {
// action = "allow"
// from_port = 1024
// protocol = "tcp"
// rule_no = 120
// to_port = 65535
// }
//
// # Custom TCP Rule
// ingress {
// action = "allow"
// from_port = 27017
// protocol = "tcp"
// rule_no = 140
// to_port = 27017
// cidr_block = var.vpcCIDRblock
// }
//
// # Custom TCP rule
// ingress {
// action = "allow"
// from_port = 1024
// protocol = "tcp"
// rule_no = 141
// to_port = 65535
// cidr_block = var.vpcCIDRblock
// }
//
// egress {
// action = "allow"
// from_port = 0
// protocol = "tcp"
// rule_no = 100
// to_port = 65535
// }
//
// tags = {
// Name = "Eng67.Anais.terraform.NACL.public"
// }
//} # End creating public NACL