-
Notifications
You must be signed in to change notification settings - Fork 0
/
r-vm.tf
222 lines (196 loc) · 8.96 KB
/
r-vm.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
locals {
virtual_machine = (local.is_linux ? azurerm_linux_virtual_machine.this[0] :
(local.is_windows ? azurerm_windows_virtual_machine.this[0] : null)
)
}
# trivy:ignore:avd-azu-0039
resource "azurerm_linux_virtual_machine" "this" {
count = local.is_linux ? 1 : 0
name = var.name
location = var.location
resource_group_name = var.resource_group_name
tags = merge(var.tags, var.tags_virtual_machine)
admin_password = local.admin_password
admin_username = var.admin_username
allow_extension_operations = var.allow_extension_operations
availability_set_id = var.availability_set_id
bypass_platform_safety_checks_on_user_schedule_enabled = var.bypass_platform_safety_checks_on_user_schedule_enabled
computer_name = var.computer_name
custom_data = var.custom_data
disable_password_authentication = !strcontains(var.authentication_type, "Password")
encryption_at_host_enabled = var.encryption_at_host_enabled
license_type = var.license_type
network_interface_ids = local.network_interface_ids
patch_assessment_mode = var.patch_assessment_mode
patch_mode = var.patch_mode
provision_vm_agent = var.provision_vm_agent
proximity_placement_group_id = var.proximity_placement_group_id
secure_boot_enabled = var.secure_boot_enabled
size = var.size
source_image_id = var.source_image_id
virtual_machine_scale_set_id = var.virtual_machine_scale_set_id
vtpm_enabled = var.vtpm_enabled
zone = var.zone
dynamic "additional_capabilities" {
for_each = var.additional_capabilities[*]
content {
ultra_ssd_enabled = var.additional_capabilities.ultra_ssd_enabled
hibernation_enabled = var.additional_capabilities.hibernation_enabled
}
}
dynamic "admin_ssh_key" {
for_each = var.authentication_type == "SSH" ? [true] : []
content {
username = var.admin_username
public_key = local.admin_ssh_public_key
}
}
dynamic "boot_diagnostics" {
for_each = var.boot_diagnostics.enabled ? [true] : []
content {
storage_account_uri = var.boot_diagnostics.storage_account_uri
}
}
dynamic "identity" {
for_each = try(var.identity.type, null) != null ? [true] : []
content {
type = var.identity.type
identity_ids = try(coalescelist(var.identity.identity_ids, azurerm_user_assigned_identity.this[*].id), null)
}
}
os_disk {
caching = var.os_disk.caching
disk_encryption_set_id = var.os_disk.disk_encryption_set_id
disk_size_gb = var.os_disk.disk_size_gb
name = coalesce(var.os_disk.name, "osdisk-${trimprefix(var.name, "vm-")}")
secure_vm_disk_encryption_set_id = var.os_disk.secure_vm_disk_encryption_set_id
security_encryption_type = var.os_disk.security_encryption_type
storage_account_type = var.os_disk.storage_account_type
write_accelerator_enabled = var.os_disk.write_accelerator_enabled
}
dynamic "plan" {
for_each = var.plan[*]
content {
name = var.plan.name
product = var.plan.product
publisher = var.plan.publisher
}
}
dynamic "source_image_reference" {
for_each = var.source_image_id == null ? [true] : []
content {
publisher = local.image.publisher
offer = local.image.offer
sku = local.image.sku
version = local.image.version
}
}
lifecycle {
ignore_changes = [
# Gallery applications are installed via Azure policy.
gallery_application,
# Disk name may change during restore or other manual operations.
os_disk[0].name,
]
}
}
resource "azurerm_windows_virtual_machine" "this" {
count = local.is_windows ? 1 : 0
name = var.name
location = var.location
resource_group_name = var.resource_group_name
tags = merge(var.tags, var.tags_virtual_machine)
admin_password = local.admin_password
admin_username = var.admin_username
allow_extension_operations = var.allow_extension_operations
availability_set_id = var.availability_set_id
bypass_platform_safety_checks_on_user_schedule_enabled = var.bypass_platform_safety_checks_on_user_schedule_enabled
computer_name = var.computer_name
custom_data = var.custom_data
enable_automatic_updates = var.enable_automatic_updates
encryption_at_host_enabled = var.encryption_at_host_enabled
hotpatching_enabled = var.hotpatching_enabled
license_type = var.license_type
network_interface_ids = local.network_interface_ids
patch_assessment_mode = var.patch_assessment_mode
patch_mode = var.patch_mode
provision_vm_agent = var.provision_vm_agent
proximity_placement_group_id = var.proximity_placement_group_id
secure_boot_enabled = var.secure_boot_enabled
size = var.size
source_image_id = var.source_image_id
timezone = var.timezone
virtual_machine_scale_set_id = var.virtual_machine_scale_set_id
vtpm_enabled = var.vtpm_enabled
zone = var.zone
dynamic "additional_capabilities" {
for_each = var.additional_capabilities[*]
content {
ultra_ssd_enabled = var.additional_capabilities.ultra_ssd_enabled
hibernation_enabled = var.additional_capabilities.hibernation_enabled
}
}
dynamic "additional_unattend_content" {
for_each = var.additional_unattend_content[*]
content {
content = var.additional_unattend_content.content
setting = var.additional_unattend_content.setting
}
}
dynamic "boot_diagnostics" {
for_each = var.boot_diagnostics.enabled ? [true] : []
content {
storage_account_uri = var.boot_diagnostics.storage_account_uri
}
}
dynamic "identity" {
for_each = try(var.identity.type, null) != null ? [true] : []
content {
type = var.identity.type
identity_ids = try(coalescelist(var.identity.identity_ids, azurerm_user_assigned_identity.this[*].id), null)
}
}
os_disk {
caching = var.os_disk.caching
disk_encryption_set_id = var.os_disk.disk_encryption_set_id
disk_size_gb = var.os_disk.disk_size_gb
name = coalesce(var.os_disk.name, "osdisk-${trimprefix(var.name, "vm-")}")
secure_vm_disk_encryption_set_id = var.os_disk.secure_vm_disk_encryption_set_id
security_encryption_type = var.os_disk.security_encryption_type
storage_account_type = var.os_disk.storage_account_type
write_accelerator_enabled = var.os_disk.write_accelerator_enabled
}
dynamic "plan" {
for_each = var.plan[*]
content {
name = var.plan.name
product = var.plan.product
publisher = var.plan.publisher
}
}
dynamic "source_image_reference" {
for_each = var.source_image_id == null ? [true] : []
content {
publisher = local.image.publisher
offer = local.image.offer
sku = local.image.sku
version = local.image.version
}
}
lifecycle {
ignore_changes = [
# Gallery applications are installed via Azure policy.
gallery_application,
# Disk name may change during restore or other manual operations.
os_disk[0].name,
]
}
}
resource "azapi_resource_action" "this" {
type = "Microsoft.Compute/disks@2024-03-02"
resource_id = "${join("/", slice(split("/", local.virtual_machine.id), 0, 5))}/providers/Microsoft.Compute/disks/${local.virtual_machine.os_disk[0].name}"
method = "PATCH"
body = {
tags = var.tags
}
}