forked from Azure/terraform-azurerm-compute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
312 lines (268 loc) · 11.9 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
module "os" {
source = "./os"
vm_os_simple = var.vm_os_simple
}
data "azurerm_resource_group" "vm" {
name = var.resource_group_name
}
locals {
ssh_keys = compact(concat([var.ssh_key], var.extra_ssh_keys))
}
resource "random_id" "vm-sa" {
keepers = {
vm_hostname = var.vm_hostname
}
byte_length = 6
}
resource "azurerm_storage_account" "vm-sa" {
count = var.boot_diagnostics && ! var.boot_diagnostics_sa_managed ? 1 : 0
name = "bootdiag${lower(random_id.vm-sa.hex)}"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
account_tier = element(split("_", var.boot_diagnostics_sa_type), 0)
account_replication_type = element(split("_", var.boot_diagnostics_sa_type), 1)
tags = var.tags
}
resource "azurerm_virtual_machine" "vm-linux" {
count = ! contains(tolist([var.vm_os_simple, var.vm_os_offer]), "WindowsServer") && ! var.is_windows_image ? var.nb_instances : 0
name = "${var.vm_hostname}${count.index == 0 ? "" : "-${count.index+1}"}"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
availability_set_id = azurerm_availability_set.vm.id
vm_size = var.vm_size
network_interface_ids = [element(azurerm_network_interface.vm.*.id, count.index)]
delete_os_disk_on_termination = var.delete_os_disk_on_termination
delete_data_disks_on_termination = var.delete_data_disks_on_termination
dynamic identity {
for_each = length(var.identity_ids) == 0 && var.identity_type == "SystemAssigned" ? [var.identity_type] : []
content {
type = var.identity_type
}
}
dynamic identity {
for_each = length(var.identity_ids) > 0 || var.identity_type == "UserAssigned" ? [var.identity_type] : []
content {
type = var.identity_type
identity_ids = length(var.identity_ids) > 0 ? var.identity_ids : []
}
}
storage_image_reference {
id = var.vm_os_id
publisher = var.vm_os_id == "" ? coalesce(var.vm_os_publisher, module.os.calculated_value_os_publisher) : ""
offer = var.vm_os_id == "" ? coalesce(var.vm_os_offer, module.os.calculated_value_os_offer) : ""
sku = var.vm_os_id == "" ? coalesce(var.vm_os_sku, module.os.calculated_value_os_sku) : ""
version = var.vm_os_id == "" ? var.vm_os_version : ""
}
storage_os_disk {
name = "osdisk-${var.vm_hostname}${count.index == 0 ? "" : "-${count.index+1}"}"
create_option = "FromImage"
caching = "ReadWrite"
managed_disk_type = var.storage_account_type
}
dynamic storage_data_disk {
for_each = range(var.nb_data_disk)
content {
name = "${var.vm_hostname}-datadisk${count.index == 0 ? "" : "-${count.index+1}"}-${storage_data_disk.value}"
create_option = var.data_disk_create_option
lun = storage_data_disk.value
disk_size_gb = var.data_disk_size_gb
managed_disk_type = var.data_sa_type
}
}
dynamic storage_data_disk {
for_each = var.extra_disks
content {
name = "${var.vm_hostname}-extradisk${count.index == 0 ? "" : "-${count.index+1}"}-${storage_data_disk.value.name}"
create_option = var.data_disk_create_option
lun = storage_data_disk.key + var.nb_data_disk
disk_size_gb = storage_data_disk.value.size
managed_disk_type = var.data_sa_type
}
}
os_profile {
computer_name = "${var.vm_hostname}${count.index == 0 ? "" : "-${count.index+1}"}"
admin_username = var.admin_username
admin_password = var.admin_password
custom_data = var.custom_data
}
os_profile_linux_config {
disable_password_authentication = var.enable_ssh_key
dynamic ssh_keys {
for_each = var.enable_ssh_key ? local.ssh_keys : []
content {
path = "/home/${var.admin_username}/.ssh/authorized_keys"
key_data = file(ssh_keys.value)
}
}
dynamic ssh_keys {
for_each = var.enable_ssh_key ? var.ssh_key_values : []
content {
path = "/home/${var.admin_username}/.ssh/authorized_keys"
key_data = ssh_keys.value
}
}
}
dynamic "os_profile_secrets" {
for_each = var.os_profile_secrets
content {
source_vault_id = os_profile_secrets.value["source_vault_id"]
vault_certificates {
certificate_url = os_profile_secrets.value["certificate_url"]
}
}
}
tags = var.tags
boot_diagnostics {
enabled = var.boot_diagnostics
storage_uri = var.boot_diagnostics ? join(",", azurerm_storage_account.vm-sa.*.primary_blob_endpoint) : ""
}
}
resource "azurerm_virtual_machine" "vm-windows" {
count = (var.is_windows_image || contains(tolist([var.vm_os_simple, var.vm_os_offer]), "WindowsServer")) ? var.nb_instances : 0
name = "${var.vm_hostname}${count.index == 0 ? "" : "-${count.index+1}"}"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
availability_set_id = azurerm_availability_set.vm.id
vm_size = var.vm_size
network_interface_ids = [element(azurerm_network_interface.vm.*.id, count.index)]
delete_os_disk_on_termination = var.delete_os_disk_on_termination
delete_data_disks_on_termination = var.delete_data_disks_on_termination
license_type = var.license_type
dynamic identity {
for_each = length(var.identity_ids) == 0 && var.identity_type == "SystemAssigned" ? [var.identity_type] : []
content {
type = var.identity_type
}
}
dynamic identity {
for_each = length(var.identity_ids) > 0 || var.identity_type == "UserAssigned" ? [var.identity_type] : []
content {
type = var.identity_type
identity_ids = length(var.identity_ids) > 0 ? var.identity_ids : []
}
}
storage_image_reference {
id = var.vm_os_id
publisher = var.vm_os_id == "" ? coalesce(var.vm_os_publisher, module.os.calculated_value_os_publisher) : ""
offer = var.vm_os_id == "" ? coalesce(var.vm_os_offer, module.os.calculated_value_os_offer) : ""
sku = var.vm_os_id == "" ? coalesce(var.vm_os_sku, module.os.calculated_value_os_sku) : ""
version = var.vm_os_id == "" ? var.vm_os_version : ""
}
storage_os_disk {
name = "${var.vm_hostname}-osdisk${count.index == 0 ? "" : "-${count.index+1}"}"
create_option = "FromImage"
caching = "ReadWrite"
managed_disk_type = var.storage_account_type
}
dynamic storage_data_disk {
for_each = range(var.nb_data_disk)
content {
name = "${var.vm_hostname}-datadisk${count.index == 0 ? "" : "-${count.index+1}"}-${storage_data_disk.value}"
create_option = "Empty"
lun = storage_data_disk.value
disk_size_gb = var.data_disk_size_gb
managed_disk_type = var.data_sa_type
}
}
dynamic storage_data_disk {
for_each = var.extra_disks
content {
name = "${var.vm_hostname}-extradisk${count.index == 0 ? "" : "-${count.index+1}"}-${storage_data_disk.value.name}"
create_option = "Empty"
lun = storage_data_disk.key + var.nb_data_disk
disk_size_gb = storage_data_disk.value.size
managed_disk_type = var.data_sa_type
}
}
os_profile {
computer_name = "${var.vm_hostname}${count.index == 0 ? "" : "-${count.index+1}"}"
admin_username = var.admin_username
admin_password = var.admin_password
}
tags = var.tags
os_profile_windows_config {
# can we add timezone and auto-updates in here ?
# or just manage this post-install ?
provision_vm_agent = true
}
dynamic "os_profile_secrets" {
for_each = var.os_profile_secrets
content {
source_vault_id = os_profile_secrets.value["source_vault_id"]
vault_certificates {
certificate_url = os_profile_secrets.value["certificate_url"]
certificate_store = os_profile_secrets.value["certificate_store"]
}
}
}
boot_diagnostics {
enabled = var.boot_diagnostics
storage_uri = var.boot_diagnostics ? join(",", azurerm_storage_account.vm-sa.*.primary_blob_endpoint) : ""
}
}
resource "azurerm_availability_set" "vm" {
name = "${var.vm_hostname}-avset"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
platform_fault_domain_count = 2
platform_update_domain_count = 2
managed = true
tags = var.tags
}
resource "azurerm_public_ip" "vm" {
count = var.nb_public_ip
name = "${var.vm_hostname}-pip${count.index == 0 ? "" : "-${count.index+1}"}"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
allocation_method = var.allocation_method
sku = var.public_ip_sku
domain_name_label = element(var.public_ip_dns, count.index)
tags = var.tags
}
// Dynamic public ip address will be got after it's assigned to a vm
data "azurerm_public_ip" "vm" {
count = var.nb_public_ip
name = azurerm_public_ip.vm[count.index].name
resource_group_name = data.azurerm_resource_group.vm.name
depends_on = [azurerm_virtual_machine.vm-linux, azurerm_virtual_machine.vm-windows]
}
resource "azurerm_network_security_group" "vm" {
name = "${var.vm_hostname}-nsg"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
tags = var.tags
}
resource "azurerm_network_security_rule" "vm" {
count = var.remote_port != "" ? 1 : 0
name = "allow_remote_${coalesce(var.remote_port, module.os.calculated_remote_port)}_in_all"
resource_group_name = data.azurerm_resource_group.vm.name
description = "Allow remote protocol in from all locations"
priority = 101
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = coalesce(var.remote_port, module.os.calculated_remote_port)
source_address_prefixes = var.source_address_prefixes
destination_address_prefix = "*"
network_security_group_name = azurerm_network_security_group.vm.name
}
resource "azurerm_network_interface" "vm" {
count = var.nb_instances
name = "${var.vm_hostname}-nic${count.index == 0 ? "" : "-${count.index+1}"}"
resource_group_name = data.azurerm_resource_group.vm.name
location = coalesce(var.location, data.azurerm_resource_group.vm.location)
enable_accelerated_networking = var.enable_accelerated_networking
ip_configuration {
name = "${var.vm_hostname}-ip${count.index == 0 ? "" : "-${count.index+1}"}"
subnet_id = var.vnet_subnet_id
private_ip_address_allocation = "Dynamic"
public_ip_address_id = length(azurerm_public_ip.vm.*.id) > 0 ? element(concat(azurerm_public_ip.vm.*.id, tolist([""])), count.index) : ""
}
tags = var.tags
}
resource "azurerm_network_interface_security_group_association" "test" {
count = var.nb_instances
network_interface_id = azurerm_network_interface.vm[count.index].id
network_security_group_id = azurerm_network_security_group.vm.id
}