Skip to content

Commit 49a6bfc

Browse files
Merge pull request #114105 from danielsollondon/patch-28
May updates
2 parents 530fe7d + 75bb21d commit 49a6bfc

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

articles/virtual-machines/linux/image-builder-user-assigned-identity.md

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,20 @@ az feature show --namespace Microsoft.VirtualMachineImages --name VirtualMachine
3838

3939
Check your registration.
4040

41+
4142
```azurecli-interactive
4243
az provider show -n Microsoft.VirtualMachineImages | grep registrationState
43-
44+
az provider show -n Microsoft.KeyVault | grep registrationState
45+
az provider show -n Microsoft.Compute | grep registrationState
4446
az provider show -n Microsoft.Storage | grep registrationState
4547
```
4648

4749
If they do not say registered, run the following:
4850

4951
```azurecli-interactive
5052
az provider register -n Microsoft.VirtualMachineImages
51-
53+
az provider register -n Microsoft.Compute
54+
az provider register -n Microsoft.KeyVault
5255
az provider register -n Microsoft.Storage
5356
```
5457

@@ -86,6 +89,37 @@ az group create -n $imageResourceGroup -l $location
8689
az group create -n $strResourceGroup -l $location
8790
```
8891

92+
Create a user-assigned identity and set permissions on the resource group.
93+
94+
Image Builder will use the [user-identity](https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/qs-configure-cli-windows-vm#user-assigned-managed-identity) provided to inject the image into the resource group. In this example, you will create an Azure role definition that has the granular actions to perform distributing the image. The role definition will then be assigned to the user-identity.
95+
96+
```console
97+
# create user assigned identity for image builder to access the storage account where the script is located
98+
idenityName=aibBuiUserId$(date +'%s')
99+
az identity create -g $imageResourceGroup -n $idenityName
100+
101+
# get identity id
102+
imgBuilderCliId=$(az identity show -g $imageResourceGroup -n $idenityName | grep "clientId" | cut -c16- | tr -d '",')
103+
104+
# get the user identity URI, needed for the template
105+
imgBuilderId=/subscriptions/$subscriptionID/resourcegroups/$imageResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$idenityName
106+
107+
# download preconfigured role definition example
108+
curl https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleImageCreation.json -o aibRoleImageCreation.json
109+
110+
# update the definition
111+
sed -i -e "s/<subscriptionID>/$subscriptionID/g" aibRoleImageCreation.json
112+
sed -i -e "s/<rgName>/$imageResourceGroup/g" aibRoleImageCreation.json
113+
114+
# create role definitions
115+
az role definition create --role-definition ./aibRoleImageCreation.json
116+
117+
# grant role definition to the user assigned identity
118+
az role assignment create \
119+
--assignee $imgBuilderCliId \
120+
--role "Azure Image Builder Service Image Creation Role" \
121+
--scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup
122+
```
89123

90124
Create the storage and copy the sample script into it from GitHub.
91125

@@ -112,37 +146,18 @@ az storage blob copy start \
112146
--source-uri https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/quickquickstarts/customizeScript.sh
113147
```
114148

115-
116-
117-
Give Image Builder permission to create resources in the image resource group. The `--assignee` value is the app registration ID for the Image Builder service.
118-
119-
```azurecli-interactive
120-
az role assignment create \
121-
--assignee cf32a0cc-373c-47c9-9156-0db11f6a6dfc \
122-
--role Contributor \
123-
--scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup
124-
```
125-
126-
127-
## Create user-assigned managed identity
128-
129-
Create the identity and assign permissions for the script storage account. For more information, see [User-Assigned Managed Identity](https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/qs-configure-cli-windows-vm#user-assigned-managed-identity).
149+
Give Image Builder permission to create resources in the image resource group. The `--assignee` value is the user-identity ID.
130150

131151
```azurecli-interactive
132-
# Create the user assigned identity
133-
identityName=aibBuiUserId$(date +'%s')
134-
az identity create -g $imageResourceGroup -n $identityName
135-
# assign the identity permissions to the storage account, so it can read the script blob
136-
imgBuilderCliId=$(az identity show -g $imageResourceGroup -n $identityName | grep "clientId" | cut -c16- | tr -d '",')
137152
az role assignment create \
138153
--assignee $imgBuilderCliId \
139154
--role "Storage Blob Data Reader" \
140155
--scope /subscriptions/$subscriptionID/resourceGroups/$strResourceGroup/providers/Microsoft.Storage/storageAccounts/$scriptStorageAcc/blobServices/default/containers/$scriptStorageAccContainer
141-
# create the user identity URI
142-
imgBuilderId=/subscriptions/$subscriptionID/resourcegroups/$imageResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$identityName
143156
```
144157

145158

159+
160+
146161
## Modify the example
147162

148163
Download the example .json file and configure it with the variables you created.
@@ -219,6 +234,13 @@ You should see the image was customized with a Message of the Day as soon as you
219234
When you are finished, you can delete the resources if they are no longer needed.
220235

221236
```azurecli-interactive
237+
238+
az role definition delete --name "$imageRoleDefName"
239+
```azurecli-interactive
240+
az role assignment delete \
241+
--assignee $imgBuilderCliId \
242+
--role "$imageRoleDefName" \
243+
--scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup
222244
az identity delete --ids $imgBuilderId
223245
az resource delete \
224246
--resource-group $imageResourceGroup \
@@ -230,4 +252,4 @@ az group delete -n $strResourceGroup
230252

231253
## Next steps
232254

233-
If you have any trouble working with Azure Image Builder, see [Troubleshooting](https://github.com/danielsollondon/azvmimagebuilder/blob/master/troubleshootingaib.md?toc=%2fazure%2fvirtual-machines%context%2ftoc.json).
255+
If you have any trouble working with Azure Image Builder, see [Troubleshooting](https://github.com/danielsollondon/azvmimagebuilder/blob/master/troubleshootingaib.md?toc=%2fazure%2fvirtual-machines%context%2ftoc.json).

0 commit comments

Comments
 (0)