This repository has been archived by the owner on Jun 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
472 lines (419 loc) · 18.7 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
#---------------------------------
# Local declarations
#---------------------------------
locals {
resource_group_name = element(coalescelist(data.azurerm_resource_group.rgrp.*.name, azurerm_resource_group.rg.*.name, [""]), 0)
location = element(coalescelist(data.azurerm_resource_group.rgrp.*.location, azurerm_resource_group.rg.*.location, [""]), 0)
if_ddos_enabled = var.create_ddos_plan ? [{}] : []
public_ip_map = { for pip in var.public_ip_names : pip => true }
fw_nat_rules = { for idx, rule in var.firewall_nat_rules : rule.name => {
idx : idx,
rule : rule,
}
}
fw_network_rules = { for idx, rule in var.firewall_network_rules : rule.name => {
idx : idx,
rule : rule,
}
}
fw_application_rules = { for idx, rule in var.firewall_application_rules : rule.name => {
idx : idx,
rule : rule,
}
}
}
#---------------------------------------------------------
# Resource Group Creation or selection - Default is "true"
#----------------------------------------------------------
data "azurerm_resource_group" "rgrp" {
count = var.create_resource_group == false ? 1 : 0
name = var.resource_group_name
}
resource "azurerm_resource_group" "rg" {
count = var.create_resource_group ? 1 : 0
name = lower(var.resource_group_name)
location = var.location
tags = merge({ "ResourceName" = format("%s", var.resource_group_name) }, var.tags, )
}
#-------------------------------------
# VNET Creation - Default is "true"
#-------------------------------------
resource "azurerm_virtual_network" "vnet" {
name = lower("vnet-${var.hub_vnet_name}-${local.location}")
location = local.location
resource_group_name = local.resource_group_name
address_space = var.vnet_address_space
dns_servers = var.dns_servers
tags = merge({ "ResourceName" = lower("vnet-${var.hub_vnet_name}-${local.location}") }, var.tags, )
dynamic "ddos_protection_plan" {
for_each = local.if_ddos_enabled
content {
id = azurerm_network_ddos_protection_plan.ddos[0].id
enable = true
}
}
}
#--------------------------------------------
# Ddos protection plan - Default is "true"
#--------------------------------------------
resource "azurerm_network_ddos_protection_plan" "ddos" {
count = var.create_ddos_plan ? 1 : 0
name = lower("${var.hub_vnet_name}-ddos-protection-plan")
resource_group_name = local.resource_group_name
location = local.location
tags = merge({ "ResourceName" = lower("${var.hub_vnet_name}-ddos-protection-plan") }, var.tags, )
}
#-------------------------------------
# Network Watcher - Default is "true"
#-------------------------------------
resource "azurerm_resource_group" "nwatcher" {
count = var.create_network_watcher != false ? 1 : 0
name = "NetworkWatcherRG"
location = local.location
tags = merge({ "ResourceName" = "NetworkWatcherRG" }, var.tags, )
}
resource "azurerm_network_watcher" "nwatcher" {
count = var.create_network_watcher != false ? 1 : 0
name = "NetworkWatcher_${local.location}"
location = local.location
resource_group_name = azurerm_resource_group.nwatcher.0.name
tags = merge({ "ResourceName" = format("%s", "NetworkWatcher_${local.location}") }, var.tags, )
}
#--------------------------------------------------------------------------------------------------------
# Subnets Creation with, private link endpoint/servie network policies, service endpoints and Deligation.
#--------------------------------------------------------------------------------------------------------
resource "azurerm_subnet" "fw-snet" {
name = "AzureFirewallSubnet"
resource_group_name = local.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = var.firewall_subnet_address_prefix #[cidrsubnet(element(var.vnet_address_space, 0), 10, 0)]
service_endpoints = var.firewall_service_endpoints
}
resource "azurerm_subnet" "gw_snet" {
count = var.gateway_subnet_address_prefix != null ? 1 : 0
name = "GatewaySubnet"
resource_group_name = local.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = var.gateway_subnet_address_prefix #[cidrsubnet(element(var.vnet_address_space, 0), 8, 1)]
service_endpoints = ["Microsoft.Storage"]
}
resource "azurerm_subnet" "snet" {
for_each = var.subnets
name = lower(format("snet-%s-${var.hub_vnet_name}-${local.location}", each.value.subnet_name))
resource_group_name = local.resource_group_name
virtual_network_name = azurerm_virtual_network.vnet.name
address_prefixes = each.value.subnet_address_prefix
service_endpoints = lookup(each.value, "service_endpoints", [])
# Applicable to the subnets which used for Private link endpoints or services
enforce_private_link_endpoint_network_policies = lookup(each.value, "enforce_private_link_endpoint_network_policies", null)
enforce_private_link_service_network_policies = lookup(each.value, "enforce_private_link_service_network_policies", null)
dynamic "delegation" {
for_each = lookup(each.value, "delegation", {}) != {} ? [1] : []
content {
name = lookup(each.value.delegation, "name", null)
service_delegation {
name = lookup(each.value.delegation.service_delegation, "name", null)
actions = lookup(each.value.delegation.service_delegation, "actions", null)
}
}
}
}
#---------------------------------------------------------------
# Network security group - NSG created for every subnet in VNet
#---------------------------------------------------------------
resource "azurerm_network_security_group" "nsg" {
for_each = var.subnets
name = lower("nsg_${each.key}_in")
resource_group_name = local.resource_group_name
location = local.location
tags = merge({ "ResourceName" = lower("nsg_${each.key}_in") }, var.tags, )
dynamic "security_rule" {
for_each = concat(lookup(each.value, "nsg_inbound_rules", []), lookup(each.value, "nsg_outbound_rules", []))
content {
name = security_rule.value[0] == "" ? "Default_Rule" : security_rule.value[0]
priority = security_rule.value[1]
direction = security_rule.value[2] == "" ? "Inbound" : security_rule.value[2]
access = security_rule.value[3] == "" ? "Allow" : security_rule.value[3]
protocol = security_rule.value[4] == "" ? "Tcp" : security_rule.value[4]
source_port_range = "*"
destination_port_range = security_rule.value[5] == "" ? "*" : security_rule.value[5]
source_address_prefix = security_rule.value[6] == "" ? element(each.value.subnet_address_prefix, 0) : security_rule.value[6]
destination_address_prefix = security_rule.value[7] == "" ? element(each.value.subnet_address_prefix, 0) : security_rule.value[7]
description = "${security_rule.value[2]}_Port_${security_rule.value[5]}"
}
}
}
resource "azurerm_subnet_network_security_group_association" "nsg-assoc" {
for_each = var.subnets
subnet_id = azurerm_subnet.snet[each.key].id
network_security_group_id = azurerm_network_security_group.nsg[each.key].id
}
#-------------------------------------------------
# route_table to dirvert traffic through Firewall
#-------------------------------------------------
resource "azurerm_route_table" "rtout" {
name = "route-network-outbound"
resource_group_name = local.resource_group_name
location = local.location
tags = merge({ "ResourceName" = "route-network-outbound" }, var.tags, )
}
resource "azurerm_subnet_route_table_association" "rtassoc" {
for_each = var.subnets
subnet_id = azurerm_subnet.snet[each.key].id
route_table_id = azurerm_route_table.rtout.id
}
resource "azurerm_route" "rt" {
name = lower("route-to-firewall-${var.hub_vnet_name}-${local.location}")
resource_group_name = var.resource_group_name
route_table_name = azurerm_route_table.rtout.name
address_prefix = "0.0.0.0/0"
next_hop_type = "VirtualAppliance"
next_hop_in_ip_address = azurerm_firewall.fw.ip_configuration.0.private_ip_address
}
#----------------------------------------
# Private DNS Zone - Default is "true"
#----------------------------------------
resource "azurerm_private_dns_zone" "dz" {
count = var.private_dns_zone_name != null ? 1 : 0
name = var.private_dns_zone_name
resource_group_name = local.resource_group_name
tags = merge({ "ResourceName" = format("%s", lower(var.private_dns_zone_name)) }, var.tags, )
}
resource "azurerm_private_dns_zone_virtual_network_link" "dzvlink" {
count = var.private_dns_zone_name != null ? 1 : 0
name = lower("${var.private_dns_zone_name}-link")
resource_group_name = local.resource_group_name
virtual_network_id = azurerm_virtual_network.vnet.id
private_dns_zone_name = azurerm_private_dns_zone.dz[0].name
tags = merge({ "ResourceName" = format("%s", lower("${var.private_dns_zone_name}-link")) }, var.tags, )
}
#----------------------------------------------------------------
# Azure Role Assignment for Service Principal - current user
#-----------------------------------------------------------------
data "azurerm_client_config" "current" {}
resource "azurerm_role_assignment" "peering" {
scope = azurerm_virtual_network.vnet.id
role_definition_name = "Network Contributor"
principal_id = data.azurerm_client_config.current.object_id
}
resource "azurerm_role_assignment" "dns" {
scope = azurerm_private_dns_zone.dz[0].id
role_definition_name = "Private DNS Zone Contributor"
principal_id = data.azurerm_client_config.current.object_id
}
#------------------------------------------
# Public IP resources for Azure Firewall
#------------------------------------------
resource "random_string" "str" {
for_each = local.public_ip_map
length = 6
special = false
upper = false
keepers = {
domain_name_label = each.key
}
}
resource "azurerm_public_ip_prefix" "pip_prefix" {
name = lower("${var.hub_vnet_name}-pip-prefix")
location = local.location
resource_group_name = local.resource_group_name
prefix_length = 30
tags = merge({ "ResourceName" = lower("${var.hub_vnet_name}-pip-prefix") }, var.tags, )
}
resource "azurerm_public_ip" "fw-pip" {
for_each = local.public_ip_map
name = lower("pip-${var.hub_vnet_name}-${each.key}-${local.location}")
location = local.location
resource_group_name = local.resource_group_name
allocation_method = "Static"
sku = "Standard"
public_ip_prefix_id = azurerm_public_ip_prefix.pip_prefix.id
domain_name_label = format("%s%s", lower(replace(each.key, "/[[:^alnum:]]/", "")), random_string.str[each.key].result)
tags = merge({ "ResourceName" = lower("pip-${var.hub_vnet_name}-${each.key}-${local.location}") }, var.tags, )
}
#-----------------
# Azure Firewall
#-----------------
resource "azurerm_firewall" "fw" {
name = lower("fw-${var.hub_vnet_name}-${local.location}")
location = local.location
resource_group_name = local.resource_group_name
zones = var.firewall_zones
tags = merge({ "ResourceName" = lower("fw-${var.hub_vnet_name}-${local.location}") }, var.tags, )
dynamic "ip_configuration" {
for_each = local.public_ip_map
iterator = ip
content {
name = ip.key
subnet_id = ip.key == var.public_ip_names[0] ? azurerm_subnet.fw-snet.id : null
public_ip_address_id = azurerm_public_ip.fw-pip[ip.key].id
}
}
}
#----------------------------------------------
# Azure Firewall Network/Application/NAT Rules
#----------------------------------------------
resource "azurerm_firewall_application_rule_collection" "fw_app" {
for_each = local.fw_application_rules
name = lower(format("fw-app-rule-%s-${var.hub_vnet_name}-${local.location}", each.key))
azure_firewall_name = azurerm_firewall.fw.name
resource_group_name = local.resource_group_name
priority = 100 * (each.value.idx + 1)
action = each.value.rule.action
rule {
name = each.key
source_addresses = each.value.rule.source_addresses
target_fqdns = each.value.rule.target_fqdns
protocol {
type = each.value.rule.protocol.type
port = each.value.rule.protocol.port
}
}
}
resource "azurerm_firewall_network_rule_collection" "fw" {
for_each = local.fw_network_rules
name = lower(format("fw-net-rule-%s-${var.hub_vnet_name}-${local.location}", each.key))
azure_firewall_name = azurerm_firewall.fw.name
resource_group_name = local.resource_group_name
priority = 100 * (each.value.idx + 1)
action = each.value.rule.action
rule {
name = each.key
source_addresses = each.value.rule.source_addresses
destination_ports = each.value.rule.destination_ports
destination_addresses = [for dest in each.value.rule.destination_addresses : contains(var.public_ip_names, dest) ? azurerm_public_ip.fw-pip[dest].ip_address : dest]
protocols = each.value.rule.protocols
}
}
resource "azurerm_firewall_nat_rule_collection" "fw" {
for_each = local.fw_nat_rules
name = lower(format("fw-nat-rule-%s-${var.hub_vnet_name}-${local.location}", each.key))
azure_firewall_name = azurerm_firewall.fw.name
resource_group_name = local.resource_group_name
priority = 100 * (each.value.idx + 1)
action = each.value.rule.action
rule {
name = each.key
source_addresses = each.value.rule.source_addresses
destination_ports = each.value.rule.destination_ports
destination_addresses = [for dest in each.value.rule.destination_addresses : contains(var.public_ip_names, dest) ? azurerm_public_ip.fw-pip[dest].ip_address : dest]
protocols = each.value.rule.protocols
translated_address = each.value.rule.translated_address
translated_port = each.value.rule.translated_port
}
}
#-----------------------------------------------
# Storage Account for Logs Archive
#-----------------------------------------------
resource "azurerm_storage_account" "storeacc" {
name = format("stdiaglogs%s", lower(replace(var.hub_vnet_name, "/[[:^alnum:]]/", "")))
resource_group_name = local.resource_group_name
location = local.location
account_kind = "StorageV2"
account_tier = "Standard"
account_replication_type = "GRS"
enable_https_traffic_only = true
tags = merge({ "ResourceName" = format("stdiaglogs%s", lower(replace(var.hub_vnet_name, "/[[:^alnum:]]/", ""))) }, var.tags, )
}
#-----------------------------------------------
# Log analytics workspace for Logs analysis
#-----------------------------------------------
resource "random_string" "main" {
length = 8
special = false
keepers = {
name = var.hub_vnet_name
}
}
resource "azurerm_log_analytics_workspace" "logws" {
name = lower("logaws-${random_string.main.result}-${var.hub_vnet_name}-${local.location}")
resource_group_name = local.resource_group_name
location = local.location
sku = var.log_analytics_workspace_sku
retention_in_days = var.log_analytics_logs_retention_in_days
tags = merge({ "ResourceName" = lower("logaws-${random_string.main.result}-${var.hub_vnet_name}-${local.location}") }, var.tags, )
}
#-----------------------------------------
# Network flow logs for subnet and NSG
#-----------------------------------------
resource "azurerm_network_watcher_flow_log" "nwflog" {
for_each = var.subnets
network_watcher_name = azurerm_network_watcher.nwatcher[0].name
resource_group_name = azurerm_resource_group.nwatcher[0].name # Must provide Netwatcher resource Group
network_security_group_id = azurerm_network_security_group.nsg[each.key].id
storage_account_id = azurerm_storage_account.storeacc.id
enabled = true
version = 2
retention_policy {
enabled = true
days = 0
}
traffic_analytics {
enabled = true
workspace_id = azurerm_log_analytics_workspace.logws.workspace_id
workspace_region = local.location
workspace_resource_id = azurerm_log_analytics_workspace.logws.id
interval_in_minutes = 10
}
}
#---------------------------------------------------------------
# azurerm monitoring diagnostics - VNet, NSG, PIP, and Firewall
#---------------------------------------------------------------
resource "azurerm_monitor_diagnostic_setting" "vnet" {
name = lower("vnet-${var.hub_vnet_name}-diag")
target_resource_id = azurerm_virtual_network.vnet.id
storage_account_id = azurerm_storage_account.storeacc.id
log_analytics_workspace_id = azurerm_log_analytics_workspace.logws.id
log {
category = "VMProtectionAlerts"
enabled = true
retention_policy {
enabled = false
}
}
metric {
category = "AllMetrics"
retention_policy {
enabled = false
}
}
}
resource "azurerm_monitor_diagnostic_setting" "nsg" {
for_each = var.subnets
name = lower("${each.key}-diag")
target_resource_id = azurerm_network_security_group.nsg[each.key].id
storage_account_id = azurerm_storage_account.storeacc.id
log_analytics_workspace_id = azurerm_log_analytics_workspace.logws.id
dynamic "log" {
for_each = var.nsg_diag_logs
content {
category = log.value
enabled = true
retention_policy {
enabled = false
}
}
}
}
resource "azurerm_monitor_diagnostic_setting" "fw-diag" {
name = lower("fw-${var.hub_vnet_name}-diag")
target_resource_id = azurerm_firewall.fw.id
storage_account_id = azurerm_storage_account.storeacc.id
log_analytics_workspace_id = azurerm_log_analytics_workspace.logws.id
dynamic "log" {
for_each = var.fw_diag_logs
content {
category = log.value
enabled = true
retention_policy {
enabled = false
}
}
}
metric {
category = "AllMetrics"
retention_policy {
enabled = false
}
}
}