-
Notifications
You must be signed in to change notification settings - Fork 51
/
main.tf
375 lines (323 loc) · 12.6 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
# Main
# Azure Provider
provider "azurerm" {
features {}
}
# Create a random id
resource "random_id" "buildSuffix" {
byte_length = 2
}
############################ Locals ############################
locals {
vnets = {
hub = {
location = var.location
addressSpace = ["10.255.0.0/16"]
subnetPrefixes = ["10.255.1.0/24", "10.255.10.0/24", "10.255.20.0/24", "10.255.255.0/24"]
subnetNames = ["mgmt", "external", "internal", "RouteServerSubnet"]
}
spoke1 = {
location = var.location
addressSpace = ["10.1.0.0/16"]
subnetPrefixes = ["10.1.1.0/24", "10.1.10.0/24", "10.1.20.0/24"]
subnetNames = ["mgmt", "external", "internal"]
}
spoke2 = {
location = var.location
addressSpace = ["10.2.0.0/16"]
subnetPrefixes = ["10.2.1.0/24", "10.2.10.0/24", "10.2.20.0/24"]
subnetNames = ["mgmt", "external", "internal"]
}
}
spokeHubPeerings = {
spoke1 = {
}
spoke2 = {
}
}
}
############################ Resource Groups ############################
# Create Resource Groups
resource "azurerm_resource_group" "rg" {
for_each = local.vnets
name = format("%s-rg-%s-%s", var.projectPrefix, each.key, random_id.buildSuffix.hex)
location = each.value["location"]
tags = {
Name = format("%s-rg-%s-%s", var.resourceOwner, each.key, random_id.buildSuffix.hex)
Terraform = "true"
}
}
############################ Route Tables ############################
# Create Route Tables
resource "azurerm_route_table" "rt" {
for_each = local.vnets
name = format("%s-rt-%s-%s", var.projectPrefix, each.key, random_id.buildSuffix.hex)
location = azurerm_resource_group.rg[each.key].location
resource_group_name = azurerm_resource_group.rg[each.key].name
disable_bgp_route_propagation = false
tags = {
Name = format("%s-rt-%s-%s", var.resourceOwner, each.key, random_id.buildSuffix.hex)
Terraform = "true"
}
}
############################ Network Security Groups ############################
# Create Mgmt NSG
module "nsg-mgmt" {
for_each = local.vnets
source = "Azure/network-security-group/azurerm"
resource_group_name = azurerm_resource_group.rg[each.key].name
location = azurerm_resource_group.rg[each.key].location
security_group_name = format("%s-nsg-mgmt-%s-%s", var.projectPrefix, each.key, random_id.buildSuffix.hex)
source_address_prefix = [var.adminSrcAddr]
custom_rules = [
{
name = "allow_http"
priority = "100"
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
destination_port_range = "80"
},
{
name = "allow_https"
priority = "110"
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
destination_port_range = "443"
},
{
name = "allow_ssh"
priority = "120"
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
destination_port_range = "22"
}
]
tags = {
Name = format("%s-nsg-mgmt-%s-%s", var.resourceOwner, each.key, random_id.buildSuffix.hex)
Terraform = "true"
}
}
# Create External NSG
module "nsg-external" {
for_each = local.vnets
source = "Azure/network-security-group/azurerm"
resource_group_name = azurerm_resource_group.rg[each.key].name
location = azurerm_resource_group.rg[each.key].location
security_group_name = format("%s-nsg-external-%s-%s", var.projectPrefix, each.key, random_id.buildSuffix.hex)
source_address_prefix = ["*"]
custom_rules = [
{
name = "allow_http"
priority = "100"
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
destination_port_range = "80"
},
{
name = "allow_https"
priority = "110"
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
destination_port_range = "443"
}
]
tags = {
Name = format("%s-nsg-external-%s-%s", var.resourceOwner, each.key, random_id.buildSuffix.hex)
Terraform = "true"
}
}
# Create Internal NSG
module "nsg-internal" {
for_each = local.vnets
source = "Azure/network-security-group/azurerm"
resource_group_name = azurerm_resource_group.rg[each.key].name
location = azurerm_resource_group.rg[each.key].location
security_group_name = format("%s-nsg-internal-%s-%s", var.projectPrefix, each.key, random_id.buildSuffix.hex)
tags = {
Name = format("%s-nsg-internal-%s-%s", var.resourceOwner, each.key, random_id.buildSuffix.hex)
Terraform = "true"
}
}
############################ VNets ############################
# Create VNets
module "network" {
for_each = local.vnets
source = "Azure/vnet/azurerm"
resource_group_name = azurerm_resource_group.rg[each.key].name
vnet_name = format("%s-vnet-%s-%s", var.projectPrefix, each.key, random_id.buildSuffix.hex)
vnet_location = each.value["location"]
address_space = each.value["addressSpace"]
subnet_prefixes = each.value["subnetPrefixes"]
subnet_names = each.value["subnetNames"]
nsg_ids = {
external = module.nsg-external[each.key].network_security_group_id
mgmt = module.nsg-mgmt[each.key].network_security_group_id
}
route_tables_ids = {
external = azurerm_route_table.rt[each.key].id
internal = azurerm_route_table.rt[each.key].id
}
tags = {
Name = format("%s-vnet-%s-%s", var.resourceOwner, each.key, random_id.buildSuffix.hex)
owner = var.resourceOwner
}
}
# Retrieve Hub Subnet Data
data "azurerm_subnet" "mgmtSubnetHub" {
name = "mgmt"
virtual_network_name = module.network["hub"].vnet_name
resource_group_name = azurerm_resource_group.rg["hub"].name
depends_on = [module.network["hub"].vnet_subnets]
}
data "azurerm_subnet" "externalSubnetHub" {
name = "external"
virtual_network_name = module.network["hub"].vnet_name
resource_group_name = azurerm_resource_group.rg["hub"].name
depends_on = [module.network["hub"].vnet_subnets]
}
data "azurerm_subnet" "internalSubnetHub" {
name = "internal"
virtual_network_name = module.network["hub"].vnet_name
resource_group_name = azurerm_resource_group.rg["hub"].name
depends_on = [module.network["hub"].vnet_subnets]
}
data "azurerm_subnet" "routeServerSubnetHub" {
name = "RouteServerSubnet"
virtual_network_name = module.network["hub"].vnet_name
resource_group_name = azurerm_resource_group.rg["hub"].name
depends_on = [module.network["hub"].vnet_subnets]
}
############################ VNet Peering ############################
# Create hub to spoke peerings
resource "azurerm_virtual_network_peering" "hubToSpoke" {
for_each = local.spokeHubPeerings
name = format("hub-to-%s", each.key)
resource_group_name = azurerm_resource_group.rg["hub"].name
virtual_network_name = module.network["hub"].vnet_name
remote_virtual_network_id = module.network[each.key].vnet_id
allow_forwarded_traffic = true
allow_gateway_transit = true
depends_on = [azurerm_virtual_hub_bgp_connection.bigip]
}
# Create spoke to hub peerings
resource "azurerm_virtual_network_peering" "spokeToHub" {
for_each = local.spokeHubPeerings
name = format("%s-to-hub", each.key)
resource_group_name = azurerm_resource_group.rg[each.key].name
virtual_network_name = module.network[each.key].vnet_name
remote_virtual_network_id = module.network["hub"].vnet_id
allow_forwarded_traffic = true
use_remote_gateways = true
depends_on = [azurerm_virtual_hub_bgp_connection.bigip]
}
############################ Route Server and BGP Peering ############################
resource "azurerm_virtual_hub" "routeServer" {
name = format("%s-routeServer-%s", var.projectPrefix, random_id.buildSuffix.hex)
resource_group_name = azurerm_resource_group.rg["hub"].name
location = azurerm_resource_group.rg["hub"].location
sku = "Standard"
depends_on = [module.network["hub"].vnet_subnets]
tags = {
Name = format("%s-routeServer-%s", var.resourceOwner, random_id.buildSuffix.hex)
owner = var.resourceOwner
}
}
resource "azurerm_public_ip" "routeServerPip" {
name = format("%s-routeServerPip-%s", var.projectPrefix, random_id.buildSuffix.hex)
resource_group_name = azurerm_resource_group.rg["hub"].name
location = azurerm_resource_group.rg["hub"].location
allocation_method = "Static"
sku = "Standard"
}
resource "azurerm_virtual_hub_ip" "routeServerIp" {
name = format("%s-routeServerIp-%s", var.projectPrefix, random_id.buildSuffix.hex)
virtual_hub_id = azurerm_virtual_hub.routeServer.id
public_ip_address_id = azurerm_public_ip.routeServerPip.id
subnet_id = data.azurerm_subnet.routeServerSubnetHub.id
}
resource "azurerm_virtual_hub_bgp_connection" "bigip" {
count = var.instanceCountBigIp
name = "bigip-${count.index}"
virtual_hub_id = azurerm_virtual_hub.routeServer.id
peer_asn = 65530
peer_ip = element(flatten(module.bigip[count.index].private_addresses.public_private.private_ip), 0)
depends_on = [azurerm_virtual_hub_ip.routeServerIp]
}
############################ VM for Client ############################
module "client" {
source = "Azure/compute/azurerm"
version = "4.0"
resource_group_name = azurerm_resource_group.rg["spoke1"].name
vm_hostname = "client"
vm_os_publisher = "Canonical"
vm_os_offer = "0001-com-ubuntu-server-focal"
vm_os_sku = "20_04-lts"
vnet_subnet_id = module.network["spoke1"].vnet_subnets[0]
ssh_key = var.ssh_key
remote_port = "22"
tags = {
Name = format("%s-client-%s", var.resourceOwner, random_id.buildSuffix.hex)
owner = var.resourceOwner
}
}
############################ VM for App ############################
# App Onboarding script
data "local_file" "appOnboard" {
filename = "${path.module}/scripts/init-app.sh"
}
module "app" {
source = "Azure/compute/azurerm"
version = "4.0"
resource_group_name = azurerm_resource_group.rg["spoke2"].name
vm_hostname = "app"
vm_os_publisher = "Canonical"
vm_os_offer = "0001-com-ubuntu-server-focal"
vm_os_sku = "20_04-lts"
vnet_subnet_id = module.network["spoke2"].vnet_subnets[2]
ssh_key = var.ssh_key
custom_data = data.local_file.appOnboard.content_base64
tags = {
Name = format("%s-app-%s", var.resourceOwner, random_id.buildSuffix.hex)
owner = var.resourceOwner
}
}
############################ Key Vault ############################
# Subscription Info
data "azurerm_subscription" "main" {
}
# Note: Jeff Giroux (REMOVE LATER)
# https://github.com/F5Networks/terraform-azure-bigip-module/issues/42
# The user_identity is not passed in BIG-IP module to the
# Key Vault policy. As a result, the Terraform auto created
# user identity is assigned the policy instead of the
# user-supplied managed identity.
# Managed Identity Info
data "azurerm_user_assigned_identity" "main" {
count = var.az_keyvault_authentication ? 1 : 0
name = split("/", var.user_identity)[8]
resource_group_name = split("/", var.user_identity)[4]
}
# Key Vault info
data "azurerm_key_vault" "main" {
count = var.az_keyvault_authentication ? 1 : 0
name = var.keyvault_name
resource_group_name = var.keyvault_rg
}
# Create Key Vault policies
resource "azurerm_key_vault_access_policy" "main" {
count = var.az_keyvault_authentication ? 1 : 0
key_vault_id = data.azurerm_key_vault.main[0].id
tenant_id = data.azurerm_subscription.main.tenant_id
object_id = data.azurerm_user_assigned_identity.main[0].principal_id
key_permissions = [
"Get", "List", "Update", "Create", "Import", "Delete", "Recover", "Backup", "Restore",
]
secret_permissions = [
"Get", "List", "Set", "Delete", "Recover", "Restore", "Backup", "Purge",
]
}