-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.tf
More file actions
executable file
·544 lines (495 loc) · 21 KB
/
main.tf
File metadata and controls
executable file
·544 lines (495 loc) · 21 KB
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//********************** Basic Configuration **************************//
module "common" {
source = "../common"
resource_group_name = var.resource_group_name
location = var.location
admin_password = var.admin_password
installation_type = var.installation_type
module_name = local.module_name
module_version = local.module_version
number_of_vm_instances = var.number_of_vm_instances
allow_upload_download = var.allow_upload_download
vm_size = var.vm_size
disk_size = var.disk_size
is_blink = var.is_blink
os_version = var.os_version
vm_os_sku = var.vm_os_sku
vm_os_offer = var.vm_os_offer
authentication_type = var.authentication_type
serial_console_password_hash = var.serial_console_password_hash
maintenance_mode_password_hash = var.maintenance_mode_password_hash
storage_account_additional_ips = var.storage_account_additional_ips
tags = merge(lookup(var.tags, "resource-group", {}), lookup(var.tags, "all", {}))
}
//********************** Networking **************************//
resource "random_id" "random_id" {
byte_length = 13
keepers = {
rg_id = module.common.resource_group_id
}
}
resource "azurerm_public_ip_prefix" "public_ip_prefix" {
count = var.use_public_ip_prefix && var.create_public_ip_prefix ? 1 : 0
name = "${module.common.resource_group_name}-ipprefix"
location = module.common.resource_group_location
resource_group_name = module.common.resource_group_name
prefix_length = 30
tags = merge(lookup(var.tags, "public-ip-prefix", {}), lookup(var.tags, "all", {}))
}
data "azurerm_subnet" "frontend" {
name = var.frontend_subnet_name
virtual_network_name = var.vnet_name
resource_group_name = var.vnet_resource_group
}
data "azurerm_subnet" "backend" {
name = var.backend_subnet_name
virtual_network_name = var.vnet_name
resource_group_name = var.vnet_resource_group
}
resource "azurerm_public_ip" "public-ip" {
count = 2
name = "${var.cluster_name}${count.index+1}_IP"
location = module.common.resource_group_location
resource_group_name = module.common.resource_group_name
allocation_method = var.vnet_allocation_method
sku = var.sku
domain_name_label = "${lower(var.cluster_name)}-${count.index+1}-${random_id.random_id.hex}"
public_ip_prefix_id = var.use_public_ip_prefix ? (var.create_public_ip_prefix ? azurerm_public_ip_prefix.public_ip_prefix[0].id : var.existing_public_ip_prefix_id) : null
tags = merge(lookup(var.tags, "public-ip", {}), lookup(var.tags, "all", {}))
}
resource "azurerm_public_ip" "cluster-vip" {
name = var.cluster_name
location = module.common.resource_group_location
resource_group_name = module.common.resource_group_name
allocation_method = var.vnet_allocation_method
sku = var.sku
domain_name_label = "${lower(var.cluster_name)}-vip-${random_id.random_id.hex}"
public_ip_prefix_id = var.use_public_ip_prefix ? (var.create_public_ip_prefix ? azurerm_public_ip_prefix.public_ip_prefix[0].id : var.existing_public_ip_prefix_id) : null
tags = merge(lookup(var.tags, "public-ip", {}), lookup(var.tags, "all", {}))
}
resource "azurerm_network_interface" "nic_vip" {
depends_on = [
azurerm_public_ip.cluster-vip,
azurerm_public_ip.public-ip]
name = "${var.cluster_name}1-eth0"
location = module.common.resource_group_location
resource_group_name = module.common.resource_group_name
enable_ip_forwarding = true
enable_accelerated_networking = true
ip_configuration {
name = "ipconfig1"
primary = true
subnet_id = data.azurerm_subnet.frontend.id
private_ip_address_allocation = var.vnet_allocation_method
private_ip_address = cidrhost(data.azurerm_subnet.frontend.address_prefixes[0], var.frontend_IP_addresses[0])
public_ip_address_id = azurerm_public_ip.public-ip.0.id
}
ip_configuration {
name = "cluster-vip"
subnet_id = data.azurerm_subnet.frontend.id
primary = false
private_ip_address_allocation = var.vnet_allocation_method
private_ip_address = cidrhost(data.azurerm_subnet.frontend.address_prefixes[0], var.frontend_IP_addresses[2])
public_ip_address_id = azurerm_public_ip.cluster-vip.id
}
lifecycle {
ignore_changes = [
# Ignore changes to ip_configuration when Re-applying, e.g. because a cluster failover and associating the cluster- vip with the other member.
# updates these based on some ruleset managed elsewhere.
ip_configuration
]
}
tags = merge(lookup(var.tags, "network-interface", {}), lookup(var.tags, "all", {}))
}
resource "azurerm_network_interface_backend_address_pool_association" "nic_vip_lb_association" {
depends_on = [azurerm_network_interface.nic_vip, azurerm_lb_backend_address_pool.frontend-lb-pool]
network_interface_id = azurerm_network_interface.nic_vip.id
ip_configuration_name = "ipconfig1"
backend_address_pool_id = azurerm_lb_backend_address_pool.frontend-lb-pool.id
}
resource "azurerm_network_interface" "nic" {
depends_on = [
azurerm_public_ip.public-ip,
azurerm_lb.frontend-lb]
name = "${var.cluster_name}2-eth0"
location = module.common.resource_group_location
resource_group_name = module.common.resource_group_name
enable_ip_forwarding = true
enable_accelerated_networking = true
ip_configuration {
name = "ipconfig1"
primary = true
subnet_id = data.azurerm_subnet.frontend.id
private_ip_address_allocation = var.vnet_allocation_method
private_ip_address = cidrhost(data.azurerm_subnet.frontend.address_prefixes[0], var.frontend_IP_addresses[1])
public_ip_address_id = azurerm_public_ip.public-ip.1.id
}
lifecycle {
ignore_changes = [
# Ignore changes to ip_configuration when Re-applying, e.g. because a cluster failover and associating the cluster- vip with the other member.
# updates these based on some ruleset managed elsewhere.
ip_configuration
]
}
tags = merge(lookup(var.tags, "network-interface", {}), lookup(var.tags, "all", {}))
}
resource "azurerm_network_interface_backend_address_pool_association" "nic_lb_association" {
depends_on = [azurerm_network_interface.nic, azurerm_lb_backend_address_pool.frontend-lb-pool]
network_interface_id = azurerm_network_interface.nic.id
ip_configuration_name = "ipconfig1"
backend_address_pool_id = azurerm_lb_backend_address_pool.frontend-lb-pool.id
}
resource "azurerm_network_interface" "nic1" {
depends_on = [
azurerm_lb.backend-lb]
count = 2
name = "${var.cluster_name}${count.index+1}-eth1"
location = module.common.resource_group_location
resource_group_name = module.common.resource_group_name
enable_ip_forwarding = true
enable_accelerated_networking = true
ip_configuration {
name = "ipconfig2"
subnet_id = data.azurerm_subnet.backend.id
private_ip_address_allocation = var.vnet_allocation_method
private_ip_address = cidrhost(data.azurerm_subnet.backend.address_prefixes[0], var.backend_IP_addresses[count.index+1])
}
tags = merge(lookup(var.tags, "network-interface", {}), lookup(var.tags, "all", {}))
}
resource "azurerm_network_interface_backend_address_pool_association" "nic1_lb_association" {
depends_on = [azurerm_network_interface.nic1, azurerm_lb_backend_address_pool.backend-lb-pool]
count = 2
network_interface_id = azurerm_network_interface.nic1[count.index].id
ip_configuration_name = "ipconfig2"
backend_address_pool_id = azurerm_lb_backend_address_pool.backend-lb-pool.id
}
//********************** Load Balancers **************************//
resource "azurerm_public_ip" "public-ip-lb" {
name = "frontend_lb_ip"
location = module.common.resource_group_location
resource_group_name = module.common.resource_group_name
allocation_method = var.vnet_allocation_method
sku = var.sku
domain_name_label = "${lower(var.cluster_name)}-${random_id.random_id.hex}"
public_ip_prefix_id = var.use_public_ip_prefix ? (var.create_public_ip_prefix ? azurerm_public_ip_prefix.public_ip_prefix[0].id : var.existing_public_ip_prefix_id) : null
tags = merge(lookup(var.tags, "public-ip", {}), lookup(var.tags, "all", {}))
}
resource "azurerm_lb" "frontend-lb" {
depends_on = [
azurerm_public_ip.public-ip-lb]
name = "frontend-lb"
location = module.common.resource_group_location
resource_group_name = module.common.resource_group_name
sku = var.sku
frontend_ip_configuration {
name = "LoadBalancerFrontend"
public_ip_address_id = azurerm_public_ip.public-ip-lb.id
}
tags = merge(lookup(var.tags, "load-balancer", {}), lookup(var.tags, "all", {}))
}
resource "azurerm_lb_backend_address_pool" "frontend-lb-pool" {
loadbalancer_id = azurerm_lb.frontend-lb.id
name = "frontend-lb-pool"
}
resource "azurerm_lb" "backend-lb" {
name = "backend-lb"
location = module.common.resource_group_location
resource_group_name = module.common.resource_group_name
sku = var.sku
frontend_ip_configuration {
name = "backend-lb"
subnet_id = data.azurerm_subnet.backend.id
private_ip_address_allocation = var.vnet_allocation_method
private_ip_address = cidrhost(data.azurerm_subnet.backend.address_prefixes[0], var.backend_IP_addresses[0])
}
tags = merge(lookup(var.tags, "load-balancer", {}), lookup(var.tags, "all", {}))
}
resource "azurerm_lb_backend_address_pool" "backend-lb-pool" {
name = "backend-lb-pool"
loadbalancer_id = azurerm_lb.backend-lb.id
}
resource "azurerm_lb_probe" "azure_lb_healprob" {
count = 2
loadbalancer_id = count.index == 0 ? azurerm_lb.frontend-lb.id : azurerm_lb.backend-lb.id
name = var.lb_probe_name
protocol = var.lb_probe_protocol
port = var.lb_probe_port
interval_in_seconds = var.lb_probe_interval
number_of_probes = var.lb_probe_unhealthy_threshold
}
resource "azurerm_lb_rule" "backend_lb_rules" {
loadbalancer_id = azurerm_lb.backend-lb.id
name = "backend-lb"
protocol = "All"
frontend_port = 0
backend_port = 0
frontend_ip_configuration_name = "backend-lb"
load_distribution = "Default"
backend_address_pool_ids = [azurerm_lb_backend_address_pool.backend-lb-pool.id]
probe_id = azurerm_lb_probe.azure_lb_healprob[1].id
enable_floating_ip = var.enable_floating_ip
}
//********************** Availability Set **************************//
locals {
availability_set_condition = var.availability_type == "Availability Set" ? true : false
SSH_authentication_type_condition = var.authentication_type == "SSH Public Key" ? true : false
}
resource "azurerm_availability_set" "availability-set" {
count = local.availability_set_condition ? 1 : 0
name = "${var.cluster_name}-AvailabilitySet"
location = module.common.resource_group_location
resource_group_name = module.common.resource_group_name
platform_fault_domain_count = 2
platform_update_domain_count = 5
managed = true
tags = merge(lookup(var.tags, "availability-set", {}), lookup(var.tags, "all", {}))
}
//********************** Storage accounts **************************//
// Generate random text for a unique storage account name
resource "random_id" "randomId" {
keepers = {
# Generate a new ID only when a new resource group is defined
resource_group = module.common.resource_group_name
}
byte_length = 8
}
resource "azurerm_storage_account" "vm-boot-diagnostics-storage" {
name = "bootdiag${random_id.randomId.hex}"
resource_group_name = module.common.resource_group_name
location = module.common.resource_group_location
account_tier = module.common.storage_account_tier
account_replication_type = module.common.account_replication_type
min_tls_version = "TLS1_2"
network_rules {
default_action = var.add_storage_account_ip_rules ? "Deny" : "Allow"
ip_rules = module.common.storage_account_ip_rules
}
blob_properties {
delete_retention_policy {
days = "15"
}
}
tags = merge(lookup(var.tags, "storage-account", {}), lookup(var.tags, "all", {}))
}
//********************** Virtual Machines **************************//
locals {
custom_image_condition = var.source_image_vhd_uri == "noCustomUri" ? false : true
}
resource "azurerm_image" "custom-image" {
count = local.custom_image_condition ? 1 : 0
name = "custom-image"
location = var.location
resource_group_name = module.common.resource_group_name
os_disk {
os_type = "Linux"
os_state = "Generalized"
blob_uri = var.source_image_vhd_uri
}
tags = merge(lookup(var.tags, "custom-image", {}), lookup(var.tags, "all", {}))
}
resource "azurerm_virtual_machine" "vm-instance-availability-set" {
depends_on = [
azurerm_network_interface.nic,
azurerm_network_interface.nic1,
azurerm_network_interface.nic_vip]
count = local.availability_set_condition ? module.common.number_of_vm_instances : 0
name = "${var.cluster_name}${count.index+1}"
location = module.common.resource_group_location
resource_group_name = module.common.resource_group_name
availability_set_id = local.availability_set_condition ? azurerm_availability_set.availability-set[0].id : ""
vm_size = module.common.vm_size
network_interface_ids = count.index == 0 ? [
azurerm_network_interface.nic_vip.id,
azurerm_network_interface.nic1.0.id] : [
azurerm_network_interface.nic.id,
azurerm_network_interface.nic1.1.id]
delete_os_disk_on_termination = module.common.delete_os_disk_on_termination
primary_network_interface_id = count.index == 0 ? azurerm_network_interface.nic_vip.id : azurerm_network_interface.nic.id
identity {
type = module.common.vm_instance_identity
}
storage_image_reference {
id = local.custom_image_condition ? azurerm_image.custom-image[0].id : null
publisher = local.custom_image_condition ? null : module.common.publisher
offer = module.common.vm_os_offer
sku = module.common.vm_os_sku
version = module.common.vm_os_version
}
storage_os_disk {
name = "${var.cluster_name}-${count.index+1}"
create_option = module.common.storage_os_disk_create_option
caching = module.common.storage_os_disk_caching
managed_disk_type = module.common.storage_account_type
disk_size_gb = module.common.disk_size
}
dynamic "plan" {
for_each = local.custom_image_condition ? [
] : [1]
content {
name = module.common.vm_os_sku
publisher = module.common.publisher
product = module.common.vm_os_offer
}
}
os_profile {
computer_name = "${lower(var.cluster_name)}${count.index+1}"
admin_username = module.common.admin_username
admin_password = module.common.admin_password
custom_data = templatefile("${path.module}/cloud-init.sh", {
installation_type = module.common.installation_type
allow_upload_download = module.common.allow_upload_download
os_version = module.common.os_version
module_name = module.common.module_name
module_version = module.common.module_version
template_type = "terraform"
is_blink = module.common.is_blink
bootstrap_script64 = base64encode(var.bootstrap_script)
location = module.common.resource_group_location
sic_key = var.sic_key
tenant_id = var.tenant_id
virtual_network = var.vnet_name
cluster_name = var.cluster_name
external_private_addresses = azurerm_network_interface.nic_vip.ip_configuration[1].private_ip_address
enable_custom_metrics = var.enable_custom_metrics ? "yes" : "no"
admin_shell = var.admin_shell
smart_1_cloud_token = count.index == 0 ? var.smart_1_cloud_token_a : var.smart_1_cloud_token_b
serial_console_password_hash = var.serial_console_password_hash
maintenance_mode_password_hash = var.maintenance_mode_password_hash
})
}
os_profile_linux_config {
disable_password_authentication = local.SSH_authentication_type_condition
dynamic "ssh_keys" {
for_each = local.SSH_authentication_type_condition ? [
1] : []
content {
path = "/home/notused/.ssh/authorized_keys"
key_data = var.admin_SSH_key
}
}
}
boot_diagnostics {
enabled = module.common.boot_diagnostics
storage_uri = module.common.boot_diagnostics ? join(",", azurerm_storage_account.vm-boot-diagnostics-storage.*.primary_blob_endpoint) : ""
}
tags = merge(lookup(var.tags, "virtual-machine", {}), lookup(var.tags, "all", {}))
}
resource "azurerm_virtual_machine" "vm-instance-availability-zone" {
depends_on = [
azurerm_network_interface.nic,
azurerm_network_interface.nic1,
azurerm_network_interface.nic_vip]
count = local.availability_set_condition ? 0 : module.common.number_of_vm_instances
name = "${var.cluster_name}${count.index+1}"
location = module.common.resource_group_location
resource_group_name = module.common.resource_group_name
zones = [
count.index+1]
vm_size = module.common.vm_size
network_interface_ids = count.index == 0 ? [
azurerm_network_interface.nic_vip.id,
azurerm_network_interface.nic1.0.id] : [
azurerm_network_interface.nic.id,
azurerm_network_interface.nic1.1.id]
delete_os_disk_on_termination = module.common.delete_os_disk_on_termination
primary_network_interface_id = count.index == 0 ? azurerm_network_interface.nic_vip.id : azurerm_network_interface.nic.id
identity {
type = module.common.vm_instance_identity
}
storage_image_reference {
id = local.custom_image_condition ? azurerm_image.custom-image[0].id : null
publisher = local.custom_image_condition ? null : module.common.publisher
offer = module.common.vm_os_offer
sku = module.common.vm_os_sku
version = module.common.vm_os_version
}
storage_os_disk {
name = "${var.cluster_name}-${count.index+1}"
create_option = module.common.storage_os_disk_create_option
caching = module.common.storage_os_disk_caching
managed_disk_type = module.common.storage_account_type
disk_size_gb = module.common.disk_size
}
dynamic "plan" {
for_each = local.custom_image_condition ? [
] : [1]
content {
name = module.common.vm_os_sku
publisher = module.common.publisher
product = module.common.vm_os_offer
}
}
os_profile {
computer_name = "${lower(var.cluster_name)}${count.index+1}"
admin_username = module.common.admin_username
admin_password = module.common.admin_password
custom_data = templatefile("${path.module}/cloud-init.sh", {
installation_type = module.common.installation_type
allow_upload_download = module.common.allow_upload_download
os_version = module.common.os_version
module_name = module.common.module_name
module_version = module.common.module_version
template_type = "terraform"
is_blink = module.common.is_blink
bootstrap_script64 = base64encode(var.bootstrap_script)
location = module.common.resource_group_location
sic_key = var.sic_key
tenant_id = var.tenant_id
virtual_network = var.vnet_name
cluster_name = var.cluster_name
external_private_addresses = azurerm_network_interface.nic_vip.ip_configuration[1].private_ip_address
enable_custom_metrics = var.enable_custom_metrics ? "yes" : "no"
admin_shell = var.admin_shell
smart_1_cloud_token = count.index == 0 ? var.smart_1_cloud_token_a : var.smart_1_cloud_token_b
serial_console_password_hash = var.serial_console_password_hash
maintenance_mode_password_hash = var.maintenance_mode_password_hash
})
}
os_profile_linux_config {
disable_password_authentication = local.SSH_authentication_type_condition
dynamic "ssh_keys" {
for_each = local.SSH_authentication_type_condition ? [
1] : []
content {
path = "/home/notused/.ssh/authorized_keys"
key_data = var.admin_SSH_key
}
}
}
boot_diagnostics {
enabled = module.common.boot_diagnostics
storage_uri = module.common.boot_diagnostics ? join(",", azurerm_storage_account.vm-boot-diagnostics-storage.*.primary_blob_endpoint) : ""
}
tags = merge(lookup(var.tags, "virtual-machine", {}), lookup(var.tags, "all", {}))
}
//********************** Role Assigments **************************//
data "azurerm_role_definition" "virtual_machine_contributor_role_definition" {
name = "Virtual Machine Contributor"
}
data "azurerm_role_definition" "reader_role_definition" {
name = "Reader"
}
data "azurerm_client_config" "client_config" {
}
resource "azurerm_role_assignment" "cluster_virtual_machine_contributor_assignment" {
count = 2
lifecycle {
ignore_changes = [
role_definition_id, principal_id
]
}
scope = module.common.resource_group_id
role_definition_id = data.azurerm_role_definition.virtual_machine_contributor_role_definition.id
principal_id = local.availability_set_condition ? lookup(azurerm_virtual_machine.vm-instance-availability-set[count.index].identity[0], "principal_id") : lookup(azurerm_virtual_machine.vm-instance-availability-zone[count.index].identity[0], "principal_id")
}
resource "azurerm_role_assignment" "cluster_reader_assigment" {
count = 2
lifecycle {
ignore_changes = [
role_definition_id, principal_id
]
}
scope = module.common.resource_group_id
role_definition_id = data.azurerm_role_definition.reader_role_definition.id
principal_id = local.availability_set_condition ? lookup(azurerm_virtual_machine.vm-instance-availability-set[count.index].identity[0], "principal_id") : lookup(azurerm_virtual_machine.vm-instance-availability-zone[count.index].identity[0], "principal_id")
}