Skip to content

Commit 530fe7d

Browse files
authored
Merge pull request #114100 from danielsollondon/patch-23
May min reqs update
2 parents bb82e65 + fa49727 commit 530fe7d

File tree

1 file changed

+163
-103
lines changed

1 file changed

+163
-103
lines changed
Lines changed: 163 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
11
---
2-
title: Create a Linux VM with Azure Image Builder (preview)
3-
description: Create a Linux VM with the Azure Image Builder.
2+
title: Use Azure Image Builder with an image gallery for Linux VMs (preview)
3+
description: Create Linux VM images with Azure Image Builder and Shared Image Gallery.
44
author: cynthn
55
ms.author: cynthn
6-
ms.date: 05/02/2019
6+
ms.date: 05/05/2019
77
ms.topic: how-to
88
ms.service: virtual-machines-linux
99
ms.subservice: imaging
10-
10+
ms.reviewer: danis
1111
---
12-
# Preview: Create a Linux VM with Azure Image Builder
12+
# Preview: Create a Linux image and distribute it to a Shared Image Gallery
1313

14-
This article shows you how you can create a customized Linux image using the Azure Image Builder and the Azure CLI. The example in this article uses three different [customizers](image-builder-json.md#properties-customize) for customizing the image:
14+
This article shows you how you can use the Azure Image Builder, and the Azure CLI, to create an image version in a [Shared Image Gallery](https://docs.microsoft.com/azure/virtual-machines/windows/shared-image-galleries), then distribute the image globally. You can also do this using [Azure PowerShell](../windows/image-builder-gallery.md).
1515

16-
- Shell (ScriptUri) - downloads and runs a [shell script](https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/quickquickstarts/customizeScript.sh).
17-
- Shell (inline) - runs specific commands. In this example, the inline commands include creating a directory and updating the OS.
18-
- File - copies a [file from GitHub](https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/quickquickstarts/exampleArtifacts/buildArtifacts/index.html) into a directory on the VM.
1916

20-
You can also specify a `buildTimeoutInMinutes`. The default is 240 minutes, and you can increase a build time to allow for longer running builds.
17+
We will be using a sample .json template to configure the image. The .json file we are using is here: [helloImageTemplateforSIG.json](https://github.com/danielsollondon/azvmimagebuilder/blob/master/quickquickstarts/1_Creating_a_Custom_Linux_Shared_Image_Gallery_Image/helloImageTemplateforSIG.json).
2118

22-
We will be using a sample .json template to configure the image. The .json file we are using is here: [helloImageTemplateLinux.json](https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/quickquickstarts/0_Creating_a_Custom_Linux_Managed_Image/helloImageTemplateLinux.json).
19+
To distribute the image to a Shared Image Gallery, the template uses [sharedImage](image-builder-json.md#distribute-sharedimage) as the value for the `distribute` section of the template.
2320

2421
> [!IMPORTANT]
2522
> Azure Image Builder is currently in public preview.
@@ -43,194 +40,257 @@ Check your registration.
4340

4441
```azurecli-interactive
4542
az provider show -n Microsoft.VirtualMachineImages | grep registrationState
46-
43+
az provider show -n Microsoft.KeyVault | grep registrationState
44+
az provider show -n Microsoft.Compute | grep registrationState
4745
az provider show -n Microsoft.Storage | grep registrationState
4846
```
4947

5048
If they do not say registered, run the following:
5149

5250
```azurecli-interactive
5351
az provider register -n Microsoft.VirtualMachineImages
54-
52+
az provider register -n Microsoft.Compute
53+
az provider register -n Microsoft.KeyVault
5554
az provider register -n Microsoft.Storage
5655
```
5756

58-
## Setup example variables
57+
## Set variables and permissions
5958

6059
We will be using some pieces of information repeatedly, so we will create some variables to store that information.
6160

61+
For Preview, image builder will only support creating custom images in the same Resource Group as the source managed image. Update the resource group name in this example to be the same resource group as your source managed image.
6262

63-
```console
64-
# Resource group name - we are using myImageBuilderRG in this example
65-
imageResourceGroup=myImageBuilerRGLinux
63+
```azurecli-interactive
64+
# Resource group name - we are using ibLinuxGalleryRG in this example
65+
sigResourceGroup=ibLinuxGalleryRG
6666
# Datacenter location - we are using West US 2 in this example
67-
location=WestUS2
68-
# Name for the image - we are using myBuilderImage in this example
69-
imageName=myBuilderImage
70-
# Run output name
71-
runOutputName=aibLinux
67+
location=westus2
68+
# Additional region to replicate the image to - we are using East US in this example
69+
additionalregion=eastus
70+
# name of the shared image gallery - in this example we are using myGallery
71+
sigName=myIbGallery
72+
# name of the image definition to be created - in this example we are using myImageDef
73+
imageDefName=myIbImageDef
74+
# image distribution metadata reference name
75+
runOutputName=aibLinuxSIG
7276
```
7377

7478
Create a variable for your subscription ID. You can get this using `az account show | grep id`.
7579

76-
```console
77-
subscriptionID=<Your subscription ID>
80+
```azurecli-interactive
81+
subscriptionID=<Subscription ID>
7882
```
7983

80-
## Create the resource group.
81-
This is used to store the image configuration template artifact and the image.
84+
Create the resource group.
8285

8386
```azurecli-interactive
84-
az group create -n $imageResourceGroup -l $location
87+
az group create -n $sigResourceGroup -l $location
8588
```
8689

87-
## Set permissions on the resource group
88-
Give Image Builder 'contributor' permission to create the image in the resource group. Without the proper permissions, the image build will fail.
90+
## Create a user-assigned identity and set permissions on the resource group
91+
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 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.
8992

90-
The `--assignee` value is the app registration ID for the Image Builder service.
93+
```bash
94+
# create user assigned identity for image builder to access the storage account where the script is located
95+
idenityName=aibBuiUserId$(date +'%s')
96+
az identity create -g $sigResourceGroup -n $idenityName
9197

92-
```azurecli-interactive
98+
# get identity id
99+
imgBuilderCliId=$(az identity show -g $sigResourceGroup -n $idenityName | grep "clientId" | cut -c16- | tr -d '",')
100+
101+
# get the user identity URI, needed for the template
102+
imgBuilderId=/subscriptions/$subscriptionID/resourcegroups/$sigResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$idenityName
103+
104+
# this command will download a Azure Role Definition template, and update the template with the parameters specified earlier.
105+
curl https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleImageCreation.json -o aibRoleImageCreation.json
106+
107+
imageRoleDefName="Azure Image Builder Image Def"$(date +'%s')
108+
109+
# update the definition
110+
sed -i -e "s/<subscriptionID>/$subscriptionID/g" aibRoleImageCreation.json
111+
sed -i -e "s/<rgName>/$sigResourceGroup/g" aibRoleImageCreation.json
112+
sed -i -e "s/Azure Image Builder Service Image Creation Role/$imageRoleDefName/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
93118
az role assignment create \
94-
--assignee cf32a0cc-373c-47c9-9156-0db11f6a6dfc \
95-
--role Contributor \
96-
--scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup
119+
--assignee $imgBuilderCliId \
120+
--role $imageRoleDefName \
121+
--scope /subscriptions/$subscriptionID/resourceGroups/$sigResourceGroup
97122
```
98123

99-
## Download the template example
100124

101-
A parameterized sample image configuration template has been created for you to use. Download the sample .json file and configure it with the variables you set earlier.
125+
## Create an image definition and gallery
102126

103-
```bash
104-
curl https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/quickquickstarts/0_Creating_a_Custom_Linux_Managed_Image/helloImageTemplateLinux.json -o helloImageTemplateLinux.json
127+
To use Image Builder with a shared image gallery, you need to have an existing image gallery and image definition. Image Builder will not create the image gallery and image definition for you.
105128

106-
sed -i -e "s/<subscriptionID>/$subscriptionID/g" helloImageTemplateLinux.json
107-
sed -i -e "s/<rgName>/$imageResourceGroup/g" helloImageTemplateLinux.json
108-
sed -i -e "s/<region>/$location/g" helloImageTemplateLinux.json
109-
sed -i -e "s/<imageName>/$imageName/g" helloImageTemplateLinux.json
110-
sed -i -e "s/<runOutputName>/$runOutputName/g" helloImageTemplateLinux.json
129+
If you don't already have a gallery and image definition to use, start by creating them. First, create an image gallery.
130+
131+
```azurecli-interactive
132+
az sig create \
133+
-g $sigResourceGroup \
134+
--gallery-name $sigName
111135
```
112136

113-
You can modify this example .json as needed. For example, you can increase the value of `buildTimeoutInMinutes` to allow for longer running builds. You can edit the file in Cloud Shell using a text editor like `vi`.
137+
Then, create an image definition.
114138

115-
```bash
116-
vi helloImageTemplateLinux.json
139+
```azurecli-interactive
140+
az sig image-definition create \
141+
-g $sigResourceGroup \
142+
--gallery-name $sigName \
143+
--gallery-image-definition $imageDefName \
144+
--publisher myIbPublisher \
145+
--offer myOffer \
146+
--sku 18.04-LTS \
147+
--os-type Linux
117148
```
118149

119-
> [!NOTE]
120-
> For source image, you must always [specify a version](https://github.com/danielsollondon/azvmimagebuilder/blob/master/troubleshootingaib.md#image-version-failure), you cannot use `latest`.
121-
>
122-
> If you add or change the resource group where the image is being distributed, you need to make sure the [permissions are set for the resource group](#set-permissions-on-the-resource-group).
123150

151+
## Download and configure the .json
124152

125-
## Submit the image configuration
126-
Submit the image configuration to the VM Image Builder service
153+
Download the .json template and configure it with your variables.
127154

128155
```azurecli-interactive
129-
az resource create \
130-
--resource-group $imageResourceGroup \
131-
--properties @helloImageTemplateLinux.json \
132-
--is-full-object \
133-
--resource-type Microsoft.VirtualMachineImages/imageTemplates \
134-
-n helloImageTemplateLinux01
156+
curl https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/quickquickstarts/1_Creating_a_Custom_Linux_Shared_Image_Gallery_Image/helloImageTemplateforSIG.json -o helloImageTemplateforSIG.json
157+
sed -i -e "s/<subscriptionID>/$subscriptionID/g" helloImageTemplateforSIG.json
158+
sed -i -e "s/<rgName>/$sigResourceGroup/g" helloImageTemplateforSIG.json
159+
sed -i -e "s/<imageDefName>/$imageDefName/g" helloImageTemplateforSIG.json
160+
sed -i -e "s/<sharedImageGalName>/$sigName/g" helloImageTemplateforSIG.json
161+
sed -i -e "s/<region1>/$location/g" helloImageTemplateforSIG.json
162+
sed -i -e "s/<region2>/$additionalregion/g" helloImageTemplateforSIG.json
163+
sed -i -e "s/<runOutputName>/$runOutputName/g" helloImageTemplateforSIG.json
164+
sed -i -e "s%<imgBuilderId>%$imgBuilderId%g" helloImageTemplateforSIG.json
135165
```
136166

137-
If it completes successfully, it will return a success message, and create an image builder configuration template artifact in the $imageResourceGroup. You can see the resource group in the portal if you enable 'Show hidden types'.
138-
139-
Also, in the background, Image Builder creates a staging resource group in your subscription. Image Builder uses the staging resource group for the image build. The name of the resource group will be in this format: `IT_<DestinationResourceGroup>_<TemplateName>`.
167+
## Create the image version
140168

141-
> [!IMPORTANT]
142-
> Do not delete the staging resource group directly. If you delete the image template artifact, it will automatically delete the staging resource group. For more information, see the [Clean up](#clean-up) section at the end of this article.
169+
This next part will create the image version in the gallery.
143170

144-
If the service reports a failure during the image configuration template submission, see the [troubleshooting](https://github.com/danielsollondon/azvmimagebuilder/blob/master/troubleshootingaib.md#template-submission-errors--troubleshooting) steps. You will also need to delete the template before you retry submitting the build. To delete the template:
171+
Submit the image configuration to the Azure Image Builder service.
145172

146173
```azurecli-interactive
147-
az resource delete \
148-
--resource-group $imageResourceGroup \
174+
az resource create \
175+
--resource-group $sigResourceGroup \
176+
--properties @helloImageTemplateforSIG.json \
177+
--is-full-object \
149178
--resource-type Microsoft.VirtualMachineImages/imageTemplates \
150-
-n helloImageTemplateLinux01
179+
-n helloImageTemplateforSIG01
151180
```
152181

153-
## Start the image build
154-
155182
Start the image build.
156183

157-
158184
```azurecli-interactive
159185
az resource invoke-action \
160-
--resource-group $imageResourceGroup \
186+
--resource-group $sigResourceGroup \
161187
--resource-type Microsoft.VirtualMachineImages/imageTemplates \
162-
-n helloImageTemplateLinux01 \
188+
-n helloImageTemplateforSIG01 \
163189
--action Run
164190
```
165191

166-
Wait until the build is complete, for this example, it can take 10-15 minutes.
167-
168-
If you encounter any errors, please review these [troubleshooting](https://github.com/danielsollondon/azvmimagebuilder/blob/master/troubleshootingaib.md#image-build-errors--troubleshooting) steps.
192+
Creating the image and replicating it to both regions can take a while. Wait until this part is finished before moving on to creating a VM.
169193

170194

171195
## Create the VM
172196

173-
Create the VM using the image you built.
197+
Create a VM from the image version that was created by Azure Image Builder.
174198

175199
```azurecli-interactive
176200
az vm create \
177-
--resource-group $imageResourceGroup \
178-
--name myVM \
179-
--admin-username azureuser \
180-
--image $imageName \
201+
--resource-group $sigResourceGroup \
202+
--name myAibGalleryVM \
203+
--admin-username aibuser \
181204
--location $location \
205+
--image "/subscriptions/$subscriptionID/resourceGroups/$sigResourceGroup/providers/Microsoft.Compute/galleries/$sigName/images/$imageDefName/versions/latest" \
182206
--generate-ssh-keys
183207
```
184208

185-
Get the IP address from the output of creating the VM and use it to SSH to the VM.
209+
SSH into the VM.
186210

187-
```bash
188-
ssh azureuser@<pubIp>
211+
```azurecli-interactive
212+
ssh aibuser@<publicIpAddress>
189213
```
190214

191-
You should see the image was customized with a Message of the Day as soon as your SSH connection is established!
192-
193-
```output
215+
You should see the image was customized with a *Message of the Day* as soon as your SSH connection is established!
194216

217+
```console
195218
*******************************************************
196219
** This VM was built from the: **
197220
** !! AZURE VM IMAGE BUILDER Custom Image !! **
198221
** You have just been Customized :-) **
199222
*******************************************************
200223
```
201224

202-
Type `exit` when you are done to close the SSH connection.
203-
204-
## Check the source
225+
## Clean up resources
205226

206-
In the Image Builder Template, in the 'Properties', you will see the source image, customization script it runs, and where it is distributed.
227+
If you want to now try re-customizing the image version to create a new version of the same image, skip the next steps and go on to [Use Azure Image Builder to create another image version](image-builder-gallery-update-image-version.md).
207228

208-
```bash
209-
cat helloImageTemplateLinux.json
210-
```
211229

212-
For more detailed information about this .json file, see [Image builder template reference](image-builder-json.md)
230+
This will delete the image that was created, along with all of the other resource files. Make sure you are finished with this deployment before deleting the resources.
213231

214-
## Clean up
215-
216-
When you are done, you can delete the resources.
232+
When deleting image gallery resources, you need delete all of the image versions before you can delete the image definition used to create them. To delete a gallery, you first need to have deleted all of the image definitions in the gallery.
217233

218234
Delete the image builder template.
219235

220236
```azurecli-interactive
221237
az resource delete \
222-
--resource-group $imageResourceGroup \
238+
--resource-group $sigResourceGroup \
223239
--resource-type Microsoft.VirtualMachineImages/imageTemplates \
224-
-n helloImageTemplateLinux01
240+
-n helloImageTemplateforSIG01
225241
```
226242

227-
Delete the image resource group.
243+
Delete permissions assignments, roles and identity
244+
```azurecli-interactive
245+
az role assignment delete \
246+
--assignee $imgBuilderCliId \
247+
--role "$imageRoleDefName" \
248+
--scope /subscriptions/$subscriptionID/resourceGroups/$sigResourceGroup
249+
250+
az role definition delete --name "$imageRoleDefName"
228251
229-
```azurecli
230-
az group delete -n $imageResourceGroup
252+
az identity delete --ids $imgBuilderId
231253
```
232254

255+
Get the image version created by image builder, this always starts with `0.`, and then delete the image version
256+
257+
```azurecli-interactive
258+
sigDefImgVersion=$(az sig image-version list \
259+
-g $sigResourceGroup \
260+
--gallery-name $sigName \
261+
--gallery-image-definition $imageDefName \
262+
--subscription $subscriptionID --query [].'name' -o json | grep 0. | tr -d '"')
263+
az sig image-version delete \
264+
-g $sigResourceGroup \
265+
--gallery-image-version $sigDefImgVersion \
266+
--gallery-name $sigName \
267+
--gallery-image-definition $imageDefName \
268+
--subscription $subscriptionID
269+
```
270+
271+
272+
Delete the image definition.
273+
274+
```azurecli-interactive
275+
az sig image-definition delete \
276+
-g $sigResourceGroup \
277+
--gallery-name $sigName \
278+
--gallery-image-definition $imageDefName \
279+
--subscription $subscriptionID
280+
```
281+
282+
Delete the gallery.
283+
284+
```azurecli-interactive
285+
az sig delete -r $sigName -g $sigResourceGroup
286+
```
287+
288+
Delete the resource group.
289+
290+
```azurecli-interactive
291+
az group delete -n $sigResourceGroup -y
292+
```
233293

234294
## Next steps
235295

236-
To learn more about the components of the .json file used in this article, see [Image Builder template reference](image-builder-json.md).
296+
Learn more about [Azure Shared Image Galleries](shared-image-galleries.md).

0 commit comments

Comments
 (0)