-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathavd.tf
250 lines (202 loc) · 7.52 KB
/
avd.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
# Resource Group
resource "azurerm_resource_group" "avd" {
name = "avd-rg"
location = var.location
}
# Network Resources
resource "azurerm_virtual_network" "avd" {
name = "avd-vnet"
location = azurerm_resource_group.avd.location
resource_group_name = azurerm_resource_group.avd.name
address_space = ["10.10.0.0/16"]
# Use AADDS DCs as DNS servers
dns_servers = azurerm_active_directory_domain_service.aadds.initial_replica_set.0.domain_controller_ip_addresses
}
resource "azurerm_subnet" "avd" {
name = "avd-snet"
resource_group_name = azurerm_resource_group.avd.name
virtual_network_name = azurerm_virtual_network.avd.name
address_prefixes = ["10.10.0.0/24"]
}
resource "azurerm_virtual_network_peering" "aadds_to_avd" {
name = "hub-to-avd-peer"
resource_group_name = azurerm_resource_group.aadds.name
virtual_network_name = azurerm_virtual_network.aadds.name
remote_virtual_network_id = azurerm_virtual_network.avd.id
}
resource "azurerm_virtual_network_peering" "avd_to_aadds" {
name = "avd-to-aadds-peer"
resource_group_name = azurerm_resource_group.avd.name
virtual_network_name = azurerm_virtual_network.avd.name
remote_virtual_network_id = azurerm_virtual_network.aadds.id
}
# Host Pool
locals {
# Switzerland North is not supported
avd_location = "West Europe"
}
resource "azurerm_virtual_desktop_host_pool" "avd" {
name = "avd-vdpool"
location = local.avd_location
resource_group_name = azurerm_resource_group.avd.name
type = "Pooled"
load_balancer_type = "BreadthFirst"
friendly_name = "AVD Host Pool using AADDS"
}
resource "time_rotating" "avd_registration_expiration" {
# Must be between 1 hour and 30 days
rotation_days = 29
}
resource "azurerm_virtual_desktop_host_pool_registration_info" "avd" {
hostpool_id = azurerm_virtual_desktop_host_pool.avd.id
expiration_date = time_rotating.avd_registration_expiration.rotation_rfc3339
}
# Workspace and App Group
resource "azurerm_virtual_desktop_workspace" "avd" {
name = "avd-vdws"
location = local.avd_location
resource_group_name = azurerm_resource_group.avd.name
}
resource "azurerm_virtual_desktop_application_group" "avd" {
name = "desktop-vdag"
location = local.avd_location
resource_group_name = azurerm_resource_group.avd.name
type = "Desktop"
host_pool_id = azurerm_virtual_desktop_host_pool.avd.id
}
resource "azurerm_virtual_desktop_workspace_application_group_association" "avd" {
workspace_id = azurerm_virtual_desktop_workspace.avd.id
application_group_id = azurerm_virtual_desktop_application_group.avd.id
}
# Session Host VMs
resource "azurerm_network_interface" "avd" {
count = var.avd_host_pool_size
name = "avd-nic-${count.index}"
location = azurerm_resource_group.avd.location
resource_group_name = azurerm_resource_group.avd.name
ip_configuration {
name = "avd-ipconf"
subnet_id = azurerm_subnet.avd.id
private_ip_address_allocation = "Dynamic"
}
}
resource "random_password" "avd_local_admin" {
length = 64
}
resource "random_id" "avd" {
count = var.avd_host_pool_size
byte_length = 2
}
# Custom Managed Image
# https://github.com/schnerring/packer-windows-avd
data "azurerm_image" "win11" {
name = "win11-21h2-avd-m365-22000.1455.230110"
resource_group_name = "packer-artifacts-rg"
}
resource "azurerm_windows_virtual_machine" "avd" {
count = var.avd_host_pool_size
name = "avd-vm-${count.index}-${random_id.avd[count.index].hex}"
location = azurerm_resource_group.avd.location
resource_group_name = azurerm_resource_group.avd.name
size = "Standard_D4s_v4"
license_type = "Windows_Client" # https://docs.microsoft.com/en-us/azure/virtual-machines/windows/windows-desktop-multitenant-hosting-deployment#verify-your-vm-is-utilizing-the-licensing-benefit
admin_username = "avd-local-admin"
admin_password = random_password.avd_local_admin.result
network_interface_ids = [azurerm_network_interface.avd[count.index].id]
os_disk {
caching = "ReadWrite"
storage_account_type = "Premium_LRS"
}
# Custom Managed Image
source_image_id = data.azurerm_image.win11.id
# Official Microsoft Default Image
#source_image_reference {
# publisher = "MicrosoftWindowsDesktop"
# offer = "windows-11"
# sku = "win11-21h2-avd"
# version = "latest"
#}
}
# AADDS Domain-join
resource "azurerm_virtual_machine_extension" "avd_aadds_join" {
count = var.avd_host_pool_size
name = "aadds-join-vmext"
virtual_machine_id = azurerm_windows_virtual_machine.avd[count.index].id
publisher = "Microsoft.Compute"
type = "JsonADDomainExtension"
type_handler_version = "1.3"
auto_upgrade_minor_version = true
settings = <<-SETTINGS
{
"Name": "${azurerm_active_directory_domain_service.aadds.domain_name}",
"OUPath": "${var.avd_ou_path}",
"User": "${azuread_user.dc_admin.user_principal_name}",
"Restart": "true",
"Options": "3"
}
SETTINGS
protected_settings = <<-PROTECTED_SETTINGS
{
"Password": "${random_password.dc_admin.result}"
}
PROTECTED_SETTINGS
lifecycle {
ignore_changes = [settings, protected_settings]
}
depends_on = [
azurerm_virtual_network_peering.aadds_to_avd,
azurerm_virtual_network_peering.avd_to_aadds
]
}
# Register to Host Pool
resource "azurerm_virtual_machine_extension" "avd_register_session_host" {
count = var.avd_host_pool_size
name = "register-session-host-vmext"
virtual_machine_id = azurerm_windows_virtual_machine.avd[count.index].id
publisher = "Microsoft.Powershell"
type = "DSC"
type_handler_version = "2.73"
settings = <<-SETTINGS
{
"modulesUrl": "${var.avd_register_session_host_dsc_modules_url}",
"configurationFunction": "Configuration.ps1\\AddSessionHost",
"properties": {
"hostPoolName": "${azurerm_virtual_desktop_host_pool.avd.name}",
"aadJoin": false
}
}
SETTINGS
protected_settings = <<-PROTECTED_SETTINGS
{
"properties": {
"registrationInfoToken": "${azurerm_virtual_desktop_host_pool_registration_info.avd.token}"
}
}
PROTECTED_SETTINGS
lifecycle {
ignore_changes = [settings, protected_settings]
}
depends_on = [azurerm_virtual_machine_extension.avd_aadds_join]
}
# Role-based Access Control
data "azurerm_role_definition" "desktop_virtualization_user" {
name = "Desktop Virtualization User"
}
resource "azuread_group" "avd_users" {
display_name = "AVD Users"
security_enabled = true
}
resource "azurerm_role_assignment" "avd_users_desktop_virtualization_user" {
scope = azurerm_virtual_desktop_application_group.avd.id
role_definition_id = data.azurerm_role_definition.desktop_virtualization_user.id
principal_id = azuread_group.avd_users.id
}
data "azuread_user" "avd_users" {
for_each = toset(var.avd_user_upns)
user_principal_name = each.key
}
resource "azuread_group_member" "avd_users" {
for_each = data.azuread_user.avd_users
group_object_id = azuread_group.avd_users.id
member_object_id = each.value.id
}