-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
1157 lines (972 loc) · 56.3 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
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
locals {
# Use `local.vpc_id` to give a hint to Terraform that subnets should be deleted before secondary CIDR blocks can be free!
vpc_id = element(concat(aws_vpc_ipv4_cidr_block_association.this.*.vpc_id, aws_vpc.this.*.id, [""]), 0)
public_azs = length(var.public_azs) > 0 ? var.public_azs : length(var.azs) > length(var.public_cidrs) ? slice(var.azs, 0, length(var.public_cidrs)) : var.azs
private_azs = length(var.private_azs) > 0 ? var.private_azs : length(var.azs) > length(var.private_cidrs) ? slice(var.azs, 0, length(var.private_cidrs)) : var.azs
intra_azs = length(var.intra_azs) > 0 ? var.intra_azs : length(var.azs) > length(var.intra_cidrs) ? slice(var.azs, 0, length(var.intra_cidrs)) : var.azs
database_azs = length(var.database_azs) > 0 ? var.database_azs : length(var.azs) > length(var.database_cidrs) ? slice(var.azs, 0, length(var.database_cidrs)) : var.azs
elasticache_azs = length(var.elasticache_azs) > 0 ? var.elasticache_azs : length(var.azs) > length(var.elasticache_cidrs) ? slice(var.azs, 0, length(var.elasticache_cidrs)) : var.azs
redshift_azs = length(var.redshift_azs) > 0 ? var.redshift_azs : length(var.azs) > length(var.redshift_cidrs) ? slice(var.azs, 0, length(var.redshift_cidrs)) : var.azs
elasticsearch_azs = length(var.elasticsearch_azs) > 0 ? var.elasticsearch_azs : length(var.azs) > length(var.elasticsearch_cidrs) ? slice(var.azs, 0, length(var.elasticsearch_cidrs)) : var.azs
custom1_azs = length(var.custom1_azs) > 0 ? var.custom1_azs : length(var.azs) > length(var.custom1_cidrs) ? slice(var.azs, 0, length(var.custom1_cidrs)) : var.azs
custom2_azs = length(var.custom1_azs) > 0 ? var.custom2_azs : length(var.azs) > length(var.custom2_cidrs) ? slice(var.azs, 0, length(var.custom2_cidrs)) : var.azs
custom3_azs = length(var.custom1_azs) > 0 ? var.custom3_azs : length(var.azs) > length(var.custom3_cidrs) ? slice(var.azs, 0, length(var.custom3_cidrs)) : var.azs
vpc_azs = sort(distinct(concat(
local.public_azs,
local.private_azs,
local.intra_azs,
local.database_azs,
local.elasticache_azs,
local.redshift_azs,
local.elasticsearch_azs,
local.custom1_azs,
local.custom2_azs,
local.custom3_azs,
)))
num_of_subnets_with_public_access = length(concat(
length(var.public_cidrs) > 0 ? var.public_cidrs : [],
local.database_create && var.database_enable_public_route ? var.database_cidrs : [],
length(var.elasticache_cidrs) > 0 && var.elasticache_enable_public_route ? var.elasticache_cidrs : [],
length(var.redshift_cidrs) > 0 && var.redshift_enable_public_route ? var.redshift_cidrs : [],
length(var.elasticsearch_cidrs) > 0 && var.elasticsearch_enable_public_route ? var.elasticsearch_cidrs : [],
length(var.custom1_cidrs) > 0 && var.custom1_enable_public_route ? var.custom1_cidrs : [],
length(var.custom2_cidrs) > 0 && var.custom2_enable_public_route ? var.custom2_cidrs : [],
length(var.custom3_cidrs) > 0 && var.custom3_enable_public_route ? var.custom3_cidrs : []
))
internet_gateway_create = var.internet_gateway_create && local.num_of_subnets_with_public_access > 0 ? true : false
egress_only_igw_create = var.enable_ipv6 && var.egress_only_igw_create && local.num_of_subnets_with_public_access > 0 ? true : false
vpc_endpoints_subnet_ids_default = concat(module.public.subnets.*.id, module.private.subnets.*.id, module.intra.subnets.*.id)
vpc_endpoints_route_table_ids_default = concat(module.public.route_table_ids, module.private.route_table_ids, module.intra.route_table_ids)
vpc_endpoints_route_table_num_default = local.public_route_table_num + local.private_route_table_num + local.intra_route_table_num
nat_gateway_required_azs = var.nat_gateway_enable ? length(var.nat_gateway_azs) > 0 ? var.nat_gateway_azs : concat(
local.private_create ? local.private_azs : [],
local.database_create && var.database_route_table_create && var.database_enable_nat_gateway_route ? local.database_azs : [],
local.redshift_create && var.redshift_route_table_create && var.redshift_enable_nat_gateway_route ? local.redshift_azs : [],
local.elasticache_create && var.elasticache_route_table_create && var.elasticache_enable_nat_gateway_route ? local.elasticache_azs : [],
local.elasticsearch_create && var.elasticsearch_route_table_create && var.elasticsearch_enable_nat_gateway_route ? local.elasticsearch_azs : [],
local.custom1_create && var.custom1_route_table_create && var.custom1_enable_nat_gateway_route ? local.custom1_azs : [],
local.custom2_create && var.custom2_route_table_create && var.custom2_enable_nat_gateway_route ? local.custom2_azs : [],
local.custom3_create && var.custom3_route_table_create && var.custom3_enable_nat_gateway_route ? local.custom3_azs : [],
) : []
nat_gateway_suitable_public_azs = var.nat_gateway_enable ? matchkeys(local.public_azs, local.public_azs, local.nat_gateway_required_azs) : []
nat_gateway_selected_azs = length(local.nat_gateway_suitable_public_azs) == length(local.nat_gateway_required_azs) || length(local.nat_gateway_suitable_public_azs) >= 2 ? local.nat_gateway_suitable_public_azs : length(local.public_azs) > 1 ? [
element(concat(local.nat_gateway_suitable_public_azs, local.public_azs), 0),
element(concat(local.nat_gateway_suitable_public_azs, local.public_azs), 1)
] : [element(concat(local.nat_gateway_suitable_public_azs, local.public_azs), 0)]
nat_gateway_azs = length(local.nat_gateway_selected_azs) > 0 ? var.nat_gateway_single ? [element(local.nat_gateway_selected_azs, 0)] : local.nat_gateway_selected_azs : []
nat_gateway_count = length(local.nat_gateway_azs)
nat_gateway_allocation_ids = split(",", length(var.nat_gateway_allocation_ids) == 0 ? join(",", aws_eip.nat.*.id) : join(",", var.nat_gateway_allocation_ids))
public_route_table_num = var.public_route_table_separate ? length(var.public_cidrs) : 1
private_create = length(var.private_cidrs) > 0
private_route_table_single = var.private_route_table_separate == false && (local.nat_gateway_count == 1 || var.nat_gateway_enable == false) ? true : false
private_route_table_num = local.private_route_table_single ? 1 : length(var.private_cidrs)
private_nat_gateways_ids = [for az in local.private_azs : element(aws_nat_gateway.this.*.id, contains(local.nat_gateway_azs, az) ? index(local.nat_gateway_azs, az) : index(local.private_azs, az))]
intra_route_table_num = length(var.intra_cidrs) == 0 ? 0 : var.intra_route_table_separate ? length(var.intra_cidrs) : 1
database_create = length(var.database_cidrs) > 0
database_route_table_use_public_rt = var.database_route_table_create == false && var.database_enable_public_route ? true : false
database_route_table_use_private_rt = var.database_route_table_create || local.database_route_table_use_public_rt ? false : true
database_route_table_single = var.database_route_table_separate == false && ((var.database_enable_nat_gateway_route && local.nat_gateway_count == 1) || var.database_enable_public_route || (var.database_enable_nat_gateway_route == false && var.database_enable_public_route == false)) ? true : false
database_route_table_num = local.database_route_table_use_public_rt || local.database_route_table_use_private_rt ? 0 : local.database_route_table_single ? 1 : length(var.database_cidrs)
database_nat_gateways_ids = var.database_route_table_create && var.database_enable_nat_gateway_route && var.database_enable_public_route == false && length(local.nat_gateway_selected_azs) > 0 ? [
for az in local.database_azs : contains(local.nat_gateway_azs, az) ? element(
aws_nat_gateway.this.*.id,
index(local.nat_gateway_azs, az)
) : element(
[
for i in matchkeys(
range(length(local.nat_gateway_azs)),
local.nat_gateway_azs,
[for az in local.nat_gateway_azs : az if ! contains(local.database_azs, az)]
) : element(aws_nat_gateway.this.*.id, i)
],
index(local.database_azs, az)
)
] : []
elasticache_create = length(var.elasticache_cidrs) > 0
elasticache_route_table_use_public_rt = var.elasticache_route_table_create == false && var.elasticache_enable_public_route ? true : false
elasticache_route_table_use_private_rt = var.elasticache_route_table_create || local.elasticache_route_table_use_public_rt ? false : true
elasticache_route_table_single = var.elasticache_route_table_separate == false && ((var.elasticache_enable_nat_gateway_route && local.nat_gateway_count == 1) || var.elasticache_enable_public_route || (var.elasticache_enable_nat_gateway_route == false && var.elasticache_enable_public_route == false)) ? true : false
elasticache_route_table_num = local.elasticache_route_table_use_public_rt || local.elasticache_route_table_use_private_rt ? 0 : local.elasticache_route_table_single ? 1 : length(var.elasticache_cidrs)
elasticache_nat_gateways_ids = var.elasticache_route_table_create && var.elasticache_enable_nat_gateway_route && var.elasticache_enable_public_route == false && length(local.nat_gateway_selected_azs) > 0 ? [
for az in local.elasticache_azs : contains(local.nat_gateway_azs, az) ? element(
aws_nat_gateway.this.*.id,
index(local.nat_gateway_azs, az)
) : element(
[
for i in matchkeys(
range(length(local.nat_gateway_azs)),
local.nat_gateway_azs,
[for az in local.nat_gateway_azs : az if ! contains(local.elasticache_azs, az)]
) : element(aws_nat_gateway.this.*.id, i)
],
index(local.elasticache_azs, az)
)
] : []
redshift_create = length(var.redshift_cidrs) > 0
redshift_route_table_use_public_rt = var.redshift_route_table_create == false && var.redshift_enable_public_route ? true : false
redshift_route_table_use_private_rt = var.redshift_route_table_create || local.redshift_route_table_use_public_rt ? false : true
redshift_route_table_single = var.redshift_route_table_separate == false && ((var.redshift_enable_nat_gateway_route && local.nat_gateway_count == 1) || var.redshift_enable_public_route || (var.redshift_enable_nat_gateway_route == false && var.redshift_enable_public_route == false)) ? true : false
redshift_route_table_num = local.redshift_route_table_use_public_rt || local.redshift_route_table_use_private_rt ? 0 : local.redshift_route_table_single ? 1 : length(var.redshift_cidrs)
redshift_nat_gateways_ids = var.redshift_route_table_create && var.redshift_enable_nat_gateway_route && var.redshift_enable_public_route == false && length(local.nat_gateway_selected_azs) > 0 ? [
for az in local.redshift_azs : contains(local.nat_gateway_azs, az) ? element(
aws_nat_gateway.this.*.id,
index(local.nat_gateway_azs, az)
) : element(
[
for i in matchkeys(
range(length(local.nat_gateway_azs)),
local.nat_gateway_azs,
[for az in local.nat_gateway_azs : az if ! contains(local.redshift_azs, az)]
) : element(aws_nat_gateway.this.*.id, i)
],
index(local.redshift_azs, az)
)
] : []
elasticsearch_create = length(var.elasticsearch_cidrs) > 0
elasticsearch_route_table_use_public_rt = var.elasticsearch_route_table_create == false && var.elasticsearch_enable_public_route ? true : false
elasticsearch_route_table_use_private_rt = var.elasticsearch_route_table_create || local.elasticsearch_route_table_use_public_rt ? false : true
elasticsearch_route_table_single = var.elasticsearch_route_table_separate == false && ((var.elasticsearch_enable_nat_gateway_route && local.nat_gateway_count == 1) || var.elasticsearch_enable_public_route || (var.elasticsearch_enable_nat_gateway_route == false && var.elasticsearch_enable_public_route == false)) ? true : false
elasticsearch_route_table_num = local.elasticsearch_route_table_use_public_rt || local.elasticsearch_route_table_use_private_rt ? 0 : local.elasticsearch_route_table_single ? 1 : length(var.elasticsearch_cidrs)
elasticsearch_nat_gateways_ids = var.elasticsearch_route_table_create && var.elasticsearch_enable_nat_gateway_route && var.elasticsearch_enable_public_route == false && length(local.nat_gateway_selected_azs) > 0 ? [
for az in local.elasticsearch_azs : contains(local.nat_gateway_azs, az) ? element(
aws_nat_gateway.this.*.id,
index(local.nat_gateway_azs, az)
) : element(
[
for i in matchkeys(
range(length(local.nat_gateway_azs)),
local.nat_gateway_azs,
[for az in local.nat_gateway_azs : az if ! contains(local.elasticsearch_azs, az)]
) : element(aws_nat_gateway.this.*.id, i)
],
index(local.elasticsearch_azs, az)
)
] : []
custom1_create = length(var.custom1_cidrs) > 0
custom1_route_table_single = var.custom1_route_table_separate ? false : true
custom1_route_table_num = length(var.custom1_route_table_ids) > 0 ? length(var.custom1_route_table_ids) : local.custom1_create ? var.custom1_route_table_separate ? length(var.custom1_cidrs) : 1 : 0
custom1_nat_gateways_ids = var.custom1_route_table_create && var.custom1_enable_nat_gateway_route && var.custom1_enable_public_route == false && length(local.nat_gateway_selected_azs) > 0 ? [
for az in local.custom1_azs : contains(local.nat_gateway_azs, az) ? element(
aws_nat_gateway.this.*.id,
index(local.nat_gateway_azs, az)
) : element(
[
for i in matchkeys(
range(length(local.nat_gateway_azs)),
local.nat_gateway_azs,
[for az in local.nat_gateway_azs : az if ! contains(local.custom1_azs, az)]
) : element(aws_nat_gateway.this.*.id, i)
],
index(local.custom1_azs, az)
)
] : []
custom2_create = length(var.custom2_cidrs) > 0
custom2_route_table_single = var.custom2_route_table_separate ? false : true
custom2_route_table_num = length(var.custom2_route_table_ids) > 0 ? length(var.custom2_route_table_ids) : local.custom2_create ? var.custom2_route_table_separate ? length(var.custom2_cidrs) : 1 : 0
custom2_nat_gateways_ids = var.custom2_route_table_create && var.custom2_enable_nat_gateway_route && var.custom2_enable_public_route == false && length(local.nat_gateway_selected_azs) > 0 ? [
for az in local.custom2_azs : contains(local.nat_gateway_azs, az) ? element(
aws_nat_gateway.this.*.id,
index(local.nat_gateway_azs, az)
) : element(
[
for i in matchkeys(
range(length(local.nat_gateway_azs)),
local.nat_gateway_azs,
[for az in local.nat_gateway_azs : az if ! contains(local.custom2_azs, az)]
) : element(aws_nat_gateway.this.*.id, i)
],
index(local.custom2_azs, az)
)
] : []
custom3_create = length(var.custom3_cidrs) > 0
custom3_route_table_single = var.custom3_route_table_separate ? false : true
custom3_route_table_num = length(var.custom3_route_table_ids) > 0 ? length(var.custom3_route_table_ids) : local.custom3_create ? var.custom3_route_table_separate ? length(var.custom3_cidrs) : 1 : 0
custom3_nat_gateways_ids = var.custom3_route_table_create && var.custom3_enable_nat_gateway_route && var.custom3_enable_public_route == false && length(local.nat_gateway_selected_azs) > 0 ? [
for az in local.custom3_azs : contains(local.nat_gateway_azs, az) ? element(
aws_nat_gateway.this.*.id,
index(local.nat_gateway_azs, az)
) : element(
[
for i in matchkeys(
range(length(local.nat_gateway_azs)),
local.nat_gateway_azs,
[for az in local.nat_gateway_azs : az if ! contains(local.custom3_azs, az)]
) : element(aws_nat_gateway.this.*.id, i)
],
index(local.custom3_azs, az)
)
] : []
vpn_gateway_id = var.vpn_gateway_create ? aws_vpn_gateway.this[0].id : var.vpn_gateway_id
}
data "aws_region" "current" {}
#####
# VPC
#####
resource "aws_vpc" "this" {
cidr_block = var.cidr
instance_tenancy = var.instance_tenancy
enable_dns_hostnames = var.enable_dns_hostnames
enable_dns_support = var.enable_dns_support
enable_classiclink = var.enable_classiclink
enable_classiclink_dns_support = var.enable_classiclink_dns_support
assign_generated_ipv6_cidr_block = var.enable_ipv6
tags = merge(
{
Name = format("%s", var.name)
},
var.tags,
var.vpc_tags,
)
}
resource "aws_vpc_ipv4_cidr_block_association" "this" {
count = length(var.secondary_cidr_blocks) > 0 ? length(var.secondary_cidr_blocks) : 0
vpc_id = aws_vpc.this.id
cidr_block = element(var.secondary_cidr_blocks, count.index)
}
##################
# DHCP Options Set
##################
resource "aws_vpc_dhcp_options" "this" {
count = var.dhcp_options_enable ? 1 : 0
domain_name = var.dhcp_options_domain_name
domain_name_servers = var.dhcp_options_domain_name_servers
ntp_servers = length(var.dhcp_options_ntp_servers) > 0 ? var.dhcp_options_ntp_servers : []
netbios_name_servers = length(var.dhcp_options_netbios_name_servers) > 0 ? var.dhcp_options_netbios_name_servers : []
netbios_node_type = var.dhcp_options_netbios_node_type == "" ? var.dhcp_options_netbios_node_type : ""
tags = merge(
{ Name = var.dhcp_options_name != "" ? var.dhcp_options_name : format("%s", var.name) },
var.tags,
var.dhcp_options_tags,
)
}
resource "aws_vpc_dhcp_options_association" "this" {
count = var.dhcp_options_enable ? 1 : 0
vpc_id = local.vpc_id
dhcp_options_id = aws_vpc_dhcp_options.this[0].id
}
#####################
# default network ACL
#####################
resource "aws_default_network_acl" "this" {
count = var.default_network_acl_manage ? 1 : 0
default_network_acl_id = aws_vpc.this.default_network_acl_id
dynamic "ingress" {
for_each = var.default_network_acl_ingress
content {
action = ingress.value.action
cidr_block = lookup(ingress.value, "cidr_block", null)
from_port = ingress.value.from_port
icmp_code = lookup(ingress.value, "icmp_code", null)
icmp_type = lookup(ingress.value, "icmp_type", null)
ipv6_cidr_block = lookup(ingress.value, "ipv6_cidr_block", null)
protocol = ingress.value.protocol
rule_no = ingress.value.rule_no
to_port = ingress.value.to_port
}
}
dynamic "egress" {
for_each = var.default_network_acl_egress
content {
action = egress.value.action
cidr_block = lookup(egress.value, "cidr_block", null)
from_port = egress.value.from_port
icmp_code = lookup(egress.value, "icmp_code", null)
icmp_type = lookup(egress.value, "icmp_type", null)
ipv6_cidr_block = lookup(egress.value, "ipv6_cidr_block", null)
protocol = egress.value.protocol
rule_no = egress.value.rule_no
to_port = egress.value.to_port
}
}
tags = merge(
{ Name = var.default_network_acl_name != "" ? var.default_network_acl_name : format("%s-default", var.name) },
var.tags,
var.default_network_acl_tags,
)
lifecycle {
ignore_changes = [subnet_ids]
}
}
########################
# default security group
########################
resource "aws_default_security_group" "this" {
count = var.default_security_group_manage ? 1 : 0
vpc_id = local.vpc_id
dynamic "ingress" {
for_each = var.default_security_group_ingress
content {
self = lookup(ingress.value, "self", null)
cidr_blocks = compact(split(",", lookup(ingress.value, "cidr_blocks", "")))
ipv6_cidr_blocks = compact(split(",", lookup(ingress.value, "ipv6_cidr_blocks", "")))
prefix_list_ids = compact(split(",", lookup(ingress.value, "prefix_list_ids", "")))
security_groups = compact(split(",", lookup(ingress.value, "security_groups", "")))
description = lookup(ingress.value, "description", null)
from_port = lookup(ingress.value, "from_port", 0)
to_port = lookup(ingress.value, "to_port", 0)
protocol = lookup(ingress.value, "protocol", "-1")
}
}
dynamic "egress" {
for_each = var.default_security_group_egress
content {
self = lookup(egress.value, "self", null)
cidr_blocks = compact(split(",", lookup(egress.value, "cidr_blocks", "")))
ipv6_cidr_blocks = compact(split(",", lookup(egress.value, "ipv6_cidr_blocks", "")))
prefix_list_ids = compact(split(",", lookup(egress.value, "prefix_list_ids", "")))
security_groups = compact(split(",", lookup(egress.value, "security_groups", "")))
description = lookup(egress.value, "description", null)
from_port = lookup(egress.value, "from_port", 0)
to_port = lookup(egress.value, "to_port", 0)
protocol = lookup(egress.value, "protocol", "-1")
}
}
tags = merge(
{ Name = var.default_security_group_name != "" ? var.default_security_group_name : format("%s-default", var.name) },
var.tags,
var.default_security_group_tags
)
}
###################
# Internet gateways
###################
resource "aws_internet_gateway" "this" {
count = local.internet_gateway_create ? 1 : 0
vpc_id = local.vpc_id
tags = merge(
{ Name = var.internet_gateway_name != "" ? var.internet_gateway_name : format("%s", var.name) },
var.tags,
var.internet_gateway_tags,
)
}
resource "aws_egress_only_internet_gateway" "this" {
count = local.egress_only_igw_create ? 1 : 0
vpc_id = local.vpc_id
tags = merge(
{ Name = var.egress_only_igw_name != "" ? var.egress_only_igw_name : format("%s", var.name) },
var.tags,
var.egress_only_igw_tags,
)
}
##############
# NAT Gateways
##############
resource "aws_eip" "nat" {
count = length(var.nat_gateway_allocation_ids) == 0 && var.nat_gateway_enable ? local.nat_gateway_count : 0
vpc = true
tags = merge(
{
Name = format(
"%s%s%s%s%s",
var.name,
var.name != "" ? "-" : "",
var.nat_gateway_eip_name_suffix,
var.nat_gateway_eip_name_suffix != "" ? "-" : "",
var.azs_short_name ? replace(
local.nat_gateway_azs[count.index],
data.aws_region.current.name,
""
) : local.nat_gateway_azs[count.index]
)
},
var.tags,
var.nat_gateway_eip_tags
)
}
resource "aws_nat_gateway" "this" {
count = var.nat_gateway_enable ? local.nat_gateway_count : 0
allocation_id = element(local.nat_gateway_allocation_ids, count.index)
subnet_id = matchkeys(module.public.subnets.*.id, module.public.subnets.*.availability_zone, [local.nat_gateway_azs[count.index]])[0]
tags = merge(
{
Name = format(
"%s%s%s",
var.name,
var.name != "" ? "-" : "",
var.azs_short_name ? replace(
local.nat_gateway_azs[count.index],
data.aws_region.current.name,
""
) : local.nat_gateway_azs[count.index]
)
},
var.tags,
var.nat_gateway_tags,
)
depends_on = [aws_internet_gateway.this]
}
###############
# VPC Endpoints
###############
module "vpc_endpoints" {
source = "./modules/vpc-endpoint"
for_each = var.vpc_endpoints
name = var.name
vpc_id = local.vpc_id
endpoint = each.key
security_group_ids = each.value == true ? [] : lookup(each.value, "security_group_ids", [])
subnet_ids = each.value == true ? local.vpc_endpoints_subnet_ids_default : concat(
lookup(each.value, "public", true) ? module.public.subnets.*.id : [],
lookup(each.value, "private", true) ? module.private.subnets.*.id : [],
lookup(each.value, "intra", true) ? module.intra.subnets.*.id : [],
lookup(each.value, "database", false) ? module.database.subnets.*.id : [],
lookup(each.value, "elasticache", false) ? module.elasticache.subnets.*.id : [],
lookup(each.value, "redshift", false) ? module.redshift.subnets.*.id : [],
lookup(each.value, "elasticsearch", false) ? module.elasticsearch.subnets.*.id : [],
lookup(each.value, "custom1", false) ? module.custom1.subnets.*.id : [],
lookup(each.value, "custom2", false) ? module.custom2.subnets.*.id : [],
lookup(each.value, "custom3", false) ? module.custom3.subnets.*.id : []
)
route_table_ids = each.value == true ? local.vpc_endpoints_route_table_ids_default : concat(
lookup(each.value, "public", true) ? module.public.route_table_ids : [],
lookup(each.value, "private", true) ? module.private.route_table_ids : [],
lookup(each.value, "intra", true) ? module.intra.route_table_ids : [],
lookup(each.value, "database", false) ? module.database.route_table_ids : [],
lookup(each.value, "elasticache", false) ? module.elasticache.route_table_ids : [],
lookup(each.value, "redshift", false) ? module.redshift.route_table_ids : [],
lookup(each.value, "elasticsearch", false) ? module.elasticsearch.route_table_ids : [],
lookup(each.value, "custom1", false) ? module.custom1.route_table_ids : [],
lookup(each.value, "custom2", false) ? module.custom2.route_table_ids : [],
lookup(each.value, "custom3", false) ? module.custom3.route_table_ids : []
)
route_table_association_num = each.value == true ? local.vpc_endpoints_route_table_num_default : sum([
lookup(each.value, "public", true) ? local.public_route_table_num : 0,
lookup(each.value, "private", true) ? local.private_route_table_num : 0,
lookup(each.value, "intra", true) ? local.intra_route_table_num : 0,
lookup(each.value, "database", false) ? local.database_route_table_num : 0,
lookup(each.value, "elasticache", false) ? local.elasticache_route_table_num : 0,
lookup(each.value, "redshift", false) ? local.redshift_route_table_num : 0,
lookup(each.value, "elasticsearch", false) ? local.elasticsearch_route_table_num : 0,
lookup(each.value, "custom1", false) ? local.custom1_route_table_num : 0,
lookup(each.value, "custom2", false) ? local.custom2_route_table_num : 0,
lookup(each.value, "custom3", false) ? local.custom3_route_table_num : 0
])
private_dns_enabled = each.value == true ? false : lookup(each.value, "private_dns_enabled", false)
sagemaker_notebook_endpoint_region = each.value == true ? "" : lookup(each.value, "sagemaker_notebook_endpoint_region", "")
tags = merge(
var.tags,
var.vpc_endpoints_tags,
each.value == true ? {} : lookup(each.value, "tags", {})
)
}
#############
# VPN Gateway
#############
resource "aws_vpn_gateway" "this" {
count = var.vpn_gateway_create ? 1 : 0
vpc_id = local.vpc_id
amazon_side_asn = var.vpn_gateway_amazon_side_asn
availability_zone = var.vpn_gateway_availability_zone
tags = merge(
{ Name = var.vpn_gateway_name != "" ? var.vpn_gateway_name : var.name },
var.tags,
var.vpn_gateway_tags
)
}
resource "aws_vpn_gateway_attachment" "this" {
count = var.vpn_gateway_id != "" && var.vpn_gateway_create == false ? 1 : 0
vpc_id = local.vpc_id
vpn_gateway_id = var.vpn_gateway_id
}
################
# Public subnets
################
module "public" {
source = "./modules/subnets"
name = format(
"%s%s%s",
var.name,
var.name != "" && var.private_name_suffix != "" ? "-" : "",
var.public_name_suffix
)
vpc_id = local.vpc_id
azs = local.public_azs
azs_short_name = var.azs_short_name
cidrs = var.public_cidrs
enable_ipv6 = var.enable_ipv6
vpc_ipv6_cidr_block = var.enable_ipv6 ? aws_vpc.this.ipv6_cidr_block : null
ipv6_prefixes = var.enable_ipv6 ? var.public_ipv6_prefixes : null
internet_gateway_route_create = local.internet_gateway_create
internet_gateway_id = local.internet_gateway_create ? aws_internet_gateway.this[0].id : null
egress_only_igw_id = local.egress_only_igw_create ? aws_egress_only_internet_gateway.this[0].id : null
map_public_ip_on_launch = var.public_map_public_ip_on_launch
map_public_ip_on_launch_per_subnet = var.public_map_public_ip_on_launch_per_subnet
assign_ipv6_address_on_creation = var.enable_ipv6 ? var.public_assign_ipv6_address_on_creation : null
assign_ipv6_address_on_creation_per_subnet = var.enable_ipv6 ? var.public_assign_ipv6_address_on_creation_per_subnet : null
route_table_create = true
route_table_single = var.public_route_table_separate == false ? true : false
route_table_tags = var.public_route_table_tags
route_table_tags_per_route_table = var.public_route_table_tags_per_route_table
network_acl_create = var.public_network_acl_enabled
network_acl_inbound_rules = var.public_network_acl_inbound_rules
network_acl_outbound_rules = var.public_network_acl_outbound_rules
network_acl_tags = var.public_network_acl_tags
vpn_gateway_id = var.public_route_table_vgw_propagation ? local.vpn_gateway_id : ""
tags = merge(var.tags, var.public_tags)
tags_per_subnet = var.public_tags_per_subnet
}
#################
# Private subnets
#################
module "private" {
source = "./modules/subnets"
name = format(
"%s%s%s",
var.name,
var.name != "" && var.private_name_suffix != "" ? "-" : "",
var.private_name_suffix
)
vpc_id = local.vpc_id
azs = local.private_azs
azs_short_name = var.azs_short_name
cidrs = var.private_cidrs
enable_ipv6 = var.enable_ipv6
vpc_ipv6_cidr_block = var.enable_ipv6 ? aws_vpc.this.ipv6_cidr_block : null
ipv6_prefixes = var.enable_ipv6 ? var.private_ipv6_prefixes : null
map_public_ip_on_launch = false
nat_gateway_routes_create = var.nat_gateway_enable
nat_gateways_ids = local.private_nat_gateways_ids
assign_ipv6_address_on_creation = var.enable_ipv6 ? var.private_assign_ipv6_address_on_creation : null
assign_ipv6_address_on_creation_per_subnet = var.enable_ipv6 ? var.private_assign_ipv6_address_on_creation_per_subnet : null
route_table_create = true
route_table_single = local.private_route_table_single
route_table_tags = var.private_route_table_tags
route_table_tags_per_route_table = var.private_route_table_tags_per_route_table
network_acl_create = var.private_network_acl_enabled
network_acl_inbound_rules = var.private_network_acl_inbound_rules
network_acl_outbound_rules = var.private_network_acl_outbound_rules
network_acl_tags = var.private_network_acl_tags
db_subnet_group_create = var.private_db_subnet_group_create
db_subnet_group_name = var.private_db_subnet_group_name != "" ? var.private_db_subnet_group_name : format(
"%s%s%s",
var.name,
var.name != "" && var.private_name_suffix != "" ? "-" : "",
var.private_name_suffix
)
db_subnet_group_description = var.private_db_subnet_group_description != "" ? var.private_db_subnet_group_description : "Database subnet group for ${var.name}"
db_subnet_group_tags = var.private_db_subnet_group_tags
db_subnet_group_azs = var.private_db_subnet_group_azs
elasticache_subnet_group_create = var.private_elasticache_subnet_group_create
elasticache_subnet_group_name = var.private_elasticache_subnet_group_name != "" ? var.private_elasticache_subnet_group_name : format(
"%s%s%s",
var.name,
var.name != "" && var.private_name_suffix != "" ? "-" : "",
var.private_name_suffix
)
elasticache_subnet_group_description = var.private_elasticache_subnet_group_description != "" ? var.private_elasticache_subnet_group_description : "ElastiCache subnet group for ${var.name}"
elasticache_subnet_group_azs = var.private_elasticache_subnet_group_azs
redshift_subnet_group_create = var.private_redshift_subnet_group_create
redshift_subnet_group_name = var.private_redshift_subnet_group_name != "" ? var.private_redshift_subnet_group_name : format(
"%s%s%s",
var.name,
var.name != "" && var.private_name_suffix != "" ? "-" : "",
var.private_name_suffix
)
redshift_subnet_group_description = var.private_redshift_subnet_group_description != "" ? var.private_redshift_subnet_group_description : "Redshift subnet group for ${var.name}"
redshift_subnet_group_tags = var.private_redshift_subnet_group_tags
redshift_subnet_group_azs = var.private_redshift_subnet_group_azs
vpn_gateway_id = var.private_route_table_vgw_propagation ? local.vpn_gateway_id : ""
tags = merge(var.tags, var.private_tags)
tags_per_subnet = var.private_tags_per_subnet
}
#################
# Intra subnets
#################
module "intra" {
source = "./modules/subnets"
name = format(
"%s%s%s",
var.name,
var.name != "" && var.intra_name_suffix != "" ? "-" : "",
var.intra_name_suffix
)
vpc_id = local.vpc_id
azs = local.intra_azs
azs_short_name = var.azs_short_name
cidrs = var.intra_cidrs
enable_ipv6 = var.enable_ipv6
vpc_ipv6_cidr_block = var.enable_ipv6 ? aws_vpc.this.ipv6_cidr_block : null
ipv6_prefixes = var.enable_ipv6 ? var.intra_ipv6_prefixes : null
map_public_ip_on_launch = false
nat_gateways_ids = []
assign_ipv6_address_on_creation = false
route_table_create = true
route_table_single = var.intra_route_table_separate == false
route_table_tags = var.intra_route_table_tags
route_table_tags_per_route_table = var.intra_route_table_tags_per_route_table
network_acl_create = var.intra_network_acl_enabled
network_acl_inbound_rules = var.intra_network_acl_inbound_rules
network_acl_outbound_rules = var.intra_network_acl_outbound_rules
network_acl_tags = var.intra_network_acl_tags
db_subnet_group_create = false
elasticache_subnet_group_create = false
redshift_subnet_group_create = false
tags = merge(var.tags, var.intra_tags)
tags_per_subnet = var.intra_tags_per_subnet
}
##################
# Database subnets
##################
module "database" {
source = "./modules/subnets"
name = format(
"%s%s%s",
var.name,
var.name != "" && var.database_name_suffix != "" ? "-" : "",
var.database_name_suffix
)
vpc_id = local.vpc_id
azs = local.database_azs
azs_short_name = var.azs_short_name
cidrs = var.database_cidrs
enable_ipv6 = var.enable_ipv6
vpc_ipv6_cidr_block = var.enable_ipv6 ? aws_vpc.this.ipv6_cidr_block : ""
ipv6_prefixes = var.enable_ipv6 ? var.database_ipv6_prefixes : []
map_public_ip_on_launch = var.database_map_public_ip_on_launch
map_public_ip_on_launch_per_subnet = var.database_map_public_ip_on_launch_per_subnet
nat_gateways_ids = local.database_nat_gateways_ids
internet_gateway_id = local.internet_gateway_create && var.database_enable_public_route ? aws_internet_gateway.this[0].id : ""
egress_only_igw_id = local.egress_only_igw_create && var.database_enable_public_route ? aws_egress_only_internet_gateway.this[0].id : ""
assign_ipv6_address_on_creation = var.enable_ipv6 ? var.database_assign_ipv6_address_on_creation : false
assign_ipv6_address_on_creation_per_subnet = var.enable_ipv6 ? var.database_assign_ipv6_address_on_creation_per_subnet : []
route_table_create = var.database_route_table_create
route_table_ids = local.database_route_table_use_private_rt ? module.private.route_tables.*.id : local.database_route_table_use_public_rt ? module.public.route_tables.*.id : []
route_table_single = local.database_route_table_single
route_table_tags = var.database_route_table_tags
route_table_tags_per_route_table = var.database_route_table_tags_per_route_table
network_acl_create = var.database_network_acl_enabled
network_acl_inbound_rules = var.database_network_acl_inbound_rules
network_acl_outbound_rules = var.database_network_acl_outbound_rules
network_acl_tags = var.database_network_acl_tags
db_subnet_group_create = var.database_db_subnet_group_create
db_subnet_group_name = var.database_db_subnet_group_name != "" ? var.database_db_subnet_group_name : var.name
db_subnet_group_description = var.database_db_subnet_group_description != "" ? var.database_db_subnet_group_description : "Database subnet group for ${var.name}"
db_subnet_group_tags = var.database_db_subnet_group_tags
db_subnet_group_azs = var.database_db_subnet_group_azs
vpn_gateway_id = var.database_route_table_vgw_propagation ? local.vpn_gateway_id : ""
tags = merge(var.tags, var.database_tags)
tags_per_subnet = var.database_tags_per_subnet
}
#####################
# Elasticache subnets
#####################
module "elasticache" {
source = "./modules/subnets"
name = format(
"%s%s%s",
var.name,
var.name != "" && var.private_name_suffix != "" ? "-" : "",
var.elasticache_name_suffix
)
vpc_id = local.vpc_id
azs = local.elasticache_azs
azs_short_name = var.azs_short_name
cidrs = var.elasticache_cidrs
enable_ipv6 = var.enable_ipv6
vpc_ipv6_cidr_block = var.enable_ipv6 ? aws_vpc.this.ipv6_cidr_block : ""
ipv6_prefixes = var.enable_ipv6 ? var.elasticache_ipv6_prefixes : []
map_public_ip_on_launch = var.elasticache_map_public_ip_on_launch
map_public_ip_on_launch_per_subnet = var.elasticache_map_public_ip_on_launch_per_subnet
nat_gateways_ids = local.elasticache_nat_gateways_ids
internet_gateway_id = local.internet_gateway_create && var.elasticache_enable_public_route ? aws_internet_gateway.this[0].id : ""
egress_only_igw_id = local.egress_only_igw_create && var.elasticache_enable_public_route ? aws_egress_only_internet_gateway.this[0].id : ""
assign_ipv6_address_on_creation = var.enable_ipv6 ? var.elasticache_assign_ipv6_address_on_creation : false
assign_ipv6_address_on_creation_per_subnet = var.enable_ipv6 ? var.elasticache_assign_ipv6_address_on_creation_per_subnet : []
route_table_create = var.elasticache_route_table_create
route_table_ids = local.elasticache_route_table_use_private_rt ? module.private.route_tables.*.id : local.elasticache_route_table_use_public_rt ? module.public.route_tables.*.id : []
route_table_single = local.elasticache_route_table_single
route_table_tags = var.elasticache_route_table_tags
route_table_tags_per_route_table = var.elasticache_route_table_tags_per_route_table
network_acl_create = var.elasticache_network_acl_enabled
network_acl_inbound_rules = var.elasticache_network_acl_inbound_rules
network_acl_outbound_rules = var.elasticache_network_acl_outbound_rules
network_acl_tags = var.elasticache_network_acl_tags
elasticache_subnet_group_create = var.elasticache_elasticache_subnet_group_create
elasticache_subnet_group_name = var.elasticache_elasticache_subnet_group_name != "" ? var.elasticache_elasticache_subnet_group_name : var.name
elasticache_subnet_group_description = var.elasticache_elasticache_subnet_group_description != "" ? var.elasticache_elasticache_subnet_group_description : "ElastiCache subnet group for ${var.name}"
elasticache_subnet_group_azs = var.elasticache_elasticache_subnet_group_azs
tags = merge(var.tags, var.elasticache_tags)
tags_per_subnet = var.elasticache_tags_per_subnet
}
#####################
# Redshift subnets
#####################
module "redshift" {
source = "./modules/subnets"
name = format(
"%s%s%s",
var.name,
var.name != "" && var.private_name_suffix != "" ? "-" : "",
var.redshift_name_suffix
)
vpc_id = local.vpc_id
azs = local.redshift_azs
azs_short_name = var.azs_short_name
cidrs = var.redshift_cidrs
enable_ipv6 = var.enable_ipv6
vpc_ipv6_cidr_block = var.enable_ipv6 ? aws_vpc.this.ipv6_cidr_block : ""
ipv6_prefixes = var.enable_ipv6 ? var.redshift_ipv6_prefixes : []
map_public_ip_on_launch = var.redshift_map_public_ip_on_launch
map_public_ip_on_launch_per_subnet = var.redshift_map_public_ip_on_launch_per_subnet
nat_gateways_ids = local.redshift_nat_gateways_ids
internet_gateway_id = local.internet_gateway_create && var.redshift_enable_public_route ? aws_internet_gateway.this[0].id : ""
egress_only_igw_id = local.egress_only_igw_create && var.redshift_enable_public_route ? aws_egress_only_internet_gateway.this[0].id : ""
assign_ipv6_address_on_creation = var.enable_ipv6 ? var.redshift_assign_ipv6_address_on_creation : false
assign_ipv6_address_on_creation_per_subnet = var.enable_ipv6 ? var.redshift_assign_ipv6_address_on_creation_per_subnet : []
route_table_create = var.redshift_route_table_create
route_table_ids = local.redshift_route_table_use_private_rt ? module.private.route_tables.*.id : local.redshift_route_table_use_public_rt ? module.public.route_tables.*.id : []
route_table_single = local.redshift_route_table_single
route_table_tags = var.redshift_route_table_tags
route_table_tags_per_route_table = var.redshift_route_table_tags_per_route_table
network_acl_create = var.redshift_network_acl_enabled
network_acl_inbound_rules = var.redshift_network_acl_inbound_rules
network_acl_outbound_rules = var.redshift_network_acl_outbound_rules
network_acl_tags = var.redshift_network_acl_tags
redshift_subnet_group_create = var.redshift_redshift_subnet_group_create
redshift_subnet_group_name = var.redshift_redshift_subnet_group_name != "" ? var.redshift_redshift_subnet_group_name : var.name
redshift_subnet_group_description = var.redshift_redshift_subnet_group_description != "" ? var.redshift_redshift_subnet_group_description : "Redshift subnet group for ${var.name}"
redshift_subnet_group_tags = var.redshift_redshift_subnet_group_tags
redshift_subnet_group_azs = var.redshift_redshift_subnet_group_azs
vpn_gateway_id = var.redshift_route_table_vgw_propagation ? local.vpn_gateway_id : ""
tags = merge(var.tags, var.redshift_tags)
tags_per_subnet = var.redshift_tags_per_subnet
}
#######################
# ElasticSearch subnets
#######################
module "elasticsearch" {
source = "./modules/subnets"
name = format(
"%s%s%s",
var.name,
var.name != "" && var.private_name_suffix != "" ? "-" : "",
var.elasticsearch_name_suffix
)
vpc_id = local.vpc_id
azs = local.elasticsearch_azs
azs_short_name = var.azs_short_name
cidrs = var.elasticsearch_cidrs
enable_ipv6 = var.enable_ipv6
vpc_ipv6_cidr_block = var.enable_ipv6 ? aws_vpc.this.ipv6_cidr_block : ""
ipv6_prefixes = var.enable_ipv6 ? var.elasticsearch_ipv6_prefixes : []
map_public_ip_on_launch = var.elasticsearch_map_public_ip_on_launch
map_public_ip_on_launch_per_subnet = var.elasticsearch_map_public_ip_on_launch_per_subnet
nat_gateways_ids = local.elasticsearch_nat_gateways_ids
internet_gateway_id = local.internet_gateway_create && var.elasticsearch_enable_public_route ? aws_internet_gateway.this[0].id : ""
egress_only_igw_id = local.egress_only_igw_create && var.elasticsearch_enable_public_route ? aws_egress_only_internet_gateway.this[0].id : ""
assign_ipv6_address_on_creation = var.enable_ipv6 ? var.elasticsearch_assign_ipv6_address_on_creation : false
assign_ipv6_address_on_creation_per_subnet = var.enable_ipv6 ? var.elasticsearch_assign_ipv6_address_on_creation_per_subnet : []
route_table_create = var.elasticsearch_route_table_create
route_table_ids = local.elasticsearch_route_table_use_private_rt ? module.private.route_tables.*.id : local.elasticsearch_route_table_use_public_rt ? module.public.route_tables.*.id : []
route_table_single = local.elasticsearch_route_table_single
route_table_tags = var.elasticsearch_route_table_tags
route_table_tags_per_route_table = var.elasticsearch_route_table_tags_per_route_table
network_acl_create = var.elasticsearch_network_acl_enabled
network_acl_inbound_rules = var.elasticsearch_network_acl_inbound_rules
network_acl_outbound_rules = var.elasticsearch_network_acl_outbound_rules
network_acl_tags = var.elasticsearch_network_acl_tags
vpn_gateway_id = var.elasticsearch_route_table_vgw_propagation ? local.vpn_gateway_id : ""
tags = merge(var.tags, var.elasticsearch_tags)
tags_per_subnet = var.elasticsearch_tags_per_subnet
}
#################
# Custom1 subnets
#################
module "custom1" {
source = "./modules/subnets"
name = format(
"%s%s%s",
var.name,
var.name != "" && var.custom1_name_suffix != "" ? "-" : "",
var.custom1_name_suffix
)
vpc_id = local.vpc_id
azs = local.custom1_azs
azs_short_name = var.azs_short_name
cidrs = var.custom1_cidrs
enable_ipv6 = var.enable_ipv6
vpc_ipv6_cidr_block = var.enable_ipv6 ? aws_vpc.this.ipv6_cidr_block : ""
ipv6_prefixes = var.enable_ipv6 ? var.custom1_ipv6_prefixes : []
nat_gateways_ids = local.custom1_nat_gateways_ids
internet_gateway_id = local.internet_gateway_create && var.custom1_enable_public_route ? aws_internet_gateway.this[0].id : ""
egress_only_igw_id = local.egress_only_igw_create && var.custom1_enable_public_route ? aws_egress_only_internet_gateway.this[0].id : ""
map_public_ip_on_launch = var.custom1_map_public_ip_on_launch
map_public_ip_on_launch_per_subnet = var.custom1_map_public_ip_on_launch_per_subnet
assign_ipv6_address_on_creation = var.enable_ipv6 ? var.custom1_assign_ipv6_address_on_creation : false
assign_ipv6_address_on_creation_per_subnet = var.enable_ipv6 ? var.custom1_assign_ipv6_address_on_creation_per_subnet : []
route_table_create = var.custom1_route_table_create
route_table_ids = var.custom1_route_table_ids
route_table_single = var.custom1_route_table_separate == false
route_table_tags = var.custom1_route_table_tags
route_table_tags_per_route_table = var.custom1_route_table_tags_per_route_table
network_acl_create = var.custom1_network_acl_enabled
network_acl_inbound_rules = var.custom1_network_acl_inbound_rules
network_acl_outbound_rules = var.custom1_network_acl_outbound_rules
network_acl_tags = var.custom1_network_acl_tags
db_subnet_group_create = var.custom1_db_subnet_group_create
db_subnet_group_name = var.custom1_db_subnet_group_name != "" ? var.custom1_db_subnet_group_name : format(
"%s%s%s",
var.name,
var.name != "" && var.custom1_name_suffix != "" ? "-" : "",
var.custom1_name_suffix
)
db_subnet_group_description = var.custom1_db_subnet_group_description != "" ? var.custom1_db_subnet_group_description : "Database subnet group for ${var.name}-custom1"
db_subnet_group_tags = var.custom1_db_subnet_group_tags
db_subnet_group_azs = var.custom1_db_subnet_group_azs
elasticache_subnet_group_create = var.custom1_elasticache_subnet_group_create
elasticache_subnet_group_name = var.custom1_elasticache_subnet_group_name != "" ? var.custom1_elasticache_subnet_group_name : format(
"%s%s%s",
var.name,
var.name != "" && var.custom1_name_suffix != "" ? "-" : "",
var.custom1_name_suffix
)
elasticache_subnet_group_description = var.custom1_elasticache_subnet_group_description != "" ? var.custom1_elasticache_subnet_group_description : "Elasticache subnet group for ${var.name}-custom1"
elasticache_subnet_group_azs = var.custom1_elasticache_subnet_group_azs
redshift_subnet_group_create = var.custom1_redshift_subnet_group_create
redshift_subnet_group_name = var.custom1_redshift_subnet_group_name != "" ? var.custom1_redshift_subnet_group_name : format(
"%s%s%s",
var.name,
var.name != "" && var.custom1_name_suffix != "" ? "-" : "",
var.custom1_name_suffix
)
redshift_subnet_group_description = var.custom1_redshift_subnet_group_description != "" ? var.custom1_redshift_subnet_group_description : "Redshift subnet group for ${var.name}-custom1"
redshift_subnet_group_tags = var.custom1_redshift_subnet_group_tags
redshift_subnet_group_azs = var.custom1_redshift_subnet_group_azs
vpn_gateway_id = var.custom1_route_table_vgw_propagation ? local.vpn_gateway_id : ""
tags = merge(var.tags, var.custom1_tags)
tags_per_subnet = var.custom1_tags_per_subnet
}
#################
# Custom2 subnets