-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
279 lines (244 loc) · 10.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
278
279
# existing vnet data source
data "azurerm_virtual_network" "existing" {
for_each = lookup(var.vnet, "existing", null) != null ? { "vnet" = var.vnet.existing } : {}
name = each.value.name
resource_group_name = try(each.value.resource_group, var.resource_group)
}
# virtual network
resource "azurerm_virtual_network" "vnet" {
for_each = lookup(var.vnet, "existing", null) != null ? {} : { "vnet" = var.vnet }
resource_group_name = coalesce(var.vnet.resource_group, var.resource_group)
location = coalesce(var.vnet.location, var.location)
name = var.vnet.name
address_space = var.vnet.cidr
edge_zone = try(var.vnet.edge_zone, null)
bgp_community = try(var.vnet.bgp_community, null)
flow_timeout_in_minutes = try(var.vnet.flow_timeout_in_minutes, null)
dynamic "encryption" {
for_each = lookup(var.vnet, "encryption", null) != null ? [lookup(var.vnet, "encryption", null)] : []
content {
enforcement = encryption.value.enforcement
}
}
tags = try(var.vnet.tags, var.tags, {})
lifecycle {
ignore_changes = [subnet, dns_servers]
}
}
# dns servers
resource "azurerm_virtual_network_dns_servers" "dns" {
for_each = {
for k, v in {
"default" = lookup(var.vnet, "existing", null) != null ? try(var.vnet.existing.dns_servers, []) : try(var.vnet.dns_servers, [])
} : k => v
if length(v) > 0
}
virtual_network_id = lookup(var.vnet, "existing", null) != null ? data.azurerm_virtual_network.existing["vnet"].id : azurerm_virtual_network.vnet["vnet"].id
dns_servers = each.value
}
# subnets
resource "azurerm_subnet" "subnets" {
for_each = lookup(lookup(var.vnet, "existing", {}), "subnets", lookup(var.vnet, "subnets", {}))
name = try(
each.value.name, join("-", [var.naming.subnet, each.key])
)
resource_group_name = lookup(var.vnet, "existing", null) != null ? var.vnet.existing.resource_group : coalesce(
var.vnet.resource_group,
var.resource_group
)
virtual_network_name = lookup(var.vnet, "existing", null) != null ? data.azurerm_virtual_network.existing["vnet"].name : azurerm_virtual_network.vnet["vnet"].name
address_prefixes = each.value.cidr
service_endpoints = try(each.value.service_endpoints, [])
private_link_service_network_policies_enabled = try(each.value.private_link_service_network_policies_enabled, false)
private_endpoint_network_policies = try(each.value.private_endpoint_network_policies, "Disabled")
service_endpoint_policy_ids = try(each.value.service_endpoint_policy_ids, [])
default_outbound_access_enabled = try(each.value.default_outbound_access_enabled, null)
dynamic "delegation" {
for_each = lookup(each.value, "delegations", {})
content {
name = delegation.key
service_delegation {
name = delegation.value.name
actions = lookup(delegation.value, "actions", [])
}
}
}
}
# network security groups
resource "azurerm_network_security_group" "this" {
for_each = merge(
lookup(var.vnet, "existing", null) != null ? lookup(lookup(var.vnet, "existing", {}), "network_security_groups", {}) : lookup(var.vnet, "network_security_groups", {}),
lookup(var.vnet, "existing", null) != null ? {
for subnet_key, subnet in lookup(lookup(var.vnet, "existing", {}), "subnets", {}) : subnet_key => subnet.network_security_group
if lookup(subnet, "network_security_group", null) != null
} : {
for subnet_key, subnet in lookup(var.vnet, "subnets", {}) : subnet_key => subnet.network_security_group
if lookup(subnet, "network_security_group", null) != null
}
)
name = try(
each.value.name,
"${var.naming.network_security_group}-${each.key}"
)
resource_group_name = lookup(var.vnet, "existing", null) != null ? var.vnet.existing.resource_group : coalesce(
var.vnet.resource_group,
var.resource_group
)
location = lookup(var.vnet, "existing", null) != null ? var.vnet.existing.location : coalesce(
var.vnet.location,
var.location
)
tags = try(var.vnet.tags, var.tags, {})
}
# security rules
resource "azurerm_network_security_rule" "this" {
for_each = merge({
for pair in flatten([
for nsg_key, nsg in lookup(lookup(var.vnet, "existing", {}), "network_security_groups", lookup(var.vnet, "network_security_groups", {})) :
try([
for rule_key, rule in lookup(nsg, "rules", {}) : {
key = "${nsg_key}_${rule_key}"
value = {
nsg_name = azurerm_network_security_group.this[nsg_key].name
rule = rule
rule_name = try(
rule.name, join("-", [var.naming.network_security_group_rule, rule_key])
)
}
}
], [])
]) : pair.key => pair.value
}, {
for pair in flatten([
for subnet_key, subnet in lookup(lookup(var.vnet, "existing", {}), "subnets", lookup(var.vnet, "subnets", {})) :
try([
for rule_key, rule in lookup(lookup(subnet, "network_security_group", {}), "rules", {}) : {
key = "${subnet_key}_${rule_key}"
value = {
nsg_name = azurerm_network_security_group.this[subnet_key].name
rule = rule
rule_name = try(
rule.name, join("-", [var.naming.network_security_group_rule, rule_key])
)
}
}
], [])
]) : pair.key => pair.value
})
name = each.value.rule_name
priority = each.value.rule.priority
direction = each.value.rule.direction
access = each.value.rule.access
protocol = each.value.rule.protocol
source_port_range = try(each.value.rule.source_port_range, null)
source_port_ranges = try(each.value.rule.source_port_ranges, null)
destination_port_range = try(each.value.rule.destination_port_range, null)
destination_port_ranges = try(each.value.rule.destination_port_ranges, null)
source_address_prefix = try(each.value.rule.source_address_prefix, null)
source_address_prefixes = try(each.value.rule.source_address_prefixes, null)
destination_address_prefix = try(each.value.rule.destination_address_prefix, null)
destination_address_prefixes = try(each.value.rule.destination_address_prefixes, null)
description = try(each.value.rule.description, null)
network_security_group_name = each.value.nsg_name
resource_group_name = lookup(var.vnet, "existing", null) != null ? var.vnet.existing.resource_group : coalesce(
var.vnet.resource_group,
var.resource_group
)
}
# nsg associations
resource "azurerm_subnet_network_security_group_association" "this" {
for_each = {
for subnet_key, subnet in lookup(lookup(var.vnet, "existing", {}), "subnets", lookup(var.vnet, "subnets", {})) : subnet_key => subnet
if lookup(lookup(subnet, "shared", {}), "network_security_group", null) != null || lookup(subnet, "network_security_group", null) != null
}
subnet_id = azurerm_subnet.subnets[each.key].id
network_security_group_id = lookup(lookup(each.value, "shared", {}), "network_security_group", null) != null ? (
azurerm_network_security_group.this[lookup(each.value.shared, "network_security_group")].id
) : azurerm_network_security_group.this[each.key].id
}
# route tables
resource "azurerm_route_table" "this" {
for_each = merge(
lookup(var.vnet, "existing", null) != null ? lookup(lookup(var.vnet, "existing", {}), "route_tables", {}) : lookup(var.vnet, "route_tables", {}),
lookup(var.vnet, "existing", null) != null ? {
for subnet_key, subnet in lookup(lookup(var.vnet, "existing", {}), "subnets", {}) : subnet_key => subnet.route_table
if lookup(subnet, "route_table", null) != null
} : {
for subnet_key, subnet in lookup(var.vnet, "subnets", {}) : subnet_key => subnet.route_table
if lookup(subnet, "route_table", null) != null
}
)
name = try(
each.value.name,
"${var.naming.route_table}-${each.key}"
)
resource_group_name = lookup(var.vnet, "existing", null) != null ? var.vnet.existing.resource_group : coalesce(
var.vnet.resource_group,
var.resource_group
)
location = lookup(var.vnet, "existing", null) != null ? var.vnet.existing.location : coalesce(
var.vnet.location,
var.location
)
bgp_route_propagation_enabled = try(each.value.bgp_route_propagation_enabled, true)
tags = try(var.vnet.tags, var.tags, {})
lifecycle {
ignore_changes = [route]
}
}
# routes
resource "azurerm_route" "this" {
for_each = merge({
for pair in flatten([
for rt_key, rt in lookup(lookup(var.vnet, "existing", {}), "route_tables", lookup(var.vnet, "route_tables", {})) :
try([
for route_key, route in lookup(rt, "routes", {}) : {
key = "${rt_key}_${route_key}"
value = {
route_table_name = azurerm_route_table.this[rt_key].name
route = route
route_name = try(
route.name, join("-", [var.naming.route, route_key])
)
}
}
], [])
]) : pair.key => pair.value
}, {
for pair in flatten([
for subnet_key, subnet in lookup(lookup(var.vnet, "existing", {}), "subnets", lookup(var.vnet, "subnets", {})) :
try([
for route_key, route in lookup(lookup(subnet, "route_table", {}), "routes", {}) : {
key = "${subnet_key}_${route_key}"
value = {
route_table_name = azurerm_route_table.this[subnet_key].name
route = route
route_name = try(
route.name, join("-", [var.naming.route, route_key])
)
}
}
], [])
]) : pair.key => pair.value
})
name = each.value.route_name
route_table_name = each.value.route_table_name
address_prefix = each.value.route.address_prefix
next_hop_type = each.value.route.next_hop_type
next_hop_in_ip_address = try(each.value.route.next_hop_in_ip_address, null)
resource_group_name = lookup(var.vnet, "existing", null) != null ? var.vnet.existing.resource_group : coalesce(
var.vnet.resource_group,
var.resource_group
)
}
# route table associations
resource "azurerm_subnet_route_table_association" "this" {
for_each = {
for subnet_key, subnet in lookup(lookup(var.vnet, "existing", {}), "subnets", lookup(var.vnet, "subnets", {})) : subnet_key => subnet
if lookup(lookup(subnet, "shared", {}), "route_table", null) != null || lookup(subnet, "route_table", null) != null
}
subnet_id = azurerm_subnet.subnets[each.key].id
route_table_id = lookup(lookup(each.value, "shared", {}), "route_table", null) != null ? (
azurerm_route_table.this[lookup(each.value.shared, "route_table")].id
) : azurerm_route_table.this[each.key].id
}