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 2
/
main.tf
277 lines (248 loc) · 11.8 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
#---------------------------------------------------------
# Resource Group Creation or selection - Default is "true"
#----------------------------------------------------------
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 ? [{}] : []
}
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 "false"
#--------------------------------------------
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" "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
}
#----------------------------------------
# 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
}
#-----------------------------------------------
# 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
}
}
}
}