-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
main.tf
461 lines (409 loc) · 21.7 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
locals {
nsg_inbound_rules = { for idx, security_rule in var.nsg_inbound_rules : security_rule.name => {
idx : idx,
security_rule : security_rule,
}
}
vm_data_disks = { for idx, data_disk in var.data_disks : data_disk.name => {
idx : idx,
data_disk : data_disk,
}
}
}
#---------------------------------------------------------------
# Generates SSH2 key Pair for Linux VM's (Dev Environment only)
#---------------------------------------------------------------
resource "tls_private_key" "rsa" {
count = var.generate_admin_ssh_key ? 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_storage_account" "storeacc" {
count = var.storage_account_name != null ? 1 : 0
name = var.storage_account_name
resource_group_name = data.azurerm_resource_group.rg.name
}
resource "random_password" "passwd" {
count = (var.os_flavor == "linux" && var.disable_password_authentication == false && var.admin_password == null ? 1 : (var.os_flavor == "windows" && var.admin_password == null ? 1 : 0))
length = var.random_password_length
min_upper = 4
min_lower = 2
min_numeric = 4
special = false
keepers = {
admin_password = var.virtual_machine_name
}
}
#-----------------------------------
# Public IP for Virtual Machine
#-----------------------------------
resource "azurerm_public_ip" "pip" {
count = var.enable_public_ip_address == true ? var.instances_count : 0
name = lower("pip-vm-${var.virtual_machine_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 = var.public_ip_allocation_method
sku = var.public_ip_sku
sku_tier = var.public_ip_sku_tier
domain_name_label = var.domain_name_label
availability_zone = var.public_ip_availability_zone
tags = merge({ "ResourceName" = lower("pip-vm-${var.virtual_machine_name}-${data.azurerm_resource_group.rg.location}-0${count.index + 1}") }, var.tags, )
lifecycle {
ignore_changes = [
tags,
ip_tags,
]
}
}
#---------------------------------------
# Network Interface for Virtual Machine
#---------------------------------------
resource "azurerm_network_interface" "nic" {
count = var.instances_count
name = var.instances_count == 1 ? lower("nic-${format("vm%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")))}") : lower("nic-${format("vm%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1)}")
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
dns_servers = var.dns_servers
enable_ip_forwarding = var.enable_ip_forwarding
enable_accelerated_networking = var.enable_accelerated_networking
internal_dns_name_label = var.internal_dns_name_label
tags = merge({ "ResourceName" = var.instances_count == 1 ? lower("nic-${format("vm%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")))}") : lower("nic-${format("vm%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1)}") }, var.tags, )
ip_configuration {
name = lower("ipconig-${format("vm%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1)}")
primary = true
subnet_id = data.azurerm_subnet.snet.id
private_ip_address_allocation = var.private_ip_address_allocation_type
private_ip_address = var.private_ip_address_allocation_type == "Static" ? element(concat(var.private_ip_address, [""]), count.index) : null
public_ip_address_id = var.enable_public_ip_address == true ? element(concat(azurerm_public_ip.pip.*.id, [""]), count.index) : null
}
lifecycle {
ignore_changes = [
tags,
]
}
}
#----------------------------------------------------------------------------------------------------
# Proximity placement group for virtual machines, virtual machine scale sets and availability sets.
#----------------------------------------------------------------------------------------------------
resource "azurerm_proximity_placement_group" "appgrp" {
count = var.enable_proximity_placement_group ? 1 : 0
name = lower("proxigrp-${var.virtual_machine_name}-${data.azurerm_resource_group.rg.location}")
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
tags = merge({ "ResourceName" = lower("proxigrp-${var.virtual_machine_name}-${data.azurerm_resource_group.rg.location}") }, var.tags, )
lifecycle {
ignore_changes = [
tags,
]
}
}
#-----------------------------------------------------
# Manages an Availability Set for Virtual Machines.
#-----------------------------------------------------
resource "azurerm_availability_set" "aset" {
count = var.enable_vm_availability_set ? 1 : 0
name = lower("avail-${var.virtual_machine_name}-${data.azurerm_resource_group.rg.location}")
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
platform_fault_domain_count = var.platform_fault_domain_count
platform_update_domain_count = var.platform_update_domain_count
proximity_placement_group_id = var.enable_proximity_placement_group ? azurerm_proximity_placement_group.appgrp.0.id : null
managed = true
tags = merge({ "ResourceName" = lower("avail-${var.virtual_machine_name}-${data.azurerm_resource_group.rg.location}") }, var.tags, )
lifecycle {
ignore_changes = [
tags,
]
}
}
#---------------------------------------------------------------
# Network security group for Virtual Machine Network Interface
#---------------------------------------------------------------
resource "azurerm_network_security_group" "nsg" {
count = var.existing_network_security_group_id == null ? 1 : 0
name = lower("nsg_${var.virtual_machine_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.virtual_machine_name}_${data.azurerm_resource_group.rg.location}_in") }, var.tags, )
lifecycle {
ignore_changes = [
tags,
]
}
}
resource "azurerm_network_security_rule" "nsg_rule" {
for_each = { for k, v in local.nsg_inbound_rules : k => v if k != null }
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.0.name
depends_on = [azurerm_network_security_group.nsg]
}
resource "azurerm_network_interface_security_group_association" "nsgassoc" {
count = var.instances_count
network_interface_id = element(concat(azurerm_network_interface.nic.*.id, [""]), count.index)
network_security_group_id = var.existing_network_security_group_id == null ? azurerm_network_security_group.nsg.0.id : var.existing_network_security_group_id
}
#---------------------------------------
# Linux Virutal machine
#---------------------------------------
resource "azurerm_linux_virtual_machine" "linux_vm" {
count = var.os_flavor == "linux" ? var.instances_count : 0
name = var.instances_count == 1 ? substr(var.virtual_machine_name, 0, 64) : substr(format("%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1), 0, 64)
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
size = var.virtual_machine_size
admin_username = var.admin_username
admin_password = var.disable_password_authentication == false && var.admin_password == null ? element(concat(random_password.passwd.*.result, [""]), 0) : var.admin_password
disable_password_authentication = var.disable_password_authentication
network_interface_ids = [element(concat(azurerm_network_interface.nic.*.id, [""]), count.index)]
source_image_id = var.source_image_id != null ? var.source_image_id : null
provision_vm_agent = true
allow_extension_operations = true
dedicated_host_id = var.dedicated_host_id
custom_data = var.custom_data != null ? var.custom_data : null
availability_set_id = var.enable_vm_availability_set == true ? element(concat(azurerm_availability_set.aset.*.id, [""]), 0) : null
encryption_at_host_enabled = var.enable_encryption_at_host
proximity_placement_group_id = var.enable_proximity_placement_group ? azurerm_proximity_placement_group.appgrp.0.id : null
zone = var.vm_availability_zone
tags = merge({ "ResourceName" = var.instances_count == 1 ? var.virtual_machine_name : format("%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1) }, var.tags, )
dynamic "admin_ssh_key" {
for_each = var.disable_password_authentication ? [1] : []
content {
username = var.admin_username
public_key = var.admin_ssh_key_data == null ? 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 = var.os_disk_caching
disk_encryption_set_id = var.disk_encryption_set_id
disk_size_gb = var.disk_size_gb
write_accelerator_enabled = var.enable_os_disk_write_accelerator
name = var.os_disk_name
}
additional_capabilities {
ultra_ssd_enabled = var.enable_ultra_ssd_data_disk_storage_support
}
dynamic "identity" {
for_each = var.managed_identity_type != null ? [1] : []
content {
type = var.managed_identity_type
identity_ids = var.managed_identity_type == "UserAssigned" || var.managed_identity_type == "SystemAssigned, UserAssigned" ? var.managed_identity_ids : null
}
}
dynamic "boot_diagnostics" {
for_each = var.enable_boot_diagnostics ? [1] : []
content {
storage_account_uri = var.storage_account_name != null ? data.azurerm_storage_account.storeacc.0.primary_blob_endpoint : var.storage_account_uri
}
}
lifecycle {
ignore_changes = [
tags,
]
}
}
#---------------------------------------
# Windows Virutal machine
#---------------------------------------
resource "azurerm_windows_virtual_machine" "win_vm" {
count = var.os_flavor == "windows" ? var.instances_count : 0
name = var.instances_count == 1 ? substr(var.virtual_machine_name, 0, 15) : substr(format("%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1), 0, 15)
computer_name = var.instances_count == 1 ? substr(var.virtual_machine_name, 0, 15) : substr(format("%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1), 0, 15)
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
size = var.virtual_machine_size
admin_username = var.admin_username
admin_password = var.admin_password == null ? element(concat(random_password.passwd.*.result, [""]), 0) : var.admin_password
network_interface_ids = [element(concat(azurerm_network_interface.nic.*.id, [""]), count.index)]
source_image_id = var.source_image_id != null ? var.source_image_id : null
provision_vm_agent = true
allow_extension_operations = true
dedicated_host_id = var.dedicated_host_id
custom_data = var.custom_data != null ? var.custom_data : null
enable_automatic_updates = var.enable_automatic_updates
license_type = var.license_type
availability_set_id = var.enable_vm_availability_set == true ? element(concat(azurerm_availability_set.aset.*.id, [""]), 0) : null
encryption_at_host_enabled = var.enable_encryption_at_host
proximity_placement_group_id = var.enable_proximity_placement_group ? azurerm_proximity_placement_group.appgrp.0.id : null
patch_mode = var.patch_mode
zone = var.vm_availability_zone
timezone = var.vm_time_zone
tags = merge({ "ResourceName" = var.instances_count == 1 ? var.virtual_machine_name : format("%s%s", lower(replace(var.virtual_machine_name, "/[[:^alnum:]]/", "")), count.index + 1) }, var.tags, )
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 = var.os_disk_caching
disk_encryption_set_id = var.disk_encryption_set_id
disk_size_gb = var.disk_size_gb
write_accelerator_enabled = var.enable_os_disk_write_accelerator
name = var.os_disk_name
}
additional_capabilities {
ultra_ssd_enabled = var.enable_ultra_ssd_data_disk_storage_support
}
dynamic "identity" {
for_each = var.managed_identity_type != null ? [1] : []
content {
type = var.managed_identity_type
identity_ids = var.managed_identity_type == "UserAssigned" || var.managed_identity_type == "SystemAssigned, UserAssigned" ? var.managed_identity_ids : null
}
}
dynamic "winrm_listener" {
for_each = var.winrm_protocol != null ? [1] : []
content {
protocol = var.winrm_protocol
certificate_url = var.winrm_protocol == "Https" ? var.key_vault_certificate_secret_url : null
}
}
dynamic "additional_unattend_content" {
for_each = var.additional_unattend_content != null ? [1] : []
content {
content = var.additional_unattend_content
setting = var.additional_unattend_content_setting
}
}
dynamic "boot_diagnostics" {
for_each = var.enable_boot_diagnostics ? [1] : []
content {
storage_account_uri = var.storage_account_name != null ? data.azurerm_storage_account.storeacc.0.primary_blob_endpoint : var.storage_account_uri
}
}
lifecycle {
ignore_changes = [
tags,
patch_mode,
]
}
}
#---------------------------------------
# Virtual machine data disks
#---------------------------------------
resource "azurerm_managed_disk" "data_disk" {
for_each = local.vm_data_disks
name = "${var.virtual_machine_name}_DataDisk_${each.value.idx}"
resource_group_name = data.azurerm_resource_group.rg.name
location = data.azurerm_resource_group.rg.location
storage_account_type = lookup(each.value.data_disk, "storage_account_type", "StandardSSD_LRS")
create_option = "Empty"
disk_size_gb = each.value.data_disk.disk_size_gb
tags = merge({ "ResourceName" = "${var.virtual_machine_name}_DataDisk_${each.value.idx}" }, var.tags, )
lifecycle {
ignore_changes = [
tags,
]
}
}
resource "azurerm_virtual_machine_data_disk_attachment" "data_disk" {
for_each = local.vm_data_disks
managed_disk_id = azurerm_managed_disk.data_disk[each.key].id
virtual_machine_id = var.os_flavor == "windows" ? azurerm_windows_virtual_machine.win_vm[0].id : azurerm_linux_virtual_machine.linux_vm[0].id
lun = each.value.idx
caching = "ReadWrite"
}
#--------------------------------------------------------------
# Azure Log Analytics Workspace Agent Installation for windows
#--------------------------------------------------------------
resource "azurerm_virtual_machine_extension" "omsagentwin" {
count = var.deploy_log_analytics_agent && var.log_analytics_workspace_id != null && var.os_flavor == "windows" ? var.instances_count : 0
name = var.instances_count == 1 ? "OmsAgentForWindows" : format("%s%s", "OmsAgentForWindows", count.index + 1)
virtual_machine_id = azurerm_windows_virtual_machine.win_vm[count.index].id
publisher = "Microsoft.EnterpriseCloud.Monitoring"
type = "MicrosoftMonitoringAgent"
type_handler_version = "1.0"
auto_upgrade_minor_version = true
settings = <<SETTINGS
{
"workspaceId": "${var.log_analytics_customer_id}"
}
SETTINGS
protected_settings = <<PROTECTED_SETTINGS
{
"workspaceKey": "${var.log_analytics_workspace_primary_shared_key}"
}
PROTECTED_SETTINGS
}
#--------------------------------------------------------------
# Azure Log Analytics Workspace Agent Installation for Linux
#--------------------------------------------------------------
resource "azurerm_virtual_machine_extension" "omsagentlinux" {
count = var.deploy_log_analytics_agent && var.log_analytics_workspace_id != null && var.os_flavor == "linux" ? var.instances_count : 0
name = var.instances_count == 1 ? "OmsAgentForLinux" : format("%s%s", "OmsAgentForLinux", count.index + 1)
virtual_machine_id = azurerm_linux_virtual_machine.linux_vm[count.index].id
publisher = "Microsoft.EnterpriseCloud.Monitoring"
type = "OmsAgentForLinux"
type_handler_version = "1.13"
auto_upgrade_minor_version = true
settings = <<SETTINGS
{
"workspaceId": "${var.log_analytics_customer_id}"
}
SETTINGS
protected_settings = <<PROTECTED_SETTINGS
{
"workspaceKey": "${var.log_analytics_workspace_primary_shared_key}"
}
PROTECTED_SETTINGS
}
#--------------------------------------
# azurerm monitoring diagnostics
#--------------------------------------
resource "azurerm_monitor_diagnostic_setting" "nsg" {
count = var.existing_network_security_group_id == null && var.log_analytics_workspace_id != null ? 1 : 0
name = lower("nsg-${var.virtual_machine_name}-diag")
target_resource_id = azurerm_network_security_group.nsg.0.id
storage_account_id = var.storage_account_name != null ? data.azurerm_storage_account.storeacc.0.id : null
log_analytics_workspace_id = var.log_analytics_workspace_id
dynamic "log" {
for_each = var.nsg_diag_logs
content {
category = log.value
enabled = true
retention_policy {
enabled = false
days = 0
}
}
}
}