-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbranch1.tf
592 lines (488 loc) · 16.2 KB
/
branch1.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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# This file will create AWS Infrastructure (VPC, Subnets, IGW, Route Tables, etc) for SD-WAN Branch 1 with host, 1 WAN Emulator Linux and SD-WAN router
# Create Branch VPC:
resource "aws_vpc" "vpc_branch1" {
cidr_block = var.aws_branch1_vpc_cidr
provider = aws.branch1
tags = {
Name = "${var.bucket_prefix} Branch1 VPC"
}
}
# Create Subnets for Branch VPC:
resource "aws_subnet" "branch1_vpc_subnet1" {
vpc_id = aws_vpc.vpc_branch1.id
cidr_block = var.aws_branch1_vpc_subnet1_cidr
map_public_ip_on_launch = "true" //it makes this a public subnet
availability_zone = var.aws_branch1_az
tags = {
Name = "${var.bucket_prefix} Branch1 Subnet-1 Mgmt"
}
}
resource "aws_subnet" "branch1_vpc_subnet2" {
vpc_id = aws_vpc.vpc_branch1.id
cidr_block = var.aws_branch1_vpc_subnet2_cidr
availability_zone = var.aws_branch1_az
tags = {
Name = "${var.bucket_prefix} Branch1 Subnet-2 VPN0"
}
}
resource "aws_subnet" "branch1_vpc_subnet3" {
vpc_id = aws_vpc.vpc_branch1.id
cidr_block = var.aws_branch1_vpc_subnet3_cidr
availability_zone = var.aws_branch1_az
tags = {
Name = "${var.bucket_prefix} Branch1 Subnet-3 VPN10"
}
}
resource "aws_subnet" "branch1_vpc_subnet4" {
vpc_id = aws_vpc.vpc_branch1.id
cidr_block = var.aws_branch1_vpc_subnet4_cidr
availability_zone = var.aws_branch1_az
tags = {
Name = "${var.bucket_prefix} Branch1 Subnet-3 WANEM"
}
}
# Create IGW for Internet Access:
resource "aws_internet_gateway" "branch1_vpc_igw" {
vpc_id = aws_vpc.vpc_branch1.id
tags = {
Name = "${var.bucket_prefix} Branch1 VPC IGW"
}
}
# Create route tables and default route pointing to IGW in MGMT and Inet networks:
resource "aws_route_table" "branch1_vpc_mgmt_rt" {
vpc_id = aws_vpc.vpc_branch1.id
route {
//associated subnet can reach everywhere
cidr_block = "0.0.0.0/0" //CRT uses this IGW to reach internet
gateway_id = aws_internet_gateway.branch1_vpc_igw.id
}
tags = {
Name = "${var.bucket_prefix} Branch1 VPC Mgmt RT"
}
}
resource "aws_route_table" "branch1_vpc_rt_wanem" {
vpc_id = aws_vpc.vpc_branch1.id
route {
cidr_block = "0.0.0.0/0" //CRT uses this IGW to reach internet
gateway_id = aws_internet_gateway.branch1_vpc_igw.id
}
tags = {
Name = "${var.bucket_prefix} Branch1 VPC RT WANEM"
}
}
# Associate CRT and Subnet for Mgmt and Traffic:
resource "aws_route_table_association" "branch1_vpc_rta_subnet-1"{
subnet_id = aws_subnet.branch1_vpc_subnet1.id
route_table_id = aws_route_table.branch1_vpc_mgmt_rt.id
}
resource "aws_route_table_association" "branch1_vpc_rta_subnet-2"{
subnet_id = aws_subnet.branch1_vpc_subnet2.id
route_table_id = aws_route_table.branch1_vpc_rt_vpn0.id
}
resource "aws_route_table_association" "branch1_vpc_rta_subnet-3"{
subnet_id = aws_subnet.branch1_vpc_subnet3.id
route_table_id = aws_route_table.branch1_vpc_rt_vpn10.id
}
resource "aws_route_table_association" "branch1_vpc_rta_subnet-4"{
subnet_id = aws_subnet.branch1_vpc_subnet4.id
route_table_id = aws_route_table.branch1_vpc_rt_wanem.id
}
# Create security group:
resource "aws_security_group" "branch1_vpc_mgmt_sg" {
vpc_id = aws_vpc.vpc_branch1.id
egress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 22 # allow ssh from the CIDR block defined in vars.tf
to_port = 22
protocol = "tcp"
cidr_blocks = [var.ssh_allow_cidr]
}
ingress {
from_port = 443 # allow https access from the CIDR block defined in vars.tf
to_port = 443
protocol = "tcp"
cidr_blocks = [var.ssh_allow_cidr]
}
ingress {
from_port = 8 # allow ping
to_port = 0
protocol = "icmp"
cidr_blocks = [var.ssh_allow_cidr]
}
ingress {
from_port = 830 # allow Netconf
to_port = 830
protocol = "tcp"
// SD-WAN TCP Ports
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0 # allow all traffic between all branches using private IPs
to_port = 0
protocol = "-1"
cidr_blocks = ["10.0.0.0/8"]
}
ingress {
from_port = 23456 # allow SD-WAN TCP ports
to_port = 24156
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 12346 # allow SD-WAN UDP ports
to_port = 13046
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 4500 # allow IKE-based IPSec ports
to_port = 4500
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 500 # allow IKE-based IPSec ports
to_port = 500
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["54.112.112.125/32"] # SD-WAN Branch2 R1 Public IP - needs to be adjusted after initial deployment
}
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["54.112.112.174/32"] # SD-WAN Branch2 Linux Host with TE Mgmt - needs to be adjusted after initial deployment
}
ingress {
from_port = 0
to_port = 0
protocol = "-1"
self = "true"
}
tags = {
Name = "${var.bucket_prefix} Branch1 SD-WAN Mgmt SG"
}
}
resource "aws_security_group" "branch1_vpc_sg" {
vpc_id = aws_vpc.vpc_branch1.id
egress {
from_port = 0
to_port = 0
protocol = -1
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 443 # allow https from the CIDR block defined in vars.tf
to_port = 443
protocol = "tcp"
cidr_blocks = [var.ssh_allow_cidr]
}
ingress {
from_port = 22 # allow ssh from the CIDR block defined in vars.tf
to_port = 22
protocol = "tcp"
cidr_blocks = [var.ssh_allow_cidr]
}
ingress {
from_port = 8 # allow ping from the CIDR block defined in vars.tf
to_port = 0
protocol = "icmp"
cidr_blocks = [var.ssh_allow_cidr]
}
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["10.0.0.0/8"]
}
ingress {
from_port = 830 # allow Netconf
to_port = 830
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 23456 # allow SD-WAN TCP Ports
to_port = 24156
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 12346 # allow SD-WAN UDP Ports
to_port = 13046
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
//IPSec udp ports
ingress {
from_port = 4500 # allow IKE-based IPSec
to_port = 4500
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 500 # allow IKE-based IPSec
to_port = 500
protocol = "udp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 8001 # allow TCP ports 8001-8009n for app probing / monitoring
to_port = 8009
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["54.112.112.125/32"] # SD-WAN Branch2 R1 Public IP - needs to be adjusted after initial deployment
}
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["54.112.112.174/32"] # SD-WAN Branch2 Linux Host with TE Mgmt - needs to be adjusted after initial deployment
}
ingress {
from_port = 0
to_port = 0
protocol = "-1"
self = "true"
}
tags = {
Name = "${var.bucket_prefix} Branch1 SD-WAN VPC SG"
}
}
# Create NICs:
resource "aws_network_interface" "branch1_r1_nic1" {
subnet_id = aws_subnet.branch1_vpc_subnet1.id
private_ips = [var.aws_branch1_r1_nic1_private_ip]
security_groups = [aws_security_group.branch1_vpc_mgmt_sg.id]
source_dest_check = false
description = "${var.bucket_prefix} Branch R1 NIC1 MGMT"
tags = {
Name = "${var.bucket_prefix} Branch1 R1 NIC1 MGMT"
}
}
resource "aws_network_interface" "branch1_r1_nic2" {
subnet_id = aws_subnet.branch1_vpc_subnet2.id
private_ips = [var.aws_branch1_r1_nic2_private_ip]
security_groups = [aws_security_group.branch1_vpc_sg.id]
source_dest_check = false
description = "${var.bucket_prefix} Branch1 R1 NIC2 VPN0"
tags = {
Name = "${var.bucket_prefix} Branch1 R1 NIC2 VPN0"
}
}
resource "aws_network_interface" "branch1_r1_nic3" {
subnet_id = aws_subnet.branch1_vpc_subnet3.id
private_ips = [var.aws_branch1_r1_nic3_private_ip]
security_groups = [aws_security_group.branch1_vpc_sg.id]
source_dest_check = false
description = "${var.bucket_prefix} Branch1 R1 NIC3 Service VPN"
tags = {
Name = "${var.bucket_prefix} Branch1 R1 NIC3 Service VPN"
}
}
# Create SD-WAN Router in the Branch:
resource "aws_instance" "branch1_r1" {
ami = var.aws_ami_id_branch1_r1
instance_type = var.aws_ami_type_branch1_r1
key_name = var.aws_key_pair_name
availability_zone = var.aws_branch1_az
user_data = file("cloud-init-branch1-r1.user_data")
network_interface {
device_index = 0
network_interface_id = aws_network_interface.branch1_r1_nic1.id
delete_on_termination = false
}
network_interface {
device_index = 1
network_interface_id = aws_network_interface.branch1_r1_nic2.id
delete_on_termination = false
}
network_interface {
device_index = 2
network_interface_id = aws_network_interface.branch1_r1_nic3.id
delete_on_termination = false
}
tags = {
Name = "${var.bucket_prefix} Branch1 SD-WAN R1"
}
}
# Allocate and assign public IP address to the mgmt interface for the SD-WAN Router R1:
resource "aws_eip" "branch1_r1_nic1_eip_mgmt" {
vpc = true
network_interface = aws_network_interface.branch1_r1_nic1.id
associate_with_private_ip = var.aws_branch1_r1_nic1_private_ip
depends_on = [aws_instance.branch1_r1]
tags = {
Name = "${var.bucket_prefix} Branch1 SD-WAN R1 Mgmt EIP"
}
}
resource "aws_eip" "sdwan_r1_nic1_eip_vpn0" {
vpc = true
network_interface = aws_network_interface.branch1_r1_nic2.id
associate_with_private_ip = var.aws_branch1_r1_nic2_private_ip
depends_on = [aws_instance.branch1_r1]
tags = {
Name = "${var.bucket_prefix} Branch1 R1 VPN0 EIP"
}
}
# Create NICs for the host:
resource "aws_network_interface" "host1_nic1" {
subnet_id = aws_subnet.branch1_vpc_subnet1.id
private_ips = [var.aws_host1-subnet1_private_ip]
security_groups = [aws_security_group.branch1_vpc_mgmt_sg.id]
source_dest_check = false
description = "${var.bucket_prefix} Branch1 Host1 NIC1 MGMT"
tags = {
Name = "${var.bucket_prefix} Branch1 Host1 NIC1 MGMT"
}
}
resource "aws_network_interface" "host1_nic2" {
subnet_id = aws_subnet.branch1_vpc_subnet3.id
private_ips = [var.aws_host1-subnet3_private_ip]
security_groups = [aws_security_group.branch1_vpc_sg.id]
source_dest_check = false
description = "${var.bucket_prefix} Branch1 Host1 NIC2"
tags = {
Name = "${var.bucket_prefix} Branch1 Host1 NIC2"
}
}
# Create NICs for Linux Public Internet WAN Emulator:
resource "aws_network_interface" "branch1_wanem_nic1" {
subnet_id = aws_subnet.branch1_vpc_subnet1.id
private_ips = [var.aws_branch1_wanem_nic1_private_ip]
security_groups = [aws_security_group.branch1_vpc_sg.id]
source_dest_check = false
description = "${var.bucket_prefix} Branch1 WANEM Linux NIC1 MGMT"
tags = {
Name = "${var.bucket_prefix} Branch1 WANEM Linux NIC1 MGMT"
}
}
resource "aws_network_interface" "branch1_wanem_nic2" {
subnet_id = aws_subnet.branch1_vpc_subnet2.id
private_ips = [var.aws_branch1_wanem_nic2_private_ip]
security_groups = [aws_security_group.branch1_vpc_sg.id]
source_dest_check = false
description = "${var.bucket_prefix} Branch1 WANEM Linux NIC2 VPN0"
tags = {
Name = "${var.bucket_prefix} Branch1 WANEM Linux NIC2 VPN0"
}
}
resource "aws_network_interface" "branch1_wanem_nic3" {
subnet_id = aws_subnet.branch1_vpc_subnet4.id
private_ips = [var.aws_branch1_wanem_nic3_private_ip]
security_groups = [aws_security_group.branch1_vpc_sg.id]
source_dest_check = false
description = "${var.bucket_prefix} Branch1 WANEM NIC3 out to Internet"
tags = {
Name = "${var.bucket_prefix} Branch1 WANEM NIC3 out to Internet"
}
}
resource "aws_route_table" "branch1_vpc_rt_vpn0" {
vpc_id = aws_vpc.vpc_branch1.id
route {
cidr_block = "0.0.0.0/0"
network_interface_id = aws_network_interface.branch1_wanem_nic2.id //NIC of the WANEM
}
tags = {
Name = "${var.bucket_prefix} Branch1 VPC RT VPN0"
}
}
resource "aws_route_table" "branch1_vpc_rt_vpn10" {
vpc_id = aws_vpc.vpc_branch1.id
route {
cidr_block = "0.0.0.0/0" //CRT uses SD-WAN router to reach internet
network_interface_id = aws_network_interface.branch1_r1_nic3.id
}
tags = {
Name = "${var.bucket_prefix} Branch1 VPC RT Service VPN 10"
}
}
# Create Host VM:
resource "aws_instance" "branch1_host1" {
ami = var.aws_ami_id_host1
instance_type = var.aws_ami_type_host1
key_name = var.aws_key_pair_name
availability_zone = var.aws_branch1_az
network_interface {
device_index = 0
network_interface_id = aws_network_interface.host1_nic1.id
delete_on_termination = false
}
network_interface {
device_index = 1
network_interface_id = aws_network_interface.host1_nic2.id
delete_on_termination = false
}
tags = {
Name = "${var.bucket_prefix} Branch1 Host1"
}
}
# Create Linux WAN Emulator in Branch1 VPC:
resource "aws_instance" "branch1_sdwan_wanem" {
ami = var.aws_ami_id_host1
instance_type = var.aws_ami_type_host1
key_name = var.aws_key_pair_name
availability_zone = var.aws_branch1_az
network_interface {
device_index = 0
network_interface_id = aws_network_interface.branch1_wanem_nic1.id
delete_on_termination = false
}
network_interface {
device_index = 1
network_interface_id = aws_network_interface.branch1_wanem_nic2.id
delete_on_termination = false
}
network_interface {
device_index = 2
network_interface_id = aws_network_interface.branch1_wanem_nic3.id
delete_on_termination = false
}
tags = {
Name = "${var.bucket_prefix} Branch1 WANEM Linux"
}
}
# Allocate and assign public IP address to the mgmt interface for the host
resource "aws_eip" "host1_nic1_eip_mgmt" {
vpc = true
network_interface = aws_network_interface.host1_nic1.id
associate_with_private_ip = var.aws_host1-subnet1_private_ip
depends_on = [aws_instance.branch1_host1]
tags = {
Name = "${var.bucket_prefix} Branch1 Host1 Mgmt EIP"
}
}
# Allocate and assign public IP address to the mgmt interface for the WANEM Linux:
resource "aws_eip" "branch1_sdwan_wanem_nic1_eip_mgmt" {
vpc = true
network_interface = aws_network_interface.branch1_wanem_nic1.id
associate_with_private_ip = var.aws_branch1_wanem_nic1_private_ip
depends_on = [aws_instance.branch1_sdwan_wanem]
tags = {
Name = "${var.bucket_prefix} Branch1 WANEM Mgmt EIP"
}
}
resource "aws_eip" "branch1_sdwan_wanem_nic1_eip_inet" {
vpc = true
network_interface = aws_network_interface.branch1_wanem_nic3.id
associate_with_private_ip = var.aws_branch1_wanem_nic3_private_ip
depends_on = [aws_instance.branch1_sdwan_wanem]
tags = {
Name = "${var.bucket_prefix} Branch1 WANEM Inet EIP"
}
}