@@ -3,7 +3,7 @@ title: Use Azure Image Builder with an image gallery for Windows VMs (preview)
3
3
description : Create Azure Shared Gallery image versions using Azure Image Builder and Azure PowerShell.
4
4
author : cynthn
5
5
ms.author : cynthn
6
- ms.date : 01/14 /2020
6
+ ms.date : 05/05 /2020
7
7
ms.topic : how-to
8
8
ms.service : virtual-machines-windows
9
9
ms.subservice : imaging
@@ -84,25 +84,59 @@ $imageTemplateName="helloImageTemplateWin02ps"
84
84
# Distribution properties object name (runOutput).
85
85
# This gives you the properties of the managed image on completion.
86
86
$runOutputName="winclientR01"
87
+
88
+ # Create a resource group for Image Template and Shared Image Gallery
89
+ New-AzResourceGroup `
90
+ -Name $imageResourceGroup `
91
+ -Location $location
87
92
```
88
93
89
94
95
+ ## Create a user-assigned identity and set permissions on the resource group
96
+ Image Builder will use the [ user-identity] ( https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/how-to-manage-ua-identity-powershell ) provided to inject the image into the Azure Shared Image Gallery (SIG). In this example, you will create an Azure role definition that has the granular actions to perform distributing the image to the SIG. The role definition will then be assigned to the user-identity.
90
97
91
- ## Create the resource group
98
+ ``` powershell
99
+ # setup role def names, these need to be unique
100
+ $timeInt=$(get-date -UFormat "%s")
101
+ $imageRoleDefName="Azure Image Builder Image Def"+$timeInt
102
+ $idenityName="aibIdentity"+$timeInt
92
103
93
- Create a resource group and give Azure Image Builder permission to create resources in that resource group.
104
+ ## Add AZ PS module to support AzUserAssignedIdentity
105
+ Install-Module -Name Az.ManagedServiceIdentity
94
106
95
- ``` powershell
96
- New-AzResourceGroup `
97
- -Name $imageResourceGroup `
98
- -Location $location
99
- New-AzRoleAssignment `
100
- -ObjectId ef511139-6170-438e-a6e1-763dc31bdf74 `
101
- -Scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup `
102
- -RoleDefinitionName Contributor
107
+ # create identity
108
+ New-AzUserAssignedIdentity -ResourceGroupName $imageResourceGroup -Name $idenityName
109
+
110
+ $idenityNameResourceId=$(Get-AzUserAssignedIdentity -ResourceGroupName $imageResourceGroup -Name $idenityName).Id
111
+ $idenityNamePrincipalId=$(Get-AzUserAssignedIdentity -ResourceGroupName $imageResourceGroup -Name $idenityName).PrincipalId
103
112
```
104
113
105
114
115
+ ### Assign permissions for identity to distribute images
116
+
117
+ This command will download a Azure Role Definition template, and update the template with the parameters specified earlier.
118
+
119
+ ``` powershell
120
+ $aibRoleImageCreationUrl="https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleImageCreation.json"
121
+ $aibRoleImageCreationPath = "aibRoleImageCreation.json"
122
+
123
+ # download config
124
+ Invoke-WebRequest -Uri $aibRoleImageCreationUrl -OutFile $aibRoleImageCreationPath -UseBasicParsing
125
+
126
+ ((Get-Content -path $aibRoleImageCreationPath -Raw) -replace '<subscriptionID>',$subscriptionID) | Set-Content -Path $aibRoleImageCreationPath
127
+ ((Get-Content -path $aibRoleImageCreationPath -Raw) -replace '<rgName>', $imageResourceGroup) | Set-Content -Path $aibRoleImageCreationPath
128
+ ((Get-Content -path $aibRoleImageCreationPath -Raw) -replace 'Azure Image Builder Service Image Creation Role', $imageRoleDefName) | Set-Content -Path $aibRoleImageCreationPath
129
+
130
+ # create role definition
131
+ New-AzRoleDefinition -InputFile ./aibRoleImageCreation.json
132
+
133
+ # grant role definition to image builder service principal
134
+ New-AzRoleAssignment -ObjectId $idenityNamePrincipalId -RoleDefinitionName $imageRoleDefName -Scope "/subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup"
135
+
136
+ ### NOTE: If you see this error: 'New-AzRoleDefinition: Role definition limit exceeded. No more role definitions can be created.' See this article to resolve:
137
+ https://docs.microsoft.com/azure/role-based-access-control/troubleshooting
138
+ ```
139
+
106
140
107
141
## Create the Shared Image Gallery
108
142
@@ -168,7 +202,7 @@ Invoke-WebRequest `
168
202
-replace '<region1>',$location | Set-Content -Path $templateFilePath
169
203
(Get-Content -path $templateFilePath -Raw ) `
170
204
-replace '<region2>',$replRegion2 | Set-Content -Path $templateFilePath
171
-
205
+ ((Get-Content -path $templateFilePath -Raw) -replace '<imgBuilderId>',$idenityNameResourceId) | Set-Content -Path $templateFilePath
172
206
```
173
207
174
208
@@ -279,6 +313,24 @@ Delete image template.
279
313
Remove-AzResource -ResourceId $resTemplateId.ResourceId -Force
280
314
```
281
315
316
+ Delete role assignment
317
+
318
+ ``` powerShell
319
+ Remove-AzRoleAssignment -ObjectId $idenityNamePrincipalId -RoleDefinitionName $imageRoleDefName -Scope "/subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup"
320
+ ```
321
+
322
+ remove definitions
323
+
324
+ ``` powerShell
325
+ Remove-AzRoleDefinition -Name "$idenityNamePrincipalId" -Force -Scope "/subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup"
326
+ ```
327
+
328
+ delete identity
329
+
330
+ ``` powerShell
331
+ Remove-AzUserAssignedIdentity -ResourceGroupName $imageResourceGroup -Name $idenityName -Force
332
+ ```
333
+
282
334
delete the resource group.
283
335
284
336
``` powerShell
0 commit comments