-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpc.tf
388 lines (337 loc) · 10.2 KB
/
vpc.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
377
378
379
380
381
382
383
384
385
386
387
388
provider "aws" {
region = "eu-west-1"
}
resource "aws_key_pair" "deployer" {
key_name = "svc_ssh_key"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzhmSRIOS7o+Uu4w4WNYG0mS1ES52Lrt+jNug+KfLkwuDoQ8PZtZ8/4FKiIhS/XlcGze/QIgMKpDDDQFLi6/PlYyleH1TeqNHTE3f8Ci/zkoouWHj9WCjKwZYIU9+gMtAoL88t7JpEpCPxUhskTscPkzh83IhSAqZZjoXrI508UoBkaj4giGER714DrgJ6azjpsBMqaGCU+1v/2VRFuXvs/ONnEYWim9jBwfClQmIrRkKS9UcvXkYKeEafIFswJR1Ykx8tc18FBQtji0vsCSohYEz3AlQ+vbOC4QUODhgUTWpeoDphKVWyVsOrcOycDenKR0pAP/PQj8E7Sd3EEKSh JuiceSSH"
}
# Create a VPC
resource "aws_vpc" "svc_vpc" {
cidr_block = "10.0.0.0/16"
tags {
Name = "svc_vpc"
}
}
# Create a security group within the VPC which allows incoming access to the Web Servers.
resource "aws_security_group" "svc_ssh_access" {
name = "svc_ssh_access"
description = "Allow inbound SSH traffic on port 443 and 22"
vpc_id = "${aws_vpc.svc_vpc.id}"
# Allow SSH from everywhere.
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# I've setup SSH on port 443 to see if the network restrictions can be bypassed.
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# Allow all outgoing traffic.
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags {
Name = "svc_ssh_access"
}
}
resource "aws_security_group" "svc_incoming_web" {
name = "svc_incoming_web"
description = "Allow inbound HTTP and SSH traffic"
vpc_id = "${aws_vpc.svc_vpc.id}"
# Allow HTTP traffic from everywhere.
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
# Allow SSH traffic from the local VPCs.
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["10.0.0.0/16"]
}
# Allow all outgoing traffic.
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags {
Name = "svc_incoming_web"
}
}
# Create a public subnet in the "svc_vpc" VPC for each availability zone.
resource "aws_subnet" "svc_vpc_public_subnet_a" {
vpc_id = "${aws_vpc.svc_vpc.id}"
cidr_block = "10.0.129.0/24"
map_public_ip_on_launch = true
availability_zone = "eu-west-1a"
tags {
Name = "svc_vpc_public_subnet_a"
}
}
resource "aws_subnet" "svc_vpc_public_subnet_b" {
vpc_id = "${aws_vpc.svc_vpc.id}"
cidr_block = "10.0.130.0/24"
map_public_ip_on_launch = true
availability_zone = "eu-west-1b"
tags {
Name = "svc_vpc_public_subnet_b"
}
}
resource "aws_subnet" "svc_vpc_public_subnet_c" {
vpc_id = "${aws_vpc.svc_vpc.id}"
cidr_block = "10.0.131.0/24"
map_public_ip_on_launch = true
availability_zone = "eu-west-1c"
tags {
Name = "svc_vpc_public_subnet_c"
}
}
# Create a private subnet in the "svc_vpc" VPC for each availability zone.
resource "aws_subnet" "svc_vpc_private_subnet_a" {
vpc_id = "${aws_vpc.svc_vpc.id}"
cidr_block = "10.0.1.0/24"
map_public_ip_on_launch = false
availability_zone = "eu-west-1a"
tags {
Name = "svc_vpc_private_subnet_a"
}
}
resource "aws_subnet" "svc_vpc_private_subnet_b" {
vpc_id = "${aws_vpc.svc_vpc.id}"
cidr_block = "10.0.2.0/24"
map_public_ip_on_launch = false
availability_zone = "eu-west-1b"
tags {
Name = "svc_vpc_private_subnet_b"
}
}
resource "aws_subnet" "svc_vpc_private_subnet_c" {
vpc_id = "${aws_vpc.svc_vpc.id}"
cidr_block = "10.0.3.0/24"
map_public_ip_on_launch = false
availability_zone = "eu-west-1c"
tags {
Name = "svc_vpc_private_subnet_c"
}
}
# Create instances and add them to the subnets created above.
# Create a bastion host for SSH access and Ansible execution.
resource "aws_instance" "ansible_controller" {
ami = "ami-e1398992"
availability_zone = "eu-west-1a"
instance_type = "t2.micro"
key_name = "svc_ssh_key"
subnet_id = "${aws_subnet.svc_vpc_public_subnet_a.id}"
vpc_security_group_ids = [ "${aws_security_group.svc_ssh_access.id}" ]
tags {
Name = "ansible_controller"
}
user_data = "${file("setup_ansible_controller.sh")}"
}
resource "aws_instance" "web1" {
ami = "ami-e1398992"
availability_zone = "eu-west-1a"
instance_type = "t2.micro"
key_name = "svc_ssh_key"
subnet_id = "${aws_subnet.svc_vpc_public_subnet_a.id}"
vpc_security_group_ids = [ "${aws_security_group.svc_incoming_web.id}" ]
tags {
Name = "web1"
}
private_ip = "10.0.129.128"
user_data = "${file("run_app.sh")}"
}
resource "aws_instance" "web2" {
ami = "ami-e1398992"
availability_zone = "eu-west-1b"
instance_type = "t2.micro"
key_name = "svc_ssh_key"
subnet_id = "${aws_subnet.svc_vpc_public_subnet_b.id}"
vpc_security_group_ids = [ "${aws_security_group.svc_incoming_web.id}" ]
tags {
Name = "web2"
}
private_ip = "10.0.130.128"
user_data = "${file("run_app.sh")}"
}
resource "aws_instance" "web3" {
ami = "ami-e1398992"
availability_zone = "eu-west-1c"
instance_type = "t2.micro"
key_name = "svc_ssh_key"
subnet_id = "${aws_subnet.svc_vpc_public_subnet_c.id}"
vpc_security_group_ids = [ "${aws_security_group.svc_incoming_web.id}" ]
tags {
Name = "web3"
}
private_ip = "10.0.131.128"
user_data = "${file("run_app.sh")}"
}
# Setup a load balancer.
# Create an Internet gateway to route traffic to the Web servers.
resource "aws_internet_gateway" "svc_gateway" {
vpc_id = "${aws_vpc.svc_vpc.id}"
tags {
Name = "svc_gateway"
}
}
# Grant the VPC Internet access on its main route table
resource "aws_route" "svc_internet_access" {
route_table_id = "${aws_vpc.svc_vpc.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.svc_gateway.id}"
}
# Now create a NAT gateway in the public subnet which the private subnet can
# access.
# To do this, setup a elastic IP.
resource "aws_eip" "svc_nat_eip" {
vpc = true
}
# Add a NAT gateway in the public subnet which uses the eip.
# Note that the NAT gateway only runs in subnet A.
resource "aws_nat_gateway" "svc_nat_gateway" {
allocation_id = "${aws_eip.svc_nat_eip.id}"
subnet_id = "${aws_subnet.svc_vpc_public_subnet_a.id}"
depends_on = ["aws_internet_gateway.svc_gateway"]
}
# Create a route to connect the private subnets to the NAT gateway.
resource "aws_route_table" "svc_vpc_private_subnet_nat_route_table" {
vpc_id = "${aws_vpc.svc_vpc.id}"
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = "${aws_nat_gateway.svc_nat_gateway.id}"
}
tags {
Name = "svc_vpc_private_subnet_nat_route_table"
}
}
# Associate the route with the private subnets.
resource "aws_route_table_association" "a" {
subnet_id = "${aws_subnet.svc_vpc_private_subnet_a.id}"
route_table_id = "${aws_route_table.svc_vpc_private_subnet_nat_route_table.id}"
}
resource "aws_route_table_association" "b" {
subnet_id = "${aws_subnet.svc_vpc_private_subnet_b.id}"
route_table_id = "${aws_route_table.svc_vpc_private_subnet_nat_route_table.id}"
}
resource "aws_route_table_association" "c" {
subnet_id = "${aws_subnet.svc_vpc_private_subnet_c.id}"
route_table_id = "${aws_route_table.svc_vpc_private_subnet_nat_route_table.id}"
}
# Setup a load balancer.
resource "aws_elb" "svc_elb" {
name = "scv-elb"
subnets = [
"${aws_subnet.svc_vpc_public_subnet_a.id}",
"${aws_subnet.svc_vpc_public_subnet_b.id}",
"${aws_subnet.svc_vpc_public_subnet_c.id}"
]
security_groups = [ "${aws_security_group.svc_incoming_web.id}" ]
listener {
instance_port = 8080
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
health_check {
healthy_threshold = 2
unhealthy_threshold = 8
timeout = 3
target = "HTTP:8080/establishment/"
interval = 10
}
instances = [ "${aws_instance.web1.id}", "${aws_instance.web2.id}", "${aws_instance.web3.id}"]
cross_zone_load_balancing = true
idle_timeout = 400
connection_draining = true
connection_draining_timeout = 400
tags {
Name = "scv-elb"
}
}
# Create MongoDB instances.
# Create a security group for MongoDB.
resource "aws_security_group" "svc_mongo_access" {
name = "svc_mongo_access"
description = "Allow inbound port 27017 from the Web servers and SSH from the bastion."
vpc_id = "${aws_vpc.svc_vpc.id}"
# Allow MongoDB from the Web Servers.
ingress {
from_port = 27017
to_port = 27017
protocol = "tcp"
cidr_blocks = ["10.0.0.0/16"]
}
# Allow SSH from the bastion server.
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["10.0.0.0/16"]
}
# Allow all outgoing traffic.
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags {
Name = "svc_mongo_access"
}
}
resource "aws_instance" "mongo1" {
ami = "ami-e1398992"
availability_zone = "eu-west-1a"
instance_type = "t2.micro"
key_name = "svc_ssh_key"
subnet_id = "${aws_subnet.svc_vpc_private_subnet_a.id}"
vpc_security_group_ids = [ "${aws_security_group.svc_mongo_access.id}" ]
private_ip = "10.0.1.250"
tags {
Name = "mongo1"
}
}
resource "aws_instance" "mongo2" {
ami = "ami-e1398992"
availability_zone = "eu-west-1b"
instance_type = "t2.micro"
key_name = "svc_ssh_key"
subnet_id = "${aws_subnet.svc_vpc_private_subnet_b.id}"
vpc_security_group_ids = [ "${aws_security_group.svc_mongo_access.id}" ]
private_ip = "10.0.2.250"
tags {
Name = "mongo2"
}
}
resource "aws_instance" "mongo3" {
ami = "ami-e1398992"
availability_zone = "eu-west-1c"
instance_type = "t2.micro"
key_name = "svc_ssh_key"
subnet_id = "${aws_subnet.svc_vpc_private_subnet_c.id}"
vpc_security_group_ids = [ "${aws_security_group.svc_mongo_access.id}" ]
private_ip = "10.0.3.250"
tags {
Name = "mongo3"
}
}