|
| 1 | +# Create a Custom Managed Image from an Azure Platform Vanilla OS Image |
| 2 | + |
| 3 | +> **MAY 2020 SERVICE ALERT** - Existing users, please ensure you are compliant this [Service Alert by 26th May!!!](https://github.com/danielsollondon/azvmimagebuilder#service-update-may-2020-action-needed-by-26th-may---please-review) |
| 4 | +
|
| 5 | +This article is to show you how you can create a basic customized image using the Azure VM Image Builder, and distribute to a region. This covers using mutliple customizations to illustrate some high level functionality: |
| 6 | + |
| 7 | +This covers using mutliple customizations to illustrate some high level functionality: |
| 8 | +* Shell (ScriptUri) - Downloading a bash script and executing it |
| 9 | +* Shell (inline) - Execute an array of commands |
| 10 | +* File - Copy a html file from github to a specified, pre-created directory |
| 11 | +* buildTimeoutInMinutes - Increase a build time to allow for longer running builds |
| 12 | +* vmProfile - specifying a vmSize and Network properties |
| 13 | +* osDiskSizeGB - you can increase the size of image |
| 14 | + |
| 15 | +To use this Quick Quickstarts, this can all be done using the Azure [Cloudshell from the Portal](https://azure.microsoft.com/en-us/features/cloud-shell/). Simply copy and paste the code from here, at a miniumum, just update the **subscriptionID** variable below. |
| 16 | + |
| 17 | +## Step 1 : Enable Prereqs |
| 18 | + |
| 19 | +Happy Image Building!!! |
| 20 | + |
| 21 | +### Register for Image Builder / VM / Storage Features |
| 22 | +```bash |
| 23 | +az feature register --namespace Microsoft.VirtualMachineImages --name VirtualMachineTemplatePreview |
| 24 | + |
| 25 | +az feature show --namespace Microsoft.VirtualMachineImages --name VirtualMachineTemplatePreview | grep state |
| 26 | + |
| 27 | +az feature show --namespace Microsoft.KeyVault --name VirtualMachineTemplatePreview | grep state |
| 28 | + |
| 29 | +# wait until it says registered |
| 30 | + |
| 31 | +# check you are registered for the providers |
| 32 | + |
| 33 | +az provider show -n Microsoft.VirtualMachineImages | grep registrationState |
| 34 | +az provider show -n Microsoft.Storage | grep registrationState |
| 35 | +az provider show -n Microsoft.Compute | grep registrationState |
| 36 | +az provider show -n Microsoft.KeyVault | grep registrationState |
| 37 | +``` |
| 38 | + |
| 39 | +If they do not show registered, run the commented out code below. |
| 40 | + |
| 41 | +```bash |
| 42 | +## az provider register -n Microsoft.VirtualMachineImages |
| 43 | +## az provider register -n Microsoft.Storage |
| 44 | +## az provider register -n Microsoft.Compute |
| 45 | +## az provider register -n Microsoft.KeyVault |
| 46 | + |
| 47 | +``` |
| 48 | + |
| 49 | +## Set Permissions & Create Resource Group for Image Builder Images |
| 50 | + |
| 51 | +```bash |
| 52 | +# set your environment variables here!!!! |
| 53 | + |
| 54 | +# destination image resource group |
| 55 | +imageResourceGroup=aibmdi001 |
| 56 | + |
| 57 | +# location (see possible locations in main docs) |
| 58 | +location=WestUS2 |
| 59 | + |
| 60 | +# your subscription |
| 61 | +# get the current subID : 'az account show | grep id' |
| 62 | +subscriptionID=$(az account show | grep id | tr -d '",' | cut -c7-) |
| 63 | + |
| 64 | +# name of the image to be created |
| 65 | +imageName=aibCustomLinuxImg01 |
| 66 | + |
| 67 | +# image distribution metadata reference name |
| 68 | +runOutputName=aibCustLinManImg01ro |
| 69 | + |
| 70 | +# create resource group |
| 71 | +az group create -n $imageResourceGroup -l $location |
| 72 | +``` |
| 73 | + |
| 74 | +## Create a user identify and assign permissions for the resource group where the image will be created |
| 75 | + |
| 76 | +### Create User-Assigned Managed Identity and Grant Permissions |
| 77 | +For more information on User-Assigned Managed Identity, see [here](https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/qs-configure-cli-windows-vm#user-assigned-managed-identity). |
| 78 | + |
| 79 | +```bash |
| 80 | +# create user assigned identity for image builder to access the storage account where the script is located |
| 81 | +idenityName=aibBuiUserId$(date +'%s') |
| 82 | +az identity create -g $imageResourceGroup -n $idenityName |
| 83 | + |
| 84 | +# get identity id |
| 85 | +imgBuilderCliId=$(az identity show -g $imageResourceGroup -n $idenityName | grep "clientId" | cut -c16- | tr -d '",') |
| 86 | + |
| 87 | +# get the user identity URI, needed for the template |
| 88 | +imgBuilderId=/subscriptions/$subscriptionID/resourcegroups/$imageResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$idenityName |
| 89 | + |
| 90 | +# download preconfigured role definition example |
| 91 | +curl https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/solutions/12_Creating_AIB_Security_Roles/aibRoleImageCreation.json -o aibRoleImageCreation.json |
| 92 | + |
| 93 | +imageRoleDefName="Azure Image Builder Image Def"$(date +'%s') |
| 94 | + |
| 95 | +# update the definition |
| 96 | +sed -i -e "s/<subscriptionID>/$subscriptionID/g" aibRoleImageCreation.json |
| 97 | +sed -i -e "s/<rgName>/$imageResourceGroup/g" aibRoleImageCreation.json |
| 98 | +sed -i -e "s/Azure Image Builder Service Image Creation Role/$imageRoleDefName/g" aibRoleImageCreation.json |
| 99 | + |
| 100 | +# create role definitions |
| 101 | +az role definition create --role-definition ./aibRoleImageCreation.json |
| 102 | + |
| 103 | +# grant role definition to the user assigned identity |
| 104 | +az role assignment create \ |
| 105 | + --assignee $imgBuilderCliId \ |
| 106 | + --role $imageRoleDefName \ |
| 107 | + --scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup |
| 108 | +``` |
| 109 | + |
| 110 | +## Step 2 : Modify HelloImage Example |
| 111 | + |
| 112 | +```bash |
| 113 | +# download the example and configure it with your vars |
| 114 | + |
| 115 | +curl https://raw.githubusercontent.com/danielsollondon/azvmimagebuilder/master/quickquickstarts/0_Creating_a_Custom_Linux_Managed_Image/helloImageTemplateLinux.json -o helloImageTemplateLinux.json |
| 116 | + |
| 117 | +sed -i -e "s/<subscriptionID>/$subscriptionID/g" helloImageTemplateLinux.json |
| 118 | +sed -i -e "s/<rgName>/$imageResourceGroup/g" helloImageTemplateLinux.json |
| 119 | +sed -i -e "s/<region>/$location/g" helloImageTemplateLinux.json |
| 120 | +sed -i -e "s/<imageName>/$imageName/g" helloImageTemplateLinux.json |
| 121 | +sed -i -e "s/<runOutputName>/$runOutputName/g" helloImageTemplateLinux.json |
| 122 | + |
| 123 | +sed -i -e "s%<imgBuilderId>%$imgBuilderId%g" helloImageTemplateLinux.json |
| 124 | + |
| 125 | +``` |
| 126 | + |
| 127 | +## Step 3 : Create the Image |
| 128 | + |
| 129 | +```bash |
| 130 | +# submit the image confiuration to the VM Image Builder Service |
| 131 | + |
| 132 | +az resource create \ |
| 133 | + --resource-group $imageResourceGroup \ |
| 134 | + --properties @helloImageTemplateLinux.json \ |
| 135 | + --is-full-object \ |
| 136 | + --resource-type Microsoft.VirtualMachineImages/imageTemplates \ |
| 137 | + -n helloImageTemplateLinux01 |
| 138 | + |
| 139 | +# wait approx 1-3mins, depending on external links |
| 140 | + |
| 141 | +# start the image build |
| 142 | + |
| 143 | +az resource invoke-action \ |
| 144 | + --resource-group $imageResourceGroup \ |
| 145 | + --resource-type Microsoft.VirtualMachineImages/imageTemplates \ |
| 146 | + -n helloImageTemplateLinux01 \ |
| 147 | + --action Run |
| 148 | + |
| 149 | +# wait approx 15mins |
| 150 | + |
| 151 | +``` |
| 152 | + |
| 153 | + |
| 154 | +## Step 4 : Create the VM |
| 155 | + |
| 156 | +```bash |
| 157 | +az vm create \ |
| 158 | + --resource-group $imageResourceGroup \ |
| 159 | + --name aibImgVm0001 \ |
| 160 | + --admin-username aibuser \ |
| 161 | + --image $imageName \ |
| 162 | + --location $location \ |
| 163 | + --generate-ssh-keys |
| 164 | + |
| 165 | +# and login... |
| 166 | + |
| 167 | +ssh aibuser@<pubIp> |
| 168 | + |
| 169 | +You should see the image was customized with a Message of the Day as soon as your SSH connection is established! |
| 170 | + |
| 171 | +******************************************************* |
| 172 | +** This VM was built from the: ** |
| 173 | +... |
| 174 | + |
| 175 | +``` |
| 176 | + |
| 177 | +## Clean Up |
| 178 | +```bash |
| 179 | +az resource delete \ |
| 180 | + --resource-group $imageResourceGroup \ |
| 181 | + --resource-type Microsoft.VirtualMachineImages/imageTemplates \ |
| 182 | + -n helloImageTemplateLinux01 |
| 183 | + |
| 184 | +# delete permissions asssignments, roles and identity |
| 185 | +az role assignment delete \ |
| 186 | + --assignee $imgBuilderCliId \ |
| 187 | + --role "$imageRoleDefName" \ |
| 188 | + --scope /subscriptions/$subscriptionID/resourceGroups/$imageResourceGroup |
| 189 | + |
| 190 | +az role definition delete --name "$imageRoleDefName" |
| 191 | + |
| 192 | +az identity delete --ids $imgBuilderId |
| 193 | + |
| 194 | + |
| 195 | +az group delete -n $imageResourceGroup |
| 196 | + |
| 197 | +``` |
| 198 | + |
| 199 | +## Next Steps |
| 200 | +If you loved or hated Image Builder, please go to next steps to leave feedback, contact dev team, more documentation, or try more examples [here](../quickquickstarts/nextSteps.md)] |
0 commit comments