-
Notifications
You must be signed in to change notification settings - Fork 216
/
aib.tf
231 lines (207 loc) · 6.96 KB
/
aib.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
provider "azurerm" {
features {}
}
data "azurerm_client_config" "current" {}
data "azurerm_subscription" "current" {}
resource "random_uuid" "aib" {
}
resource "random_string" "aib" {
length = 8
special = false
upper = false
numeric = false
lower = true
keepers = {
always_run = "${timestamp()}"
}
}
resource "azurerm_resource_group" "aib" {
name = "rg-${var.location}-avd-${var.prefix}-shared-resources"
location = var.location
tags = var.tags
}
resource "azurerm_user_assigned_identity" "aib" {
name = "AIB-${random_uuid.aib.result}"
resource_group_name = azurerm_resource_group.aib.name
location = azurerm_resource_group.aib.location
tags = var.tags
}
resource "azurerm_role_definition" "aib" {
name = "AIB-${random_uuid.aib.result}"
scope = data.azurerm_subscription.current.id
description = "Azure Image Builder AVD"
permissions {
actions = [
"Microsoft.Authorization/*/read",
"Microsoft.Compute/images/write",
"Microsoft.Compute/images/read",
"Microsoft.Compute/images/delete",
"Microsoft.Compute/galleries/read",
"Microsoft.Compute/galleries/images/read",
"Microsoft.Compute/galleries/images/versions/read",
"Microsoft.Compute/galleries/images/versions/write",
"Microsoft.Storage/storageAccounts/blobServices/containers/read",
"Microsoft.Storage/storageAccounts/blobServices/containers/write",
"Microsoft.Storage/storageAccounts/blobServices/read",
"Microsoft.ContainerInstance/containerGroups/read",
"Microsoft.ContainerInstance/containerGroups/write",
"Microsoft.ContainerInstance/containerGroups/start/action",
"Microsoft.ManagedIdentity/userAssignedIdentities/*/read",
"Microsoft.ManagedIdentity/userAssignedIdentities/*/assign/action",
"Microsoft.Authorization/*/read",
"Microsoft.Resources/deployments/*",
"Microsoft.Resources/deploymentScripts/read",
"Microsoft.Resources/deploymentScripts/write",
"Microsoft.Resources/subscriptions/resourceGroups/read",
"Microsoft.VirtualMachineImages/imageTemplates/run/action",
"Microsoft.VirtualMachineImages/imageTemplates/read",
"Microsoft.Network/virtualNetworks/read",
"Microsoft.Network/virtualNetworks/subnets/join/action"
]
not_actions = []
}
assignable_scopes = [
data.azurerm_subscription.current.id,
azurerm_resource_group.aib.id
]
}
resource "azurerm_role_assignment" "aib" {
scope = azurerm_resource_group.aib.id
role_definition_id = azurerm_role_definition.aib.role_definition_resource_id
principal_id = azurerm_user_assigned_identity.aib.principal_id
}
resource "time_sleep" "aib" {
depends_on = [azurerm_role_assignment.aib]
create_duration = "60s"
}
resource "azurerm_shared_image_gallery" "aib" {
name = "avdgallery_${var.location}_${var.prefix}"
resource_group_name = azurerm_resource_group.aib.name
location = azurerm_resource_group.aib.location
tags = var.tags
}
resource "azurerm_shared_image" "aib" {
name = "avdImage-${var.publisher}-${var.offer}-${var.sku}"
gallery_name = azurerm_shared_image_gallery.aib.name
resource_group_name = azurerm_resource_group.aib.name
location = azurerm_resource_group.aib.location
os_type = "Windows"
hyper_v_generation = "V2"
identifier {
publisher = var.publisher
offer = var.offer
sku = var.sku
}
}
resource "azurerm_resource_group_template_deployment" "aib" {
name = random_string.aib.result
resource_group_name = azurerm_resource_group.aib.name
deployment_mode = "Incremental"
parameters_content = jsonencode({
"imageTemplateName" = {
value = random_string.aib.result
},
"api-version" = {
value = var.aib_api_version
}
"svclocation" = {
value = var.aib_region
}
})
template_content = <<TEMPLATE
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"imageTemplateName": {
"type": "string"
},
"api-version": {
"type": "string"
},
"svclocation": {
"type": "string"
}
},
"variables": {},
"resources": [
{
"name": "[parameters('imageTemplateName')]",
"type": "Microsoft.VirtualMachineImages/imageTemplates",
"apiVersion": "[parameters('api-version')]",
"location": "[parameters('svclocation')]",
"dependsOn": [],
"tags": {
"imagebuilderTemplate": "AzureImageBuilderSIG",
"userIdentity": "enabled"
},
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"${azurerm_user_assigned_identity.aib.id}": {}
}
},
"properties": {
"buildTimeoutInMinutes": 100,
"vmProfile": {
"vmSize": "Standard_DS4_v2",
"osDiskSizeGB": 127
},
"source": {
"type": "PlatformImage",
"publisher": "microsoftwindowsdesktop",
"offer": "office-365",
"sku": "win11-23h2-avd-m365",
"version": "latest"
},
"customize": [
{
"type": "PowerShell",
"name": "CreateBuildPath",
"scriptUri": "https://raw.githubusercontent.com/Azure/avdaccelerator/main/workload/scripts/Optimize_OS_for_AVD.ps1"
},
{
"type": "WindowsRestart",
"restartCheckCommand": "echo Azure-Image-Builder-Restarted-the-VM > c:\\buildArtifacts\\azureImageBuilderRestart.txt",
"restartTimeout": "5m"
},
{
"type": "WindowsUpdate",
"searchCriteria": "IsInstalled=0",
"filters": ["exclude:$_.Title -like '*Preview*'", "include:$true"],
"updateLimit": 40
}
],
"distribute": [
{
"type": "SharedImage",
"galleryImageId": "${azurerm_shared_image.aib.id}",
"runOutputName": "[parameters('imageTemplateName')]",
"artifactTags": {
"source": "azureVmImageBuilder",
"baseosimg": "windows11"
},
"replicationRegions": [${join(",", formatlist("\"%s\"", var.image_replication_regions))}]
}
]
}
}
]
}
TEMPLATE
depends_on = [
time_sleep.aib,
azurerm_shared_image.aib
]
}
resource "null_resource" "aib" {
triggers = {
always_run = "${timestamp()}"
}
provisioner "local-exec" {
command = "az resource invoke-action --resource-group ${azurerm_resource_group.aib.name} --resource-type Microsoft.VirtualMachineImages/imageTemplates -n ${random_string.aib.result} --action Run"
}
depends_on = [
azurerm_resource_group_template_deployment.aib,
]
}