Create a VM from a VHD – Automation, Deployment, and Configuration of Resources

You can create generalized or specialized virtual images from the Azure portal, Azure PowerShell, or the Azure CLI. To create an image from the Azure portal, you can navigate to the virtual machine and click the Capture option, as shown in Figure 8.20.

FIGURE 8.20 Capture a VM from the Azure portal

This will take you through the process for creating an image. Here you have to specify the image gallery, version, and other details of the image that you are planning to create. You can version the image inside an image gallery, and this can be used for future deployments. There is an option that is called Operating System State, and you will have two options: Generalized and Specialized (refer to Figure 8.21).

FIGURE 8.21 Creating an image of a VM

The portal gives a clear explanation of generalized and specialized. One thing you have to keep in mind is if you create a generalized image, the virtual machine will become unusable. This action cannot be reversed. Usually, you create a VM with the desired OS, install all the packages you need, and then create the generalized image. The portal shows a warning on the screen whenever you select the generalized option (refer to Figure 8.21). During the generalized image creation process, the VM will be deallocated, and a new image is created without any username, password, hostname, and other related setup. All this needs to be configured during the first boot. If you are selecting the specialized image, this won’t affect the current state of the existing VM, and you can have a copy of the VM. This will be useful if you want to roll back to a certain stage in the configuration if something goes wrong.

Based on the requirements, you will be able to create a generalized or specialized image of our virtual machine. This image can be used for future deployments. In Figure 8.22, you can see a sample image created in the Azure portal.

You can use this image to create VMs or VMSS per your requirements. In certain scenarios, you might not need to create a custom image and may prefer to configure our VMs post-deployment. There are a set of tools that can be leveraged for this post-deployment configuration; let’s understand more about this.