forked from aws-ia/terraform-aws-network-hubandspoke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
367 lines (301 loc) · 16.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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
# --- root/main.tf ---
# ---------------- AWS TRANSIT GATEWAY ----------------
resource "aws_ec2_transit_gateway" "tgw" {
count = local.create_tgw ? 1 : 0
description = "Transit_Gateway-${var.identifier}"
default_route_table_association = "disable"
default_route_table_propagation = "disable"
amazon_side_asn = try(var.transit_gateway_attributes.amazon_side_asn, 64512)
auto_accept_shared_attachments = try(var.transit_gateway_attributes.auto_accept_shared_attachments, "disable")
dns_support = try(var.transit_gateway_attributes.dns_support, "enable")
multicast_support = try(var.transit_gateway_attributes.multicast_support, "disable")
transit_gateway_cidr_blocks = try(var.transit_gateway_attributes.transit_gateway_cidr_blocks, [])
vpn_ecmp_support = try(var.transit_gateway_attributes.vpn_ecmp_support, "enable")
tags = merge(
{
Name = try(var.transit_gateway_attributes.name, "tgw-${var.identifier}")
},
module.tags.tags_aws,
try(var.transit_gateway_attributes.tags, {})
)
}
# ---------------- CENTRAL VPCs ----------------
module "central_vpcs" {
for_each = var.central_vpcs
source = "aws-ia/vpc/aws"
version = "= 4.4.0"
name = try(each.value.name, each.key)
vpc_id = try(each.value.vpc_id, null)
cidr_block = try(each.value.cidr_block, null)
vpc_secondary_cidr = try(each.value.vpc_secondary_cidr, false)
az_count = each.value.az_count
vpc_enable_dns_hostnames = try(each.value.vpc_enable_dns_hostnames, true)
vpc_enable_dns_support = try(each.value.vpc_enable_dns_support, true)
vpc_instance_tenancy = try(each.value.vpc_instance_tenancy, "default")
vpc_ipv4_ipam_pool_id = try(each.value.vpc_ipv4_ipam_pool_id, null)
vpc_ipv4_netmask_length = try(each.value.vpc_ipv4_netmask_length, null)
vpc_flow_logs = try(each.value.vpc_flow_logs, local.vpc_flow_logs_default)
subnets = merge(try(each.value.subnets, {}), local.subnet_config[each.key])
transit_gateway_id = local.transit_gateway_id
transit_gateway_routes = local.transit_gateway_routes[each.key]
tags = merge(
module.tags.tags_aws,
try(each.value.tags, {})
)
}
# -------- TRANSIT GATEWAY ROUTE TABLE AND ASSOCATIONS - CENTRAL VPCS --------
resource "aws_ec2_transit_gateway_route_table" "tgw_route_table" {
for_each = {
for k, v in module.central_vpcs : k => v if local.associate_and_propagate_to_tgw[k]
}
transit_gateway_id = local.transit_gateway_id
tags = merge(
{
Name = "${each.key}-tgw-rt-${var.identifier}"
},
module.tags.tags_aws
)
}
resource "aws_ec2_transit_gateway_route_table_association" "tgw_route_table_association" {
for_each = {
for k, v in module.central_vpcs : k => v if local.associate_and_propagate_to_tgw[k]
}
transit_gateway_attachment_id = each.value.transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.tgw_route_table[each.key].id
}
# --------- TRANSIT GATEWAY ROUTE TABLE, ASSOCIATIONS, AND PROPAGATIONS (IF APPLIES) - SPOKE VPCS ---------
# Transit Gateway Route Tables (Routing Domains)
resource "aws_ec2_transit_gateway_route_table" "spokes_tgw_rt" {
for_each = toset(local.routing_domains)
transit_gateway_id = local.transit_gateway_id
tags = merge(
{
Name = "${each.key}-tgw-rt-${var.identifier}"
},
module.tags.tags_aws
)
}
# Spoke VPC TGW association
resource "aws_ec2_transit_gateway_route_table_association" "spokes_tgw_rt_association" {
count = local.number_vpcs
transit_gateway_attachment_id = local.vpc_information[count.index].transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.spokes_tgw_rt[try(local.vpc_information[count.index].routing_domain, "spokes")].id
}
# Spoke VPC TGW propagation
resource "aws_ec2_transit_gateway_route_table_propagation" "spokes_to_spokes_propagation" {
count = local.spoke_to_spoke_propagation ? local.number_vpcs : 0
transit_gateway_attachment_id = local.vpc_information[count.index].transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.spokes_tgw_rt[try(local.vpc_information[count.index].routing_domain, "spokes")].id
}
# ---------------------- TRANSIT GATEWAY STATIC ROUTES ----------------------
# Static Route (0.0.0.0/0) from Spoke VPCs to Inspection VPC if:
# 1/ The Inspection VPC is created and no Egress VPC is created or,
# 2/ Both Inspection VPC and Egress VPC are created, and the traffic inspection is "all" or "north-south".
resource "aws_ec2_transit_gateway_route" "spokes_to_inspection_default_route" {
for_each = {
for k, v in aws_ec2_transit_gateway_route_table.spokes_tgw_rt : k => v.id
if local.spoke_to_inspection_default
}
destination_cidr_block = "0.0.0.0/0"
transit_gateway_attachment_id = module.central_vpcs["inspection"].transit_gateway_attachment_id
transit_gateway_route_table_id = each.value
}
# Static Route (0.0.0.0/0) from Spoke VPCs to Egress VPC if:
# 1/ The Egress VPC is created and no Inspection VPC is created or,
# 2/ Both Inspection VPC and Egress VPC are created, and the traffic inspection is "east-west".
resource "aws_ec2_transit_gateway_route" "spokes_to_egress_default_route" {
for_each = {
for k, v in aws_ec2_transit_gateway_route_table.spokes_tgw_rt : k => v.id
if local.spoke_to_egress_default
}
destination_cidr_block = "0.0.0.0/0"
transit_gateway_attachment_id = module.central_vpcs["egress"].transit_gateway_attachment_id
transit_gateway_route_table_id = each.value
}
# Static Route (Network's CIDR) from Spoke VPCs to Inspection VPC if:
# 1/ Both Inspection VPC and Egress VPC are created, and the traffic inspection is "east-west".
resource "aws_ec2_transit_gateway_route" "spokes_to_inspection_network_route" {
for_each = {
for k, v in aws_ec2_transit_gateway_route_table.spokes_tgw_rt : k => v.id
if local.spoke_to_inspection_network && !local.network_pl
}
destination_cidr_block = var.network_definition.value
transit_gateway_attachment_id = module.central_vpcs["inspection"].transit_gateway_attachment_id
transit_gateway_route_table_id = each.value
}
resource "aws_ec2_transit_gateway_prefix_list_reference" "spokes_to_inspection_network_prefix_list" {
for_each = {
for k, v in aws_ec2_transit_gateway_route_table.spokes_tgw_rt : k => v.id
if local.spoke_to_inspection_network && local.network_pl
}
prefix_list_id = var.network_definition.value
transit_gateway_attachment_id = module.central_vpcs["inspection"].transit_gateway_attachment_id
transit_gateway_route_table_id = each.value
}
# Static Route (0.0.0.0/0) from Inspection VPC to Egress VPC if:
# 1/ Both Inspection VPC and Egress VPC are created, and the traffic inspection is "all" or "north-south".
resource "aws_ec2_transit_gateway_route" "inspection_to_egress_default_route" {
count = (
local.inspection_and_egress_routes &&
try(local.associate_and_propagate_to_tgw["inspection"], true)
) ? 1 : 0
destination_cidr_block = "0.0.0.0/0"
transit_gateway_attachment_id = module.central_vpcs["egress"].transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.tgw_route_table["inspection"].id
}
# Static Route (Network's CIDR) from Egress VPC to Inspection VPC if:
# 1/ Both Inspection VPC and Egress VPC are created, and the traffic inspection is "all" or "north-south".
resource "aws_ec2_transit_gateway_route" "egress_to_inspection_network_route" {
count = (
local.inspection_and_egress_routes &&
!local.network_pl && try(local.associate_and_propagate_to_tgw["egress"], true)
) ? 1 : 0
destination_cidr_block = var.network_definition.value
transit_gateway_attachment_id = module.central_vpcs["inspection"].transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.tgw_route_table["egress"].id
}
resource "aws_ec2_transit_gateway_prefix_list_reference" "egress_to_inspection_network_prefix_list" {
count = (
local.inspection_and_egress_routes &&
local.network_pl && try(local.associate_and_propagate_to_tgw["egress"], true)
) ? 1 : 0
prefix_list_id = var.network_definition.value
transit_gateway_attachment_id = module.central_vpcs["inspection"].transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.tgw_route_table["egress"].id
}
# Static Route (Network's CIDR) from Ingress VPC to Inspection VPC if:
# 1/ Both Inspection VPC and Ingress VPC are created, and the traffic inspection is "all" or "north-south".
resource "aws_ec2_transit_gateway_route" "ingress_to_inspection_network_route" {
count = (
local.ingress_to_inspection_network &&
!local.network_pl && try(local.associate_and_propagate_to_tgw["ingress"], true)
) ? 1 : 0
destination_cidr_block = var.network_definition.value
transit_gateway_attachment_id = module.central_vpcs["inspection"].transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.tgw_route_table["ingress"].id
}
resource "aws_ec2_transit_gateway_prefix_list_reference" "ingress_to_inspection_network_prefix_list" {
count = (
local.ingress_to_inspection_network &&
local.network_pl && try(local.associate_and_propagate_to_tgw["ingress"], true)
) ? 1 : 0
prefix_list_id = var.network_definition.value
transit_gateway_attachment_id = module.central_vpcs["inspection"].transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.tgw_route_table["ingress"].id
}
# -------------------- TRANSIT GATEWAY PROPAGATED ROUTES --------------------
# Ingress VPC propagates to the Inspection VPC if both Ingress and Inspection are created, and traffic inspection is "all" or "north-south"
resource "aws_ec2_transit_gateway_route_table_propagation" "ingress_to_inspection_propagation" {
count = (
local.ingress_to_inspection_network &&
try(local.associate_and_propagate_to_tgw["inspection"], true)
) ? 1 : 0
transit_gateway_attachment_id = module.central_vpcs["ingress"].transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.tgw_route_table["inspection"].id
}
# Spoke VPCs propagation to the Inspection RT - anytime this VPC is created
resource "aws_ec2_transit_gateway_route_table_propagation" "spokes_to_inspection_propagation" {
count = (
local.spoke_to_inspection_propagation &&
try(local.associate_and_propagate_to_tgw["inspection"], true)
) ? local.number_vpcs : 0
transit_gateway_attachment_id = local.vpc_information[count.index].transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.tgw_route_table["inspection"].id
}
# Spoke VPCs propagation to the Egress RT if:
# 1/ The Egress VPC is created without Inspection VPC or,
# 2/ Both Egress and Inspection VPC are created, and the traffic inspeciton is "all" or "east-west"
resource "aws_ec2_transit_gateway_route_table_propagation" "spokes_to_egress_propagation" {
count = (
local.spoke_to_egress_propagation &&
try(local.associate_and_propagate_to_tgw["egress"], true)
) ? local.number_vpcs : 0
transit_gateway_attachment_id = local.vpc_information[count.index].transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.tgw_route_table["egress"].id
}
# Spoke VPCs propagation to the Ingress RT if:
# 1/ The Ingress VPC is created without Inspection VPC or,
# 2/ Both Egress and Inspection VPC are created, and the traffic inspeciton is "east-west"
resource "aws_ec2_transit_gateway_route_table_propagation" "spokes_to_ingress_propagation" {
count = (
local.spoke_to_ingress_propagation &&
try(local.associate_and_propagate_to_tgw["ingress"], true)
) ? local.number_vpcs : 0
transit_gateway_attachment_id = local.vpc_information[count.index].transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.tgw_route_table["ingress"].id
}
# Ingress VPC propagates to Spoke VPCs if:
# 1/ The Ingress VPC is created without Inspection VPC or,
# 2/ Both Egress and Inspection VPC are created, and the traffic inspection is "east-west"
resource "aws_ec2_transit_gateway_route_table_propagation" "ingress_to_spokes_propagation" {
for_each = {
for k, v in aws_ec2_transit_gateway_route_table.spokes_tgw_rt : k => v.id
if local.spoke_to_ingress_propagation
}
transit_gateway_attachment_id = module.central_vpcs["ingress"].transit_gateway_attachment_id
transit_gateway_route_table_id = each.value
}
# Spoke VPCs propagation to the Shared Services RT - anytime this VPC is created
resource "aws_ec2_transit_gateway_route_table_propagation" "spokes_to_shared_services_propagation" {
count = (
contains(keys(var.central_vpcs), "shared_services") &&
try(local.associate_and_propagate_to_tgw["shared_services"], true)
) ? local.number_vpcs : 0
transit_gateway_attachment_id = local.vpc_information[count.index].transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.tgw_route_table["shared_services"].id
}
# Spoke VPCs propagation to the Hybrid DNS RT - anytime this VPC is created
resource "aws_ec2_transit_gateway_route_table_propagation" "spokes_to_hybrid_dns_propagation" {
count = (
contains(keys(var.central_vpcs), "hybrid_dns") &&
try(local.associate_and_propagate_to_tgw["hybrid_dns"], true)
) ? local.number_vpcs : 0
transit_gateway_attachment_id = local.vpc_information[count.index].transit_gateway_attachment_id
transit_gateway_route_table_id = aws_ec2_transit_gateway_route_table.tgw_route_table["hybrid_dns"].id
}
# If Shared Services VPC is created, it propagates its CIDR to all the Segment TGW Route Tables
resource "aws_ec2_transit_gateway_route_table_propagation" "shared_services_to_spokes_propagation" {
for_each = {
for k, v in aws_ec2_transit_gateway_route_table.spokes_tgw_rt : k => v.id
if contains(keys(var.central_vpcs), "shared_services")
}
transit_gateway_attachment_id = module.central_vpcs["shared_services"].transit_gateway_attachment_id
transit_gateway_route_table_id = each.value
}
# If Hybrid DNS VPC is created, it propagates its CIDR to the Segment TGW Route Tables
resource "aws_ec2_transit_gateway_route_table_propagation" "hybrid_dns_to_spokes_propagation" {
for_each = {
for k, v in aws_ec2_transit_gateway_route_table.spokes_tgw_rt : k => v.id
if contains(keys(var.central_vpcs), "hybrid_dns")
}
transit_gateway_attachment_id = module.central_vpcs["hybrid_dns"].transit_gateway_attachment_id
transit_gateway_route_table_id = each.value
}
# ---------------------- AWS NETWORK FIREWALL ----------------------
module "aws_network_firewall" {
count = local.create_anfw ? 1 : 0
source = "aws-ia/networkfirewall/aws"
version = "= 1.0.0"
network_firewall_name = var.central_vpcs.inspection.aws_network_firewall.name
network_firewall_description = var.central_vpcs.inspection.aws_network_firewall.description
network_firewall_policy = var.central_vpcs.inspection.aws_network_firewall.policy_arn
network_firewall_policy_change_protection = try(var.central_vpcs.inspection.aws_network_firewall.network_firewall_policy_change_protection, false)
network_firewall_subnet_change_protection = try(var.central_vpcs.inspection.aws_network_firewall.network_firewall_subnet_change_protection, false)
vpc_id = module.central_vpcs["inspection"].vpc_attributes.id
vpc_subnets = { for k, v in module.central_vpcs["inspection"].private_subnet_attributes_by_az : split("/", k)[1] => v.id if split("/", k)[0] == "endpoints" }
number_azs = var.central_vpcs.inspection.az_count
routing_configuration = local.anfw_routing_configuration[local.inspection_configuration]
tags = merge(
module.tags.tags_aws,
try(var.central_vpcs.inspection.tags, {})
)
}
# We need to get the CIDR blocks from a provided managed prefix list if:
# 1/ Network Firewall is deployed and,
# 2/ The Inspection VPC has public subnets.
data "aws_ec2_managed_prefix_list" "data_network_prefix_list" {
count = local.network_pl ? 1 : 0
id = var.network_definition.value
}