-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
314 lines (258 loc) · 11.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
# Limits:
# 1. This module is used to create n vms with m data disks each.
# ATTENTION: Data disks cannot be added or removed after the first deploy if the number of vms is more
# than one (the list of data disks will be regenerated), but is still possible to change their sizes.
# Tests:
# 1. Change data disk 0 size: OK (update in place)
# 2. Change data disk 1 size: OK (update in place)
# 3. Change data disk 2 size: OK (update in place)
# 4. Add data disk 3: KO (bad change in data disks list! NOT DO!)
# 5. Add a vm at the end of the list virtual_machine_names: OK (all objects created correctly)
# 6. Delete a vm not last in the list virtual_machine_names: KO (bad change in objects lists! NOT DO!)
# 7. Delete the last vm the list virtual_machine_names: OK (remove the right objects)
locals {
# Used to create nic to asg associations
net_ints_2_app_sec_grps = flatten([
for net_int in azurerm_network_interface.nic[*].id: [
for app_sec_grp in var.application_security_group_ids : {
network_interface_id = net_int
application_security_group_id = app_sec_grp
}
]
])
# Used to create n data_disk (number of vms * number of data disks)
vms_2_data_disks = flatten([
for vm in var.virtual_machine_names: [
for data_disk in var.data_disks : {
data_disk_name = "${vm}-datadisk-${data_disk.data_disk_lun}"
data_disk_sa_type = data_disk.data_disk_sa_type
data_disk_size_gb = data_disk.data_disk_size_gb
data_disk_lun = data_disk.data_disk_lun
data_disk_caching = data_disk.data_disk_caching
}
]
])
# Create an array where each element contains interface data
net_ints_data = flatten([
for net_int in azurerm_network_interface.nic[*]: [
{
network_interface_id = net_int.id
ip_conf_name = net_int.ip_configuration[0].name
}
]
])
}
# Network interface
resource "azurerm_network_interface" "nic" {
count = var.virtual_machine_instances
name = "${var.virtual_machine_names[count.index]}-nic"
location = var.location
resource_group_name = var.resource_group
enable_accelerated_networking = var.enable_accelerated_networking
enable_ip_forwarding = var.enable_ip_forwarding
dns_servers = var.dns_servers
ip_configuration {
name = "${var.virtual_machine_names[count.index]}-ip"
subnet_id = var.subnet_id
private_ip_address_allocation = var.private_ip_address_allocation
private_ip_address = length(var.private_ip_addresses) > 0 ? var.private_ip_addresses[count.index] : ""
public_ip_address_id = length(var.public_ip_addresses) > 0 ? var.public_ip_addresses[count.index] : null
}
tags = var.tags
}
# Association nics to asg ids
resource "azurerm_network_interface_application_security_group_association" "nic2asg" {
count = length(var.application_security_group_ids) > 0 ? length(local.net_ints_2_app_sec_grps) : 0
network_interface_id = local.net_ints_2_app_sec_grps[count.index].network_interface_id
application_security_group_id = local.net_ints_2_app_sec_grps[count.index].application_security_group_id
}
# Association nics to nsg id
resource "azurerm_network_interface_security_group_association" "nic2nsg" {
count = var.use_network_security_group ? var.virtual_machine_instances : 0
network_interface_id = azurerm_network_interface.nic[count.index].id
network_security_group_id = var.network_security_group_id
}
# Virtual machine Linux
resource "azurerm_linux_virtual_machine" "vm-linux" {
count = !var.is_windows ? var.virtual_machine_instances : 0
name = "${var.virtual_machine_names[count.index]}-vm"
location = var.location
resource_group_name = var.resource_group
availability_set_id = var.availability_set_enabled ? var.availability_set_id : null
network_interface_ids = [azurerm_network_interface.nic[count.index].id]
size = var.vm_size
source_image_reference {
publisher = var.vm_os_publisher
offer = var.vm_os_offer
sku = var.vm_os_sku
version = var.vm_os_version
}
os_disk {
name = "${var.virtual_machine_names[count.index]}-osdisk"
caching = "ReadWrite"
storage_account_type = var.storage_account_type
}
computer_name = var.virtual_machine_names[count.index]
disable_password_authentication = false
admin_username = var.admin_username
admin_password = var.admin_password
boot_diagnostics {
storage_account_uri = var.boot_diagnostics ? var.storage_account_boot_diagnostics : ""
}
zone = var.availability_zones_enabled ? (count.index % var.availability_zones_number) + 1 : null
tags = var.tags
}
# Virtual machine Windows
resource "azurerm_windows_virtual_machine" "vm-windows" {
count = var.is_windows ? var.virtual_machine_instances : 0
name = "${var.virtual_machine_names[count.index]}-vm"
location = var.location
resource_group_name = var.resource_group
availability_set_id = var.availability_set_enabled ? var.availability_set_id : null
network_interface_ids = [azurerm_network_interface.nic[count.index].id]
size = var.vm_size
license_type = var.license_type
source_image_reference {
publisher = var.vm_os_publisher
offer = var.vm_os_offer
sku = var.vm_os_sku
version = var.vm_os_version
}
os_disk {
name = "${var.virtual_machine_names[count.index]}-osdisk"
caching = "ReadWrite"
storage_account_type = var.storage_account_type
}
dynamic "identity" {
for_each = var.identity_type == "" ? toset([]) : toset([1])
content {
type = var.identity_type
identity_ids = var.identity_ids
}
}
computer_name = var.virtual_machine_names[count.index]
admin_username = var.admin_username
admin_password = var.admin_password
provision_vm_agent = true
boot_diagnostics {
storage_account_uri = var.boot_diagnostics ? var.storage_account_boot_diagnostics : ""
}
zone = var.availability_zones_enabled ? (count.index % var.availability_zones_number) + 1 : null
tags = var.tags
}
# Create optionals data disks
resource "azurerm_managed_disk" "vm-data-disk" {
count = length(local.vms_2_data_disks) > 0 ? length(local.vms_2_data_disks) : 0
name = local.vms_2_data_disks[count.index].data_disk_name
location = var.location
resource_group_name = var.resource_group
storage_account_type = local.vms_2_data_disks[count.index].data_disk_sa_type
create_option = "Empty"
disk_size_gb = local.vms_2_data_disks[count.index].data_disk_size_gb
zone = var.availability_zones_enabled ? ((floor(count.index / length(var.data_disks))) % var.availability_zones_number) + 1 : null
tags = var.tags
}
# Data disk association
# Linux vm
resource "azurerm_virtual_machine_data_disk_attachment" "data-disk2vm-linux" {
count = (length(local.vms_2_data_disks) > 0) && !var.is_windows ? length(local.vms_2_data_disks) : 0
managed_disk_id = azurerm_managed_disk.vm-data-disk[count.index].id
virtual_machine_id = azurerm_linux_virtual_machine.vm-linux[floor(count.index / length(var.data_disks))].id
lun = local.vms_2_data_disks[count.index].data_disk_lun
caching = local.vms_2_data_disks[count.index].data_disk_caching
}
# Windows vm
resource "azurerm_virtual_machine_data_disk_attachment" "data-disk2vm-windows" {
count = (length(local.vms_2_data_disks) > 0) && var.is_windows ? length(local.vms_2_data_disks) : 0
managed_disk_id = azurerm_managed_disk.vm-data-disk[count.index].id
virtual_machine_id = azurerm_windows_virtual_machine.vm-windows[floor(count.index / length(var.data_disks))].id
lun = local.vms_2_data_disks[count.index].data_disk_lun
caching = local.vms_2_data_disks[count.index].data_disk_caching
}
# JsonADDomainExtension extension
resource "azurerm_virtual_machine_extension" "vm-windows-joinext" {
count = var.join && var.is_windows ? var.virtual_machine_instances : 0
name = "${var.virtual_machine_names[count.index]}-joinext"
virtual_machine_id = azurerm_windows_virtual_machine.vm-windows[count.index].id
publisher = "Microsoft.Compute"
type = "JsonADDomainExtension"
type_handler_version = "1.3"
settings = <<SETTINGS
{
"Name": "${var.windows_domain_name}",
"User": "${var.windows_domain_username}",
"Restart": "false",
"Options": "3"
}
SETTINGS
protected_settings = <<PROTECTED_SETTINGS
{
"Password": "${var.windows_domain_password}"
}
PROTECTED_SETTINGS
tags = var.tags
}
# AADLoginForWindows extension
resource "azurerm_virtual_machine_extension" "vm-windows-aadjoinext" {
count = var.aad_login_for_windows && var.is_windows ? var.virtual_machine_instances : 0
name = "${var.virtual_machine_names[count.index]}-aadjoinext"
virtual_machine_id = azurerm_windows_virtual_machine.vm-windows[count.index].id
publisher = "Microsoft.Azure.ActiveDirectory"
type = "AADLoginForWindows"
type_handler_version = "1.0"
auto_upgrade_minor_version = true
settings = <<SETTINGS
{
"mdmId": ""
}
SETTINGS
tags = var.tags
}
# Custom script extension
resource "azurerm_virtual_machine_extension" "vm-windows-cse" {
count = var.customize && var.is_windows ? var.virtual_machine_instances : 0
name = "${var.virtual_machine_names[count.index]}-cse"
virtual_machine_id = azurerm_windows_virtual_machine.vm-windows[count.index].id
publisher = "Microsoft.Compute"
type = "CustomScriptExtension"
type_handler_version = "1.9"
# Works only if container custom-script-extension has read permission by anonymous (access level container)
settings = <<SETTINGS
{
"fileUris": [
"${var.windows_cs_file_uri}"
],
"commandToExecute": "${var.windows_cs_command}"
}
SETTINGS
tags = var.tags
}
resource "azurerm_virtual_machine_extension" "vm-linux-cse" {
count = var.customize && !var.is_windows ? var.virtual_machine_instances : 0
name = "${var.virtual_machine_names[count.index]}-cse"
virtual_machine_id = azurerm_linux_virtual_machine.vm-linux[count.index].id
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type_handler_version = "2.0"
# Works only if container custom-script-extension has read permission by anonymous (access level container)
settings = <<SETTINGS
{
"fileUris": [
"${var.linux_cs_file_uri}"
],
"commandToExecute": "${var.linux_cs_command}"
}
SETTINGS
tags = var.tags
}
# End Custom script extension
# Backup policy for the virtual machine
resource "azurerm_backup_protected_vm" "rs-protected-vm" {
count = var.backup_enabled ? var.virtual_machine_instances : 0
resource_group_name = var.recovery_vault_resource_group
recovery_vault_name = var.recovery_vault_name
source_vm_id = var.is_windows ? azurerm_windows_virtual_machine.vm-windows[count.index].id : azurerm_linux_virtual_machine.vm-linux[count.index].id
backup_policy_id = var.backup_policy_id
# TO TRY! It's seem tags don't work on this resource
# tags = "${var.tags}"
}