generated from cyber-scot/terraform-module-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
354 lines (287 loc) · 15.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
resource "azurerm_public_ip" "pip" {
for_each = { for vm in var.windows_vms : vm.name => vm if vm.public_ip_sku != null }
name = each.value.pip_name != null ? each.value.pip_name : "pip-${each.value.name}"
location = each.value.location
resource_group_name = each.value.rg_name
allocation_method = each.value.allocation_method
domain_name_label = try(each.value.pip_custom_dns_label, each.value.computer_name, null)
sku = each.value.public_ip_sku
lifecycle {
ignore_changes = [domain_name_label]
}
}
resource "azurerm_network_interface" "nic" {
for_each = { for vm in var.windows_vms : vm.name => vm }
name = each.value.nic_name != null ? each.value.nic_name : "nic-${each.value.name}"
location = each.value.location
resource_group_name = each.value.rg_name
enable_accelerated_networking = each.value.enable_accelerated_networking
ip_configuration {
name = each.value.nic_ipconfig_name != null ? each.value.nic_ipconfig_name : "nic-ipcon-${each.value.name}"
primary = true
private_ip_address_allocation = each.value.static_private_ip == null ? "Dynamic" : "Static"
private_ip_address = each.value.static_private_ip
public_ip_address_id = lookup(each.value, "public_ip_sku", null) == null ? null : azurerm_public_ip.pip[each.key].id
subnet_id = each.value.subnet_id
}
tags = each.value.tags
timeouts {
create = "5m"
delete = "10m"
}
}
resource "azurerm_application_security_group" "asg" {
for_each = { for vm in var.windows_vms : vm.name => vm if vm.create_asg == true }
name = each.value.asg_name != null ? each.value.asg_name : "asg-${each.value.name}"
location = each.value.location
resource_group_name = each.value.rg_name
tags = each.value.tags
}
resource "azurerm_network_interface_application_security_group_association" "asg_association" {
for_each = { for vm in var.windows_vms : vm.name => vm }
network_interface_id = azurerm_network_interface.nic[each.key].id
application_security_group_id = each.value.asg_id != null ? each.value.asg_id : azurerm_application_security_group.asg[each.key].id
}
resource "random_integer" "zone" {
for_each = { for vm in var.windows_vms : vm.name => vm if vm.availability_zone == "random" }
min = 1
max = 3
}
locals {
sanitized_names = { for vm in var.windows_vms : vm.name => upper(replace(replace(replace(vm.name, " ", ""), "-", ""), "_", "")) }
netbios_names = { for key, value in local.sanitized_names : key => substr(value, 0, min(length(value), 15)) }
random_zones = { for idx, vm in var.windows_vms : vm.name => vm.availability_zone == "random" ? tostring(idx + 1) : vm.availability_zone }
}
resource "azurerm_windows_virtual_machine" "this" {
for_each = { for vm in var.windows_vms : vm.name => vm }
// Forces acceptance of marketplace terms before creating a VM
depends_on = [
azurerm_marketplace_agreement.plan_acceptance_simple,
azurerm_marketplace_agreement.plan_acceptance_custom
]
name = each.value.name
resource_group_name = each.value.rg_name
location = each.value.location
network_interface_ids = [azurerm_network_interface.nic[each.key].id]
license_type = each.value.license_type
patch_mode = each.value.patch_mode
enable_automatic_updates = each.value.enable_automatic_updates
computer_name = each.value.computer_name != null ? each.value.computer_name : local.netbios_names[each.key]
admin_username = each.value.admin_username
admin_password = each.value.admin_password
size = each.value.vm_size
source_image_id = try(each.value.use_custom_image, null) == true ? each.value.custom_source_image_id : null
zone = local.random_zones[each.key]
availability_set_id = each.value.availability_set_id
virtual_machine_scale_set_id = each.value.virtual_machine_scale_set_id
timezone = each.value.timezone
user_data = each.value.user_data
custom_data = each.value.custom_data
tags = each.value.tags
encryption_at_host_enabled = each.value.enable_encryption_at_host
allow_extension_operations = each.value.allow_extension_operations
provision_vm_agent = each.value.provision_vm_agent
dynamic "additional_capabilities" {
for_each = each.value.ultra_ssd_enabled ? [1] : []
content {
ultra_ssd_enabled = each.value.ultra_ssd_enabled
}
}
# Use simple image
dynamic "source_image_reference" {
for_each = try(each.value.use_simple_image, null) == true && try(each.value.use_simple_image_with_plan, null) == false && try(each.value.use_custom_image, null) == false ? [1] : []
content {
publisher = coalesce(each.value.vm_os_publisher, module.os_calculator[each.value.name].calculated_value_os_publisher)
offer = coalesce(each.value.vm_os_offer, module.os_calculator[each.value.name].calculated_value_os_offer)
sku = coalesce(each.value.vm_os_sku, module.os_calculator[each.value.name].calculated_value_os_sku)
version = coalesce(each.value.vm_os_version, "latest")
}
}
# Use custom image reference
dynamic "source_image_reference" {
for_each = try(each.value.use_simple_image, null) == false && try(each.value.use_simple_image_with_plan, null) == false && try(length(each.value.source_image_reference), 0) > 0 && try(length(each.value.plan), 0) == 0 && try(each.value.use_custom_image, null) == false ? [1] : []
content {
publisher = lookup(each.value.source_image_reference, "publisher", null)
offer = lookup(each.value.source_image_reference, "offer", null)
sku = lookup(each.value.source_image_reference, "sku", null)
version = lookup(each.value.source_image_reference, "version", null)
}
}
dynamic "source_image_reference" {
for_each = try(each.value.use_simple_image, null) == true && try(each.value.use_simple_image_with_plan, null) == true && try(each.value.use_custom_image, null) == false ? [1] : []
content {
publisher = coalesce(each.value.vm_os_publisher, module.os_calculator_with_plan[each.value.name].calculated_value_os_publisher)
offer = coalesce(each.value.vm_os_offer, module.os_calculator_with_plan[each.value.name].calculated_value_os_offer)
sku = coalesce(each.value.vm_os_sku, module.os_calculator_with_plan[each.value.name].calculated_value_os_sku)
version = coalesce(each.value.vm_os_version, "latest")
}
}
dynamic "plan" {
for_each = try(each.value.use_simple_image, null) == false && try(each.value.use_simple_image_with_plan, null) == false && try(length(each.value.plan), 0) > 0 && try(each.value.use_custom_image, null) == false ? [1] : []
content {
name = coalesce(each.value.vm_os_sku, module.os_calculator_with_plan[each.value.name].calculated_value_os_sku)
product = coalesce(each.value.vm_os_offer, module.os_calculator_with_plan[each.value.name].calculated_value_os_offer)
publisher = coalesce(each.value.vm_os_publisher, module.os_calculator_with_plan[each.value.name].calculated_value_os_publisher)
}
}
dynamic "plan" {
for_each = try(each.value.use_simple_image, null) == false && try(each.value.use_simple_image_with_plan, null) == false && try(length(each.value.plan), 0) > 0 && try(each.value.use_custom_image, null) == false ? [1] : []
content {
name = lookup(each.value.plan, "name", null)
product = lookup(each.value.plan, "product", null)
publisher = lookup(each.value.plan, "publisher", null)
}
}
dynamic "identity" {
for_each = each.value.identity_type == "SystemAssigned" ? [each.value.identity_type] : []
content {
type = each.value.identity_type
}
}
dynamic "identity" {
for_each = each.value.identity_type == "SystemAssigned, UserAssigned" ? [each.value.identity_type] : []
content {
type = each.value.identity_type
identity_ids = try(each.value.identity_ids, [])
}
}
dynamic "identity" {
for_each = each.value.identity_type == "UserAssigned" ? [each.value.identity_type] : []
content {
type = each.value.identity_type
identity_ids = length(try(each.value.identity_ids, [])) > 0 ? each.value.identity_ids : []
}
}
priority = try(each.value.spot_instance, false) ? "Spot" : "Regular"
max_bid_price = try(each.value.spot_instance, false) ? each.value.spot_instance_max_bid_price : null
eviction_policy = try(each.value.spot_instance, false) ? each.value.spot_instance_eviction_policy : null
os_disk {
name = each.value.os_disk.name != null ? each.value.os_disk.name : "osdisk-${each.value.name}"
caching = each.value.os_disk.caching
storage_account_type = each.value.os_disk.os_disk_type
disk_size_gb = each.value.os_disk.disk_size_gb
disk_encryption_set_id = each.value.os_disk.disk_encryption_set_id
secure_vm_disk_encryption_set_id = each.value.os_disk.secure_vm_disk_encryption_set_id
security_encryption_type = each.value.os_disk.security_encryption_type
write_accelerator_enabled = each.value.os_disk.write_accelerator_enabled
dynamic "diff_disk_settings" {
for_each = each.value.os_disk.diff_disk_settings != null ? [each.value.os_disk.diff_disk_settings] : []
content {
option = diff_disk_settings.value.option
}
}
}
dynamic "boot_diagnostics" {
for_each = each.value.boot_diagnostics_storage_account_uri != null ? [each.value.boot_diagnostics_storage_account_uri] : [null]
content {
storage_account_uri = boot_diagnostics.value
}
}
dynamic "additional_unattend_content" {
for_each = each.value.additional_unattend_content != null ? each.value.additional_unattend_content : []
content {
content = additional_unattend_content.value.content
setting = additional_unattend_content.value.setting
}
}
dynamic "secret" {
for_each = each.value.secrets != null ? each.value.secrets : []
content {
key_vault_id = secret.value.key_vault_id
dynamic "certificate" {
for_each = secret.value.certificates
content {
store = certificate.value.store
url = certificate.value.url
}
}
}
}
dynamic "termination_notification" {
for_each = each.value.termination_notification != null ? [each.value.termination_notification] : []
content {
enabled = termination_notification.value.enabled
timeout = lookup(termination_notification.value, "timeout", "PT5M")
}
}
dynamic "winrm_listener" {
for_each = each.value.winrm_listener != null ? each.value.winrm_listener : []
content {
protocol = winrm_listener.value.protocol
certificate_url = winrm_listener.value.certificate_url
}
}
}
module "os_calculator" {
source = "cyber-scot/windows-virtual-machine-os-sku-calculator/azurerm"
for_each = { for vm in var.windows_vms : vm.name => vm if try(vm.use_simple_image, null) == true }
vm_os_simple = each.value.vm_os_simple
}
module "os_calculator_with_plan" {
source = "cyber-scot/windows-virtual-machine-os-sku-with-plan-calculator/azurerm"
for_each = { for vm in var.windows_vms : vm.name => vm if try(vm.use_simple_image_with_plan, null) == true }
vm_os_simple = each.value.vm_os_simple
}
resource "azurerm_marketplace_agreement" "plan_acceptance_simple" {
for_each = { for vm in var.windows_vms : vm.name => vm if try(vm.use_simple_image_with_plan, null) == true && try(vm.accept_plan, null) == true && try(vm.use_custom_image, null) == false }
publisher = coalesce(each.value.vm_os_publisher, module.os_calculator_with_plan[each.key].calculated_value_os_publisher)
offer = coalesce(each.value.vm_os_offer, module.os_calculator_with_plan[each.key].calculated_value_os_offer)
plan = coalesce(each.value.vm_os_sku, module.os_calculator_with_plan[each.key].calculated_value_os_sku)
}
resource "azurerm_marketplace_agreement" "plan_acceptance_custom" {
for_each = { for vm in var.windows_vms : vm.name => vm if try(vm.use_custom_image_with_plan, null) == true && try(vm.accept_plan, null) == true && try(vm.use_custom_image, null) == true }
publisher = lookup(each.value.plan, "publisher", null)
offer = lookup(each.value.plan, "product", null)
plan = lookup(each.value.plan, "name", null)
}
resource "azurerm_virtual_machine_extension" "windows_vm_inline_command" {
for_each = { for vm in var.windows_vms : vm.name => vm if try(vm.run_vm_command.inline, null) != null }
depends_on = [azurerm_windows_virtual_machine.this]
name = each.value.run_vm_command.extension_name != null ? each.value.run_vm_command.extension_name : "run-command-${each.value.name}"
publisher = "Microsoft.CPlat.Core"
type = "RunCommandWindows"
type_handler_version = "1.1"
auto_upgrade_minor_version = true
settings = jsonencode({
script = tolist([each.value.run_vm_command.inline])
})
tags = each.value.tags
virtual_machine_id = azurerm_windows_virtual_machine.this[each.key].id
lifecycle {
ignore_changes = all
}
}
resource "azurerm_virtual_machine_extension" "windows_vm_file_command" {
for_each = { for vm in var.windows_vms : vm.name => vm if try(vm.run_vm_command.script_file, null) != null }
depends_on = [azurerm_windows_virtual_machine.this]
name = each.value.run_vm_command.extension_name != null ? each.value.run_vm_command.extension_name : "run-command-file-${each.value.name}"
publisher = "Microsoft.CPlat.Core"
type = "RunCommandWindows"
type_handler_version = "1.1"
auto_upgrade_minor_version = true
settings = jsonencode({
script = compact(tolist([each.value.run_vm_command.script_file]))
})
tags = each.value.tags
virtual_machine_id = azurerm_windows_virtual_machine.this[each.key].id
lifecycle {
ignore_changes = all
}
}
resource "azurerm_virtual_machine_extension" "windows_vm_uri_command" {
for_each = { for vm in var.windows_vms : vm.name => vm if try(vm.run_vm_command.script_uri, null) != null }
depends_on = [azurerm_windows_virtual_machine.this]
name = each.value.run_vm_command.extension_name != null ? each.value.run_vm_command.extension_name : "run-command-uri-${each.value.name}"
publisher = "Microsoft.CPlat.Core"
type = "RunCommandWindows"
type_handler_version = "1.1"
auto_upgrade_minor_version = true
settings = jsonencode({
script = compact(tolist([each.value.run_vm_command.script_uri]))
})
tags = each.value.tags
virtual_machine_id = azurerm_windows_virtual_machine.this[each.key].id
lifecycle {
ignore_changes = all
}
}