@@ -5,74 +5,18 @@ module "vpc" {
5
5
6
6
for_each = var. vpcs
7
7
8
- name = " ${ var . name_prefix } ${ each . value . name } "
9
- cidr_block = each. value . cidr
10
- nacls = each. value . nacls
11
- security_groups = each. value . security_groups
12
- create_internet_gateway = true
13
- enable_dns_hostnames = true
14
- enable_dns_support = true
15
- instance_tenancy = " default"
16
- }
17
-
18
- # ## SUBNETS ###
19
-
20
- locals {
21
- # Flatten the VPCs and their subnets into a list of maps, each containing the VPC name, subnet name, and subnet details.
22
- subnets_in_vpcs = flatten ([for vk , vv in var . vpcs : [for sk , sv in vv . subnets :
23
- {
24
- cidr = sk
25
- nacl = sv.nacl
26
- az = sv.az
27
- subnet = sv.subnet_group
28
- vpc = vk
29
- create_subnet = try (sv. create_subnet , true )
30
- create_route_table = try (sv. create_route_table , sv. create_subnet , true )
31
- existing_route_table_id = try (sv. existing_route_table_id , null )
32
- associate_route_table = try (sv. associate_route_table , true )
33
- route_table_name = try (sv. route_table_name , null )
34
- local_tags = try (sv. local_tags , {})
35
- }
36
- ]])
37
- # Create a map of subnets, keyed by the VPC name and subnet name.
38
- subnets_with_lists = { for subnet_in_vpc in local . subnets_in_vpcs : " ${ subnet_in_vpc . vpc } -${ subnet_in_vpc . subnet } " => subnet_in_vpc ... }
39
- subnets = { for key , value in local . subnets_with_lists : key => {
40
- vpc = distinct ([for v in value : v . vpc ])[0 ] # VPC name (always take first from the list as key is limitting number of VPCs)
41
- subnet = distinct ([for v in value : v . subnet ])[0 ] # Subnet name (always take first from the list as key is limitting number of subnets)
42
- az = [for v in value : v . az ] # List of AZs
43
- cidr = [for v in value : v . cidr ] # List of CIDRs
44
- nacl = compact ([for v in value : v . nacl ]) # List of NACLs
45
- create_subnet = [for v in value : try (v. create_subnet , true )] # List of create_subnet flags
46
- create_route_table = [for v in value : try (v. create_route_table , v. create_subnet , true )] # List of create_route_table flags
47
- existing_route_table_id = [for v in value : try (v. existing_route_table_id , null )] # List of existing_route_table_id values
48
- associate_route_table = [for v in value : try (v. associate_route_table , true )] # List of associate_route_table flags
49
- route_table_name = [for v in value : try (v. route_table_name , null )] # List of route_table_name values
50
- local_tags = [for v in value : try (v. local_tags , {})] # List of local_tags maps
51
- } }
52
- }
53
-
54
- module "subnet_sets" {
55
- source = " ../../modules/subnet_set"
56
-
57
- for_each = local. subnets
58
-
59
- name = each. value . subnet
60
- vpc_id = module. vpc [each . value . vpc ]. id
61
- has_secondary_cidrs = module. vpc [each . value . vpc ]. has_secondary_cidrs
62
- nacl_associations = {
63
- for index , az in each . value . az : az =>
64
- lookup (module. vpc [each . value . vpc ]. nacl_ids , each. value . nacl [index ], null ) if length (each. value . nacl ) > 0
8
+ region = var. region
9
+ name = " ${ var . name_prefix } ${ each . value . name } "
10
+ cidr_block = each. value . cidr_block
11
+ subnets = each. value . subnets
12
+ nacls = each. value . nacls
13
+ security_groups = each. value . security_groups
14
+
15
+ options = {
16
+ enable_dns_hostnames = true
17
+ enable_dns_support = true
18
+ instance_tenancy = " default"
65
19
}
66
- cidrs = {
67
- for index , cidr in each . value . cidr : cidr => {
68
- az = each.value.az[index]
69
- create_subnet = each.value.create_subnet[index]
70
- create_route_table = each.value.create_route_table[index]
71
- existing_route_table_id = each.value.existing_route_table_id[index]
72
- associate_route_table = each.value.associate_route_table[index]
73
- route_table_name = each.value.route_table_name[index]
74
- local_tags = each.value.local_tags[index]
75
- } }
76
20
}
77
21
78
22
# ## ROUTES ###
@@ -83,7 +27,7 @@ locals {
83
27
#
84
28
# tgw_default = {
85
29
# vpc = "security_vpc"
86
- # subnet = "tgw_attach"
30
+ # subnet_group = "tgw_attach"
87
31
# to_cidr = "0.0.0.0/0"
88
32
# next_hop_key = "security_gwlb_outbound"
89
33
# next_hop_type = "gwlbe_endpoint"
@@ -106,7 +50,7 @@ locals {
106
50
for vk , vv in var . vpcs : [
107
51
for rk , rv in vv . routes : {
108
52
vpc = rv . vpc
109
- subnet = rv . subnet_group
53
+ subnet_group = rv . subnet_group
110
54
to_cidr = rv . to_cidr
111
55
next_hop_type = rv . next_hop_type
112
56
next_hop_map = {
@@ -118,9 +62,9 @@ locals {
118
62
}
119
63
]]))
120
64
vpc_routes = {
121
- for route in local . vpc_routes_with_next_hop_map : " ${ route . vpc } -${ route . subnet } -${ route . to_cidr } " => {
65
+ for route in local . vpc_routes_with_next_hop_map : " ${ route . vpc } -${ route . subnet_group } -${ route . to_cidr } " => {
122
66
vpc = route.vpc
123
- subnet = route.subnet
67
+ subnet_group = route.subnet_group
124
68
to_cidr = route.to_cidr
125
69
next_hop_set = lookup (route. next_hop_map , route. next_hop_type , null )
126
70
}
@@ -132,7 +76,7 @@ module "vpc_routes" {
132
76
133
77
for_each = local. vpc_routes
134
78
135
- route_table_ids = module. subnet_sets [ " ${ each . value . vpc } - ${ each . value . subnet } " ] . unique_route_table_ids
79
+ route_table_ids = { for k , v in module . vpc [ each . value . vpc ] . route_tables : v . az => v . id if v . subnet_group == each . value . subnet_group }
136
80
to_cidr = each. value . to_cidr
137
81
next_hop_set = each. value . next_hop_set
138
82
}
@@ -144,7 +88,7 @@ module "natgw_set" {
144
88
145
89
for_each = var. natgws
146
90
147
- subnets = module. subnet_sets [ " ${ each . value . vpc } - ${ each . value . subnet_group } " ] . subnets
91
+ subnets = { for k , v in module . vpc [ each . value . vpc ] . subnets : v . az => v if v . subnet_group == each . value . subnet_group }
148
92
}
149
93
150
94
# ## TGW ###
@@ -167,8 +111,8 @@ module "transit_gateway_attachment" {
167
111
for_each = var. tgw . attachments
168
112
169
113
name = " ${ var . name_prefix } ${ each . value . name } "
170
- vpc_id = module. subnet_sets [ " ${ each . value . vpc } - ${ each . value . subnet_group } " ] . vpc_id
171
- subnets = module. subnet_sets [ " ${ each . value . vpc } - ${ each . value . subnet_group } " ] . subnets
114
+ vpc_id = module. vpc [ each . value . vpc ] . id
115
+ subnets = { for k , v in module . vpc [ each . value . vpc ] . subnets : k => v if v . subnet_group == each . value . subnet_group }
172
116
transit_gateway_route_table = module. transit_gateway . route_tables [each . value . route_table ]
173
117
propagate_routes_to = {
174
118
to1 = module.transit_gateway.route_tables[each.value.propagate_routes_to].id
@@ -198,8 +142,8 @@ module "gwlb" {
198
142
for_each = var. gwlbs
199
143
200
144
name = " ${ var . name_prefix } ${ each . value . name } "
201
- vpc_id = module. subnet_sets [ " ${ each . value . vpc } - ${ each . value . subnet_group } " ] . vpc_id
202
- subnets = module. subnet_sets [ " ${ each . value . vpc } - ${ each . value . subnet_group } " ] . subnets
145
+ vpc_id = module. vpc [ each . value . vpc ] . id
146
+ subnets = { for k , v in module . vpc [ each . value . vpc ] . subnets : v . az => v if v . subnet_group == each . value . subnet_group }
203
147
}
204
148
205
149
resource "aws_lb_target_group_attachment" "this" {
@@ -221,13 +165,13 @@ module "gwlbe_endpoint" {
221
165
222
166
name = " ${ var . name_prefix } ${ each . value . name } "
223
167
gwlb_service_name = module. gwlb [each . value . gwlb ]. endpoint_service . service_name
224
- vpc_id = module. subnet_sets [ " ${ each . value . vpc } - ${ each . value . subnet_group } " ] . vpc_id
225
- subnets = module. subnet_sets [ " ${ each . value . vpc } - ${ each . value . subnet_group } " ] . subnets
168
+ vpc_id = module. vpc [ each . value . vpc ] . id
169
+ subnets = { for k , v in module . vpc [ each . value . vpc ] . subnets : v . az => v if v . subnet_group == each . value . subnet_group }
226
170
227
171
act_as_next_hop_for = each. value . act_as_next_hop ? {
228
172
" from-igw-to-lb" = {
229
173
route_table_id = module.vpc[each.value.vpc].internet_gateway_route_table.id
230
- to_subnets = module.subnet_sets[ " ${ each . value . from_igw_to_vpc } - ${ each . value . from_igw_to_subnet_group } " ].subnets
174
+ to_subnets = { for k , v in module . vpc [ each . value . from_igw_to_vpc ] . subnets : v . az => v if v . subnet_group == each . value . from_igw_to_subnet_group }
231
175
}
232
176
# The routes in this section are special in that they are on the "edge", that is they are part of an IGW route table,
233
177
# and AWS allows their destinations to only be:
@@ -336,7 +280,7 @@ module "vmseries" {
336
280
device_index = v.device_index
337
281
security_group_ids = try ([module . vpc [each . value . common . vpc ]. security_group_ids [v . security_group ]], [])
338
282
source_dest_check = try (v. source_dest_check , false )
339
- subnet_id = module.subnet_sets[ " ${ v . vpc } - ${ v . subnet_group } " ].subnets[ each.value.az].id
283
+ subnet_id = module.vpc[ v.vpc].subnets[ " ${ v . subnet_group } ${ each . value . az } " ].id
340
284
create_public_ip = try (v. create_public_ip , false )
341
285
}
342
286
}
@@ -345,7 +289,7 @@ module "vmseries" {
345
289
346
290
iam_instance_profile = aws_iam_instance_profile. vm_series_iam_instance_profile . name
347
291
ssh_key_name = var. ssh_key_name
348
- tags = var. global_tags
292
+ tags = var. tags
349
293
}
350
294
351
295
# ## Public ALB and NLB used in centralized model ###
@@ -356,13 +300,13 @@ module "public_alb" {
356
300
for_each = { for k , v in var . vmseries : k => v }
357
301
358
302
lb_name = " ${ var . name_prefix } ${ each . value . application_lb . name } "
359
- subnets = { for k , v in module . subnet_sets [ " ${ each . value . vpc } - ${ each . value . application_lb . subnet_group } " ]. subnets : k => { id = v.id } }
303
+ subnets = { for k , v in module . vpc [ each . value . vpc ]. subnets : k => { id = v.id } if v . subnet_group == each . value . application_lb . subnet_group }
360
304
vpc_id = module. vpc [each . value . vpc ]. id
361
305
security_groups = [module . vpc [each . value . vpc ]. security_group_ids [each . value . application_lb . security_group ]]
362
306
rules = each. value . application_lb . rules
363
307
targets = { for vmseries in local . vmseries_instances : " ${ vmseries . group } -${ vmseries . instance } " => module . vmseries [" ${ vmseries . group } -${ vmseries . instance } " ]. interfaces [" public" ]. private_ip }
364
308
365
- tags = var. global_tags
309
+ tags = var. tags
366
310
}
367
311
368
312
module "public_nlb" {
@@ -372,7 +316,7 @@ module "public_nlb" {
372
316
373
317
name = " ${ var . name_prefix } ${ each . value . network_lb . name } "
374
318
internal_lb = false
375
- subnets = { for k , v in module . subnet_sets [ " ${ each . value . vpc } - ${ each . value . network_lb . subnet_group } " ]. subnets : k => { id = v.id } }
319
+ subnets = { for k , v in module . vpc [ each . value . vpc ]. subnets : k => { id = v.id } if v . subnet_group == each . value . network_lb . subnet_group }
376
320
vpc_id = module. vpc [each . value . vpc ]. id
377
321
378
322
balance_rules = { for k , v in each . value . network_lb . rules : k => {
@@ -384,7 +328,7 @@ module "public_nlb" {
384
328
targets = { for vmseries in local . vmseries_instances : " ${ vmseries . group } -${ vmseries . instance } " => module . vmseries [" ${ vmseries . group } -${ vmseries . instance } " ]. interfaces [" public" ]. private_ip }
385
329
} }
386
330
387
- tags = var . global_tags
331
+ tags = var . tags
388
332
}
389
333
390
334
# ## SPOKE VM INSTANCES ####
@@ -446,9 +390,9 @@ resource "aws_instance" "spoke_vms" {
446
390
ami = data. aws_ami . this . id
447
391
instance_type = each. value . type
448
392
key_name = var. ssh_key_name
449
- subnet_id = module. subnet_sets [ " ${ each . value . vpc } - ${ each . value . subnet_group } " ] . subnets [ each . value . az ]. id
393
+ subnet_id = module. vpc [ each . value . vpc ] . subnets [ " ${ each . value . subnet_group } ${ each . value . az } " ]. id
450
394
vpc_security_group_ids = [module . vpc [each . value . vpc ]. security_group_ids [each . value . security_group ]]
451
- tags = merge ({ Name = " ${ var . name_prefix } ${ each . key } " }, var. global_tags )
395
+ tags = merge ({ Name = " ${ var . name_prefix } ${ each . key } " }, var. tags )
452
396
iam_instance_profile = aws_iam_instance_profile. spoke_vm_iam_instance_profile . name
453
397
454
398
root_block_device {
@@ -485,8 +429,8 @@ module "app_lb" {
485
429
486
430
name = " ${ var . name_prefix } ${ each . key } "
487
431
internal_lb = true
488
- subnets = { for k , v in module . subnet_sets [ " ${ each . value . vpc } - ${ each . value . subnet_group } " ]. subnets : k => { id = v.id } }
489
- vpc_id = module. subnet_sets [ " ${ each . value . vpc } - ${ each . value . subnet_group } " ] . vpc_id
432
+ subnets = { for k , v in module . vpc [ each . value . vpc ]. subnets : k => { id = v.id } if v . subnet_group == each . value . subnet_group }
433
+ vpc_id = module. vpc [ each . value . vpc ] . id
490
434
491
435
balance_rules = {
492
436
" SSH-traffic" = {
@@ -512,5 +456,5 @@ module "app_lb" {
512
456
}
513
457
}
514
458
515
- tags = var.global_tags
459
+ tags = var.tags
516
460
}
0 commit comments