forked from kumarvna/terraform-azurerm-vm-scale-sets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
577 lines (515 loc) · 26.4 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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#---------------------------
# Local declarations
#---------------------------
locals {
nsg_inbound_rules = { for idx, security_rule in var.nsg_inbound_rules : security_rule.name => {
idx : idx,
security_rule : security_rule,
}
}
}
#---------------------------------------------------------------
# Generates SSH2 key Pair for Linux VM's (Dev Environment only)
#---------------------------------------------------------------
resource "tls_private_key" "rsa" {
count = var.generate_admin_ssh_key == true && var.os_flavor == "linux" ? 1 : 0
algorithm = "RSA"
rsa_bits = 4096
}
#----------------------------------------------------------
# Resource Group, VNet, Subnet selection & Random Resources
#----------------------------------------------------------
data "azurerm_resource_group" "rg" {
name = var.resource_group_name
}
data "azurerm_virtual_network" "vnet" {
name = var.virtual_network_name
resource_group_name = data.azurerm_resource_group.rg.name
}
data "azurerm_subnet" "snet" {
name = var.subnet_name
virtual_network_name = data.azurerm_virtual_network.vnet.name
resource_group_name = data.azurerm_resource_group.rg.name
}
data "azurerm_log_analytics_workspace" "logws" {
count = var.log_analytics_workspace_name != null ? 1 : 0
name = var.log_analytics_workspace_name
resource_group_name = data.azurerm_resource_group.rg.name
}
data "azurerm_storage_account" "storeacc" {
count = var.hub_storage_account_name != null ? 1 : 0
name = var.hub_storage_account_name
resource_group_name = data.azurerm_resource_group.rg.name
}
resource "random_password" "passwd" {
count = var.disable_password_authentication != true || var.os_flavor == "windows" && var.admin_password == null ? 1 : 0
length = 24
min_upper = 4
min_lower = 2
min_numeric = 4
special = false
keepers = {
admin_password = var.os_flavor
}
}
#-----------------------------------
# Public IP for Load Balancer
#-----------------------------------
resource "azurerm_public_ip" "pip" {
count = var.enable_load_balancer == true && var.load_balancer_type == "public" ? 1 : 0
name = lower("pip-vm-${var.vmscaleset_name}-${data.azurerm_resource_group.rg.location}-0${count.index + 1}")
location = data.azurerm_resource_group.rg.location
resource_group_name = data.azurerm_resource_group.rg.name
allocation_method = "Static"
sku = "Standard"
domain_name_label = format("vm%spip0${count.index + 1}", lower(replace(var.vmscaleset_name, "/[[:^alnum:]]/", "")))
tags = merge({ "ResourceName" = lower("pip-vm-${var.vmscaleset_name}-${data.azurerm_resource_group.rg.location}-0${count.index + 1}") }, var.tags, )
}
#---------------------------------------
# External Load Balancer with Public IP
#---------------------------------------
resource "azurerm_lb" "vmsslb" {
count = var.enable_load_balancer ? 1 : 0
name = var.load_balancer_type == "public" ? lower("lbext-${var.vmscaleset_name}-${data.azurerm_resource_group.rg.location}") : lower("lbint-${var.vmscaleset_name}-${data.azurerm_resource_group.rg.location}")
location = data.azurerm_resource_group.rg.location
resource_group_name = data.azurerm_resource_group.rg.name
sku = var.load_balancer_sku
tags = merge({ "ResourceName" = var.load_balancer_type == "public" ? lower("lbext-${var.vmscaleset_name}-${data.azurerm_resource_group.rg.location}") : lower("lbint-${var.vmscaleset_name}-${data.azurerm_resource_group.rg.location}") }, var.tags, )
frontend_ip_configuration {
name = var.load_balancer_type == "public" ? lower("lbext-frontend-${var.vmscaleset_name}") : lower("lbint-frontend-${var.vmscaleset_name}")
public_ip_address_id = var.enable_load_balancer == true && var.load_balancer_type == "public" ? azurerm_public_ip.pip[count.index].id : null
private_ip_address_allocation = var.load_balancer_type == "private" ? var.private_ip_address_allocation : null
private_ip_address = var.load_balancer_type == "private" && var.private_ip_address_allocation == "Static" ? var.lb_private_ip_address : null
subnet_id = var.load_balancer_type == "private" ? data.azurerm_subnet.snet.id : null
}
}
#---------------------------------------
# Backend address pool for Load Balancer
#---------------------------------------
resource "azurerm_lb_backend_address_pool" "bepool" {
count = var.enable_load_balancer ? 1 : 0
name = lower("lbe-backend-pool-${var.vmscaleset_name}")
#resource_group_name = data.azurerm_resource_group.rg.name
loadbalancer_id = azurerm_lb.vmsslb[count.index].id
}
#---------------------------------------
# Load Balancer NAT pool
#---------------------------------------
resource "azurerm_lb_nat_pool" "natpol" {
count = var.enable_load_balancer && var.enable_lb_nat_pool ? 1 : 0
name = lower("lbe-nat-pool-${var.vmscaleset_name}-${data.azurerm_resource_group.rg.location}")
resource_group_name = data.azurerm_resource_group.rg.name
loadbalancer_id = azurerm_lb.vmsslb.0.id
protocol = "Tcp"
frontend_port_start = var.nat_pool_frontend_ports[0]
frontend_port_end = var.nat_pool_frontend_ports[1]
backend_port = var.os_flavor == "linux" ? 22 : 3389
frontend_ip_configuration_name = azurerm_lb.vmsslb.0.frontend_ip_configuration.0.name
}
#---------------------------------------
# Health Probe for resources
#---------------------------------------
resource "azurerm_lb_probe" "lbp" {
count = var.enable_load_balancer ? 1 : 0
name = lower("lb-probe-port-${var.load_balancer_health_probe_port}-${var.vmscaleset_name}")
resource_group_name = data.azurerm_resource_group.rg.name
loadbalancer_id = azurerm_lb.vmsslb[count.index].id
port = var.load_balancer_health_probe_port
}
#--------------------------
# Load Balancer Rules
#--------------------------
resource "azurerm_lb_rule" "lbrule" {
count = var.enable_load_balancer ? length(var.load_balanced_port_list) : 0
name = format("%s-%02d-rule", var.vmscaleset_name, count.index + 1)
resource_group_name = data.azurerm_resource_group.rg.name
loadbalancer_id = azurerm_lb.vmsslb[0].id
probe_id = azurerm_lb_probe.lbp[0].id
protocol = "Tcp"
frontend_port = tostring(var.load_balanced_port_list[count.index])
backend_port = tostring(var.load_balanced_port_list[count.index])
frontend_ip_configuration_name = azurerm_lb.vmsslb[0].frontend_ip_configuration.0.name
backend_address_pool_id = azurerm_lb_backend_address_pool.bepool[0].id
}
#---------------------------------------------------------------
# Network security group for Virtual Machine Network Interface
#---------------------------------------------------------------
resource "azurerm_network_security_group" "nsg" {
name = lower("nsg_${var.vmscaleset_name}_${data.azurerm_resource_group.rg.location}_in")
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
tags = merge({ "ResourceName" = lower("nsg_${var.vmscaleset_name}_${data.azurerm_resource_group.rg.location}_in") }, var.tags, )
}
resource "azurerm_network_security_rule" "nsg_rule" {
for_each = local.nsg_inbound_rules
name = each.key
priority = 100 * (each.value.idx + 1)
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = each.value.security_rule.destination_port_range
source_address_prefix = each.value.security_rule.source_address_prefix
destination_address_prefix = element(concat(data.azurerm_subnet.snet.address_prefixes, [""]), 0)
description = "Inbound_Port_${each.value.security_rule.destination_port_range}"
resource_group_name = data.azurerm_resource_group.rg.name
network_security_group_name = azurerm_network_security_group.nsg.name
depends_on = [azurerm_network_security_group.nsg]
}
#---------------------------------------
# Linux Virtual machine scale set
#---------------------------------------
resource "azurerm_linux_virtual_machine_scale_set" "linux_vmss" {
count = var.os_flavor == "linux" ? 1 : 0
name = format("vmss-%s%s", lower(var.vmscaleset_name), count.index + 1)
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
overprovision = var.overprovision
sku = var.virtual_machine_size
instances = var.instances_count
# This is not supported in all regions right now, for example China.
# zones = var.availability_zones
zone_balance = var.availability_zone_balance
single_placement_group = var.single_placement_group
admin_username = var.admin_username
admin_password = var.disable_password_authentication != true && var.admin_password == null ? random_password.passwd[count.index].result : var.admin_password
tags = merge({ "ResourceName" = format("vm%s%s", lower(replace(var.vmscaleset_name, "/[[:^alnum:]]/", "")), count.index + 1) }, var.tags, )
source_image_id = var.source_image_id != null ? var.source_image_id : null
upgrade_mode = var.os_upgrade_mode
health_probe_id = var.enable_load_balancer ? azurerm_lb_probe.lbp[0].id : null
provision_vm_agent = true
disable_password_authentication = var.disable_password_authentication
admin_ssh_key {
username = var.admin_username
public_key = var.generate_admin_ssh_key == true && var.os_flavor == "linux" ? tls_private_key.rsa[0].public_key_openssh : file(var.admin_ssh_key_data)
}
dynamic "source_image_reference" {
for_each = var.source_image_id != null ? [] : [1]
content {
publisher = var.custom_image != null ? var.custom_image["publisher"] : var.linux_distribution_list[lower(var.linux_distribution_name)]["publisher"]
offer = var.custom_image != null ? var.custom_image["offer"] : var.linux_distribution_list[lower(var.linux_distribution_name)]["offer"]
sku = var.custom_image != null ? var.custom_image["sku"] : var.linux_distribution_list[lower(var.linux_distribution_name)]["sku"]
version = var.custom_image != null ? var.custom_image["version"] : var.linux_distribution_list[lower(var.linux_distribution_name)]["version"]
}
}
os_disk {
storage_account_type = var.os_disk_storage_account_type
caching = "ReadWrite"
}
dynamic "data_disk" {
for_each = var.additional_data_disks
content {
lun = data_disk.key
disk_size_gb = data_disk.value
caching = "ReadWrite"
storage_account_type = var.additional_data_disks_storage_account_type
}
}
network_interface {
name = lower("nic-${format("vm%s%s", lower(replace(var.vmscaleset_name, "/[[:^alnum:]]/", "")), count.index + 1)}")
primary = true
dns_servers = var.dns_servers
enable_ip_forwarding = var.enable_ip_forwarding
enable_accelerated_networking = var.enable_accelerated_networking
network_security_group_id = azurerm_network_security_group.nsg.id
ip_configuration {
name = lower("ipconfig-${format("vm%s%s", lower(replace(var.vmscaleset_name, "/[[:^alnum:]]/", "")), count.index + 1)}")
primary = true
subnet_id = data.azurerm_subnet.snet.id
load_balancer_backend_address_pool_ids = var.enable_load_balancer ? [azurerm_lb_backend_address_pool.bepool[0].id] : null
load_balancer_inbound_nat_rules_ids = var.enable_load_balancer && var.enable_lb_nat_pool ? [azurerm_lb_nat_pool.natpol[0].id] : null
dynamic "public_ip_address" {
for_each = var.assign_public_ip_to_each_vm_in_vmss ? [{}] : []
content {
name = lower("pip-${format("vm%s%s", lower(replace(var.vmscaleset_name, "/[[:^alnum:]]/", "")), "0${count.index + 1}")}")
domain_name_label = format("vm-%s-pip0${count.index + 1}", lower(replace(var.vmscaleset_name, "/[[:^alnum:]]/", "")))
}
}
}
}
# If there is a custom image, this needs to be false
dynamic "automatic_os_upgrade_policy" {
for_each = var.os_upgrade_mode == "Automatic" ? [{}] : []
content {
disable_automatic_rollback = var.source_image_id == null ? true : false
enable_automatic_os_upgrade = var.source_image_id == null ? true : false
}
}
dynamic "rolling_upgrade_policy" {
for_each = var.os_upgrade_mode == "Automatic" ? [{}] : []
content {
max_batch_instance_percent = 20
max_unhealthy_instance_percent = 20
max_unhealthy_upgraded_instance_percent = 20
pause_time_between_batches = "PT0S"
}
}
automatic_instance_repair {
enabled = var.enable_automatic_instance_repair
grace_period = var.grace_period
}
# As per the recomendation by Terraform documentation
depends_on = [azurerm_lb_rule.lbrule]
}
#---------------------------------------
# Windows Virutal machine scale set
#---------------------------------------
resource "azurerm_windows_virtual_machine_scale_set" "winsrv_vmss" {
count = var.os_flavor == "windows" ? 1 : 0
name = format("vmss-%s%s", lower(var.vmscaleset_name), count.index + 1)
computer_name_prefix = format("%s%s", lower(replace(var.vm_computer_name, "/[[:^alnum:]]/", "")), count.index + 1)
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
overprovision = var.overprovision
sku = var.virtual_machine_size
instances = var.instances_count
# This is not supported in all regions right now, for example China.
# zones = var.availability_zones
zone_balance = var.availability_zone_balance
single_placement_group = var.single_placement_group
admin_username = var.admin_username
admin_password = var.admin_password == null ? random_password.passwd[count.index].result : var.admin_password
tags = merge({ "ResourceName" = format("%s", lower(replace(var.vmscaleset_name, "/[[:^alnum:]]/", ""))) }, var.tags, )
source_image_id = var.source_image_id != null ? var.source_image_id : null
upgrade_mode = var.os_upgrade_mode
health_probe_id = var.enable_load_balancer ? azurerm_lb_probe.lbp[0].id : null
provision_vm_agent = true
license_type = var.license_type
dynamic "source_image_reference" {
for_each = var.source_image_id != null ? [] : [1]
content {
publisher = var.custom_image != null ? var.custom_image["publisher"] : var.windows_distribution_list[lower(var.windows_distribution_name)]["publisher"]
offer = var.custom_image != null ? var.custom_image["offer"] : var.windows_distribution_list[lower(var.windows_distribution_name)]["offer"]
sku = var.custom_image != null ? var.custom_image["sku"] : var.windows_distribution_list[lower(var.windows_distribution_name)]["sku"]
version = var.custom_image != null ? var.custom_image["version"] : var.windows_distribution_list[lower(var.windows_distribution_name)]["version"]
}
}
os_disk {
storage_account_type = var.os_disk_storage_account_type
caching = "ReadWrite"
}
dynamic "data_disk" {
for_each = var.additional_data_disks
content {
lun = data_disk.key
disk_size_gb = data_disk.value
caching = "ReadWrite"
storage_account_type = var.additional_data_disks_storage_account_type
}
}
network_interface {
name = lower("nic-${format("vm%s%s", lower(replace(var.vmscaleset_name, "/[[:^alnum:]]/", "")), count.index + 1)}")
primary = true
dns_servers = var.dns_servers
enable_ip_forwarding = var.enable_ip_forwarding
enable_accelerated_networking = var.enable_accelerated_networking
network_security_group_id = azurerm_network_security_group.nsg.id
ip_configuration {
name = lower("ipconfig-${format("vm%s%s", lower(replace(var.vmscaleset_name, "/[[:^alnum:]]/", "")), count.index + 1)}")
primary = true
subnet_id = data.azurerm_subnet.snet.id
load_balancer_backend_address_pool_ids = var.enable_load_balancer ? [azurerm_lb_backend_address_pool.bepool[0].id] : null
load_balancer_inbound_nat_rules_ids = var.enable_load_balancer && var.enable_lb_nat_pool ? [azurerm_lb_nat_pool.natpol.0.id] : null
dynamic "public_ip_address" {
for_each = var.assign_public_ip_to_each_vm_in_vmss ? [{}] : []
content {
name = lower("pip-${format("vm%s%s", lower(replace(var.vmscaleset_name, "/[[:^alnum:]]/", "")), count.index + 1)}")
domain_name_label = format("vm-%s%s-pip0${count.index + 1}", lower(replace(var.vmscaleset_name, "/[[:^alnum:]]/", "")))
}
}
}
}
# If there is a custom image, this needs to be false
dynamic "automatic_os_upgrade_policy" {
for_each = var.os_upgrade_mode == "Automatic" ? [{}] : []
content {
disable_automatic_rollback = var.source_image_id == null ? true : false
enable_automatic_os_upgrade = var.source_image_id == null ? true : false
}
}
dynamic "rolling_upgrade_policy" {
for_each = var.os_upgrade_mode == "Automatic" ? [{}] : []
content {
max_batch_instance_percent = 20
max_unhealthy_instance_percent = 20
max_unhealthy_upgraded_instance_percent = 5
pause_time_between_batches = "PT0S"
}
}
automatic_instance_repair {
enabled = var.enable_automatic_instance_repair
grace_period = var.grace_period
}
# As per the recomendation by Terraform documentation
depends_on = [azurerm_lb_rule.lbrule]
}
#-----------------------------------------------
# Auto Scaling for Virtual machine scale set
#-----------------------------------------------
resource "azurerm_monitor_autoscale_setting" "auto" {
count = var.enable_autoscale_for_vmss ? 1 : 0
name = lower("auto-scale-set-${var.vmscaleset_name}")
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
target_resource_id = var.os_flavor == "windows" ? azurerm_windows_virtual_machine_scale_set.winsrv_vmss.0.id : azurerm_linux_virtual_machine_scale_set.linux_vmss.0.id
profile {
name = "default"
capacity {
default = var.instances_count
minimum = var.minimum_instances_count == null ? var.instances_count : var.minimum_instances_count
maximum = var.maximum_instances_count
}
rule {
metric_trigger {
metric_name = "Percentage CPU"
metric_resource_id = var.os_flavor == "windows" ? azurerm_windows_virtual_machine_scale_set.winsrv_vmss.0.id : azurerm_linux_virtual_machine_scale_set.linux_vmss.0.id
time_grain = "PT1M"
statistic = "Average"
time_window = "PT5M"
time_aggregation = "Average"
operator = "GreaterThan"
threshold = var.scale_out_cpu_percentage_threshold
}
scale_action {
direction = "Increase"
type = "ChangeCount"
value = var.scaling_action_instances_number
cooldown = "PT1M"
}
}
rule {
metric_trigger {
metric_name = "Percentage CPU"
metric_resource_id = var.os_flavor == "windows" ? azurerm_windows_virtual_machine_scale_set.winsrv_vmss.0.id : azurerm_linux_virtual_machine_scale_set.linux_vmss.0.id
time_grain = "PT1M"
statistic = "Average"
time_window = "PT5M"
time_aggregation = "Average"
operator = "LessThan"
threshold = var.scale_in_cpu_percentage_threshold
}
scale_action {
direction = "Decrease"
type = "ChangeCount"
value = var.scaling_action_instances_number
cooldown = "PT1M"
}
}
}
}
#--------------------------------------------------------------
# Azure Log Analytics Workspace Agent Installation for windows
#--------------------------------------------------------------
resource "azurerm_virtual_machine_scale_set_extension" "omsagentwin" {
count = var.log_analytics_workspace_name != null && var.os_flavor == "windows" ? 1 : 0
name = "OmsAgentForWindows"
publisher = "Microsoft.EnterpriseCloud.Monitoring"
type = "MicrosoftMonitoringAgent"
type_handler_version = "1.0"
auto_upgrade_minor_version = true
virtual_machine_scale_set_id = azurerm_windows_virtual_machine_scale_set.winsrv_vmss.0.id
settings = <<SETTINGS
{
"workspaceId": "${data.azurerm_log_analytics_workspace.logws.0.workspace_id}"
}
SETTINGS
protected_settings = <<PROTECTED_SETTINGS
{
"workspaceKey": "${data.azurerm_log_analytics_workspace.logws.0.primary_shared_key}"
}
PROTECTED_SETTINGS
}
#--------------------------------------------------------------
# Azure Log Analytics Workspace Agent Installation for Linux
#--------------------------------------------------------------
resource "azurerm_virtual_machine_scale_set_extension" "omsagentlinux" {
count = var.log_analytics_workspace_name != null && var.os_flavor == "linux" ? 1 : 0
name = "OmsAgentForLinux"
publisher = "Microsoft.EnterpriseCloud.Monitoring"
type = "OmsAgentForLinux"
type_handler_version = "1.13"
auto_upgrade_minor_version = true
virtual_machine_scale_set_id = azurerm_linux_virtual_machine_scale_set.linux_vmss.0.id
settings = <<SETTINGS
{
"workspaceId": "${data.azurerm_log_analytics_workspace.logws.0.workspace_id}"
}
SETTINGS
protected_settings = <<PROTECTED_SETTINGS
{
"workspaceKey": "${data.azurerm_log_analytics_workspace.logws.0.primary_shared_key}"
}
PROTECTED_SETTINGS
}
#--------------------------------------
# azurerm monitoring diagnostics
#--------------------------------------
resource "azurerm_monitor_diagnostic_setting" "vmmsdiag" {
count = var.log_analytics_workspace_name != null && var.hub_storage_account_name != null ? 1 : 0
name = lower("${var.vmscaleset_name}-diag")
target_resource_id = var.os_flavor == "windows" ? azurerm_windows_virtual_machine_scale_set.winsrv_vmss.0.id : azurerm_linux_virtual_machine_scale_set.linux_vmss.0.id
storage_account_id = data.azurerm_storage_account.storeacc.0.id
log_analytics_workspace_id = data.azurerm_log_analytics_workspace.logws.0.id
metric {
category = "AllMetrics"
retention_policy {
enabled = false
}
}
}
resource "azurerm_monitor_diagnostic_setting" "nsg" {
count = var.log_analytics_workspace_name != null && var.hub_storage_account_name != null ? 1 : 0
name = lower("nsg-${var.vmscaleset_name}-diag")
target_resource_id = azurerm_network_security_group.nsg.id
storage_account_id = data.azurerm_storage_account.storeacc.0.id
log_analytics_workspace_id = data.azurerm_log_analytics_workspace.logws.0.id
dynamic "log" {
for_each = var.nsg_diag_logs
content {
category = log.value
enabled = true
retention_policy {
enabled = false
}
}
}
}
resource "azurerm_monitor_diagnostic_setting" "lb-pip" {
count = var.load_balancer_type == "public" && var.log_analytics_workspace_name != null && var.hub_storage_account_name != null ? 1 : 0
name = "${var.vmscaleset_name}-pip-diag"
target_resource_id = azurerm_public_ip.pip.0.id
storage_account_id = data.azurerm_storage_account.storeacc.0.id
log_analytics_workspace_id = data.azurerm_log_analytics_workspace.logws.0.id
dynamic "log" {
for_each = var.pip_diag_logs
content {
category = log.value
enabled = true
retention_policy {
enabled = false
}
}
}
metric {
category = "AllMetrics"
retention_policy {
enabled = false
}
}
}
#-----------------------------------------------------------
# Install IIS web server in every Instance in VM scale sets
#-----------------------------------------------------------
resource "azurerm_virtual_machine_scale_set_extension" "vmss_iis" {
count = var.intall_iis_server_on_instances && var.os_flavor == "windows" ? 1 : 0
name = "install-iis"
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
virtual_machine_scale_set_id = azurerm_windows_virtual_machine_scale_set.winsrv_vmss[0].id
settings = <<SETTINGS
{
"commandToExecute" : "powershell Install-WindowsFeature -name Web-Server -IncludeManagementTools"
}
SETTINGS
}