Problem
During the development process, my team and I need to deploy several VMs. However, we often lose time creating and deploying these VMs in different moments of the project. How can we accelerate this process?
Solution
In this article, we are going to look at how to create Azure VMs from VHD templates to reduce deployment time for new Azure VMs.
Exercise
An Azure virtual machine has one or more virtual hard disks (VHD) where the main VHD hosts the operating system. When we create an Azure VM image, we are creating a template that contains the virtual machine preconfigured operating system. The image can include more than the operating system, for example, a SQL Server instance and the necessary tools can be used as a starting point to create new VMs for your organization.
Generalize a VM
For this exercise, you’ll need to have an Azure VM already created. You can use one with just the operating system (Windows or Linux) or you can have more software or services installed on the VM such as SQL Server, IIS, Apache or other tools that you use regularly.
If you are new to the creation of Azure VMs, please follow these instructions to create a new Azure VM.
IMPORTANT NOTE: The following step will make your VM inoperable and you won’t be able to use it again. If you didn’t create a new VM for this exercise, please perform a backup of your VM before continuing.
If you are using a Windows VM, log into your VM and go to C:\windows\system32\sysprep and run sysprep.exe:

In the popup window, be sure to select the same options as seen in the below image

- System Cleanup Action: Enter System Out-of-Box Experience (OOBE)
- Generalize: Check
- Shutdown Options: Shutdown
Let the generalization process end and allow the VM to shut down.
For Linux users, the above operation is achieved by running the following command. Then, you should disconnect from the Azure Linux VM.
- waagent -deprovision+user
You must now deallocate the Azure VM. In the Azure Portal, go to the respective VM and click on the Stop option:

Wait for the operation to end and check the VM status to confirm that it’s deallocated:

Create the image
You can now create the VM image by clicking on the Capture button:

In the following screen, provide a name for the Image and specify which Resource Group you want to store this new image in (you can choose to create a new resource group).
You can also choose to delete the VM after the image creation since after you Generalize a VM, it can’t be used anymore. (check the warning messaging alerting for this).
At the end of the screen, provide the VM name where you are creating the image from. You can now click Create to initialize the image creation process.

Confirm in the Resources that the new VM image is stored there:







