-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
319 lines (269 loc) · 13 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
##################################
# Vnets and Subnets
##################################
# remove first layer of data structure
locals {
network = var.network.network
}
# data structure for vnets and subnets
locals {
network_settings = flatten([
local.network == null ? [] : [
for vnets, value in local.network : {
# vnet settings
name = vnets
address_space = value.address_space
bgp_community = value.bgp_community
ddos_protection_plan = value.ddos_protection_plan
dns_servers = value.dns_servers
edge_zone = value.edge_zone
flow_timeout_in_minutes = value.flow_timeout_in_minutes
# subnet settings
subnets = value.subnets
# private DNS zone links
link_these_private_dns_zones = value.link_these_private_dns_zones
dns_registration_enabled = value.dns_registration_enabled
}
]
])
}
# Resource documentation: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group
resource "azurerm_resource_group" "this" {
name = var.application_details.resource_group_name == null ? "rg-network-${var.application_details.name}-${var.application_details.environment}" : var.application_details.resource_group_name
location = var.application_details.location
tags = try(var.tags, null)
# tags are typically provided by policies on Subscriptions or ResourceGroups and will be overwritten by
# DeployIfNotExists policies this results in conflicts to each other
lifecycle {
ignore_changes = [tags]
}
}
module "network" {
for_each = { for vnet, vnet_config in local.network_settings : "${vnet_config.name}" => vnet_config }
source = "./modules/terraform-azurerm-network"
providers = {
azurerm.hub = azurerm.hub
}
# miscellaneous
resource_group_name = azurerm_resource_group.this.name
application_location = azurerm_resource_group.this.location
tags = try(var.tags, null)
network = {
# vnet configuration
name = "vnet-${each.key}-${var.application_details.name}-${var.application_details.environment}"
address_space = each.value.address_space
bgp_community = each.value.bgp_community
ddos_protection_plan = each.value.ddos_protection_plan
dns_servers = each.value.dns_servers
edge_zone = each.value.edge_zone
flow_timeout_in_minutes = each.value.flow_timeout_in_minutes
# subnet configuration
subnets = each.value.subnets
# private DNS zone links
link_these_private_dns_zones = each.value.link_these_private_dns_zones
dns_registration_enabled = each.value.dns_registration_enabled
}
# peering
hub_details = var.hub_details
vnet_peering_hub_to_spoke = var.vnet_peering_hub_to_spoke
vnet_peering_spoke_to_hub = var.vnet_peering_spoke_to_hub
}
##################################
# Route Tables
##################################
# remove first layer of data structure
locals {
route_tables = var.route_tables.route_tables
}
# Resource documentation: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/route_table
resource "azurerm_route_table" "this" {
for_each = try(local.route_tables, null) == null ? {} : local.route_tables
name = "rt-${each.key}-${var.application_details.name}-${var.application_details.environment}"
resource_group_name = azurerm_resource_group.this.name
location = azurerm_resource_group.this.location
disable_bgp_route_propagation = each.value.disable_bgp_route_propagation
tags = try(var.tags, null)
depends_on = [azurerm_resource_group.this]
lifecycle {
ignore_changes = [tags]
}
}
# data structure for route table associations
locals {
route_table_associations = flatten([
local.route_tables == null ? [] : [
for route_table_in_file, route_table_config_in_file in local.route_tables : [
route_table_config_in_file.subnet_associations == null ? [] : [
for associated_subnet in route_table_config_in_file.subnet_associations : [
for vnet, vnet_config in module.network : [
for subnet_container, subnet_container_content in vnet_config : [
for subnet, subnet_config in subnet_container_content : [
for route_table, route_table_config in azurerm_route_table.this : [
length(regexall(associated_subnet, subnet_config.name)) > 0 && length(regexall(route_table_in_file, route_table_config.name)) > 0 ? {
route_table_id = route_table_config.id
subnet_id = subnet_config.id
key = "${route_table}.${subnet}"
} : {}
]
]
]
]
]
]
]
]
])
filtered_route_table_associations = [
for association in local.route_table_associations : association if length(association) > 0
]
route_table_associations_as_map = {
for k, v in local.filtered_route_table_associations : v.key => v
}
}
# Resource documentation: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet_route_table_association
resource "azurerm_subnet_route_table_association" "this" {
for_each = local.route_table_associations_as_map
subnet_id = each.value.subnet_id
route_table_id = each.value.route_table_id
depends_on = [module.network, azurerm_route_table.this]
}
# data structure for routes
locals {
routes = flatten([
local.route_tables == null ? [] : [
for route_table, route_table_config in local.route_tables : [
route_table_config.routes == null ? [] : [
for route, route_config in route_table_config.routes : {
key = "${route}.${route_table}"
name = "udr-${route}"
route_table_name = "rt-${route_table}-${var.application_details.name}-${var.application_details.environment}"
address_prefix = route_config.address_prefix
next_hop_type = route_config.next_hop_type
next_hop_in_ip_address = try(route_config.next_hop_in_ip_address, null)
}
]
]
]
])
routes_as_map = { for k, v in local.routes : v.key => v }
}
# Resource documentation: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/route
resource "azurerm_route" "this" {
for_each = local.routes_as_map
name = each.value.name
resource_group_name = azurerm_resource_group.this.name
route_table_name = each.value.route_table_name
address_prefix = each.value.address_prefix
next_hop_type = each.value.next_hop_type
next_hop_in_ip_address = each.value.next_hop_in_ip_address
depends_on = [azurerm_route_table.this]
}
##################################
# Network Security Groups
##################################
# remove first layer of data structure
locals {
nsg = var.network_security_groups.network_security_groups
}
# Resource documentation: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_group
resource "azurerm_network_security_group" "this" {
for_each = try(local.nsg, null) == null ? {} : local.nsg
name = "nsg-${each.key}-${var.application_details.name}-${var.application_details.environment}"
location = azurerm_resource_group.this.location
resource_group_name = azurerm_resource_group.this.name
tags = try(var.tags, null)
lifecycle {
ignore_changes = [tags]
}
}
# data structure for network security group association
locals {
nsg_associations = flatten([
local.nsg == null ? [] : [
for nsg_in_file, nsg_config_in_file in local.nsg : [
nsg_config_in_file.subnet_associations == null ? [] : [
for associated_subnet in nsg_config_in_file.subnet_associations : [
for vnet, vnet_config in module.network : [
for subnet_container, subnet_container_content in vnet_config : [
for subnet, subnet_config in subnet_container_content : [
for nsg, nsg_config in azurerm_network_security_group.this : [
length(regexall(associated_subnet, subnet_config.name)) > 0 && length(regexall(nsg_in_file, nsg_config.name)) > 0 ? {
nsg_id = nsg_config.id
subnet_id = subnet_config.id
key = "${nsg}.${subnet}"
} : {}
]
]
]
]
]
]
]
]
])
filtered_nsg_associations = [
for association in local.nsg_associations : association if length(association) > 0
]
nsg_associations_as_map = {
for k, v in local.filtered_nsg_associations : v.key => v
}
}
# Resource Documentation: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/subnet_network_security_group_association
resource "azurerm_subnet_network_security_group_association" "this" {
for_each = local.nsg_associations_as_map
subnet_id = each.value.subnet_id
network_security_group_id = each.value.nsg_id
}
# data structure for nsg rules
locals {
rules = flatten([
local.nsg == null ? [] : [
for nsg, nsg_config in local.nsg : [
nsg_config.security_rules == null ? [] : [
for rule, rule_config in nsg_config.security_rules : {
key = "${rule}.${nsg}"
name = "nsgr-${rule}-${var.application_details.name}-${var.application_details.environment}"
protocol = rule_config.protocol
source_port_range = try(rule_config.source_port_range, null)
source_port_ranges = try(rule_config.source_port_ranges, null)
destination_port_range = try(rule_config.destination_port_range, null)
destination_port_ranges = try(rule_config.destination_port_ranges, null)
source_address_prefix = try(rule_config.source_address_prefix, null)
source_address_prefixes = try(rule_config.source_address_prefixes, null)
destination_address_prefix = try(rule_config.destination_address_prefix, null)
destination_address_prefixes = try(rule_config.destination_address_prefixes, null)
destination_application_security_group_ids = try(rule_config.destination_application_security_group_ids, null)
source_application_security_group_ids = try(rule_config.source_application_security_group_ids, null)
access = rule_config.access
priority = rule_config.priority
direction = rule_config.direction
network_security_group_name = "nsg-${nsg}-${var.application_details.name}-${var.application_details.environment}"
}
]
]
]
])
rules_as_map = { for k, v in local.rules : v.key => v }
}
# Resource documentation: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/network_security_rule
resource "azurerm_network_security_rule" "this" {
for_each = local.rules_as_map
name = each.value.name
network_security_group_name = each.value.network_security_group_name
resource_group_name = azurerm_resource_group.this.name
protocol = each.value.protocol
source_port_range = try(each.value.source_port_range, null)
source_port_ranges = try(each.value.source_port_ranges, null)
destination_port_range = try(each.value.destination_port_range, null)
destination_port_ranges = try(each.value.destination_port_ranges, null)
source_address_prefix = try(each.value.source_address_prefix, null)
source_address_prefixes = try(each.value.source_address_prefixes, null)
destination_address_prefix = try(each.value.destination_address_prefix, null)
destination_address_prefixes = try(each.value.destination_address_prefixes, null)
source_application_security_group_ids = try(each.value.source_application_security_group_ids, null)
destination_application_security_group_ids = try(each.value.destination_application_security_group_ids, null)
access = each.value.access
priority = each.value.priority
direction = each.value.direction
depends_on = [azurerm_network_security_group.this]
}