Skip to content

Commit 024c83b

Browse files
May updates
1 parent 700b603 commit 024c83b

File tree

1 file changed

+64
-12
lines changed

1 file changed

+64
-12
lines changed

articles/virtual-machines/windows/image-builder-gallery.md

Lines changed: 64 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Use Azure Image Builder with an image gallery for Windows VMs (preview)
33
description: Create Azure Shared Gallery image versions using Azure Image Builder and Azure PowerShell.
44
author: cynthn
55
ms.author: cynthn
6-
ms.date: 01/14/2020
6+
ms.date: 05/05/2020
77
ms.topic: how-to
88
ms.service: virtual-machines-windows
99
ms.subservice: imaging
@@ -84,25 +84,59 @@ $imageTemplateName="helloImageTemplateWin02ps"
8484
# Distribution properties object name (runOutput).
8585
# This gives you the properties of the managed image on completion.
8686
$runOutputName="winclientR01"
87+
88+
# Create a resource group for Image Template and Shared Image Gallery
89+
New-AzResourceGroup `
90+
-Name $imageResourceGroup `
91+
-Location $location
8792
```
8893

8994

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/en-us/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.
9097

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
92103
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
94106
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
103112
```
104113

105114

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/en-us/azure/role-based-access-control/troubleshooting
138+
```
139+
106140

107141
## Create the Shared Image Gallery
108142

@@ -168,7 +202,7 @@ Invoke-WebRequest `
168202
-replace '<region1>',$location | Set-Content -Path $templateFilePath
169203
(Get-Content -path $templateFilePath -Raw ) `
170204
-replace '<region2>',$replRegion2 | Set-Content -Path $templateFilePath
171-
205+
((Get-Content -path $templateFilePath -Raw) -replace '<imgBuilderId>',$idenityNameResourceId) | Set-Content -Path $templateFilePath
172206
```
173207

174208

@@ -279,6 +313,24 @@ Delete image template.
279313
Remove-AzResource -ResourceId $resTemplateId.ResourceId -Force
280314
```
281315

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+
282334
delete the resource group.
283335

284336
```powerShell

0 commit comments

Comments
 (0)