Create a Virtual Machine and Attach an osDisk Snapshot using ARM Template
Azure Resource Manager (ARM)| Snapshot | Azure CLI
--
Have you ever had a need to replicate a Virtual Machine using an Existing Operating System (OS) Disk on Azure? Well, i know i did. So this article will explain how to do this.
Assumptions:
- Assumes you already created a Virtual Network (VNet), Network Security Groups (NSGs), and Subnet to associate to the below Virtual Machine.
- Assumes you have Azure CLI Installed
- Assumes you have the capability to run ARM Templates
Step 1. Creating the Snapshot
You can create a snapshot with easy by using the Azure CLI commands. I have written a powershell script that will do just this:
# Must already have a Virtual Machine with an OS Disk prior to running the script.
$resourceGroup = "<your resource group name>"
$virtualMachineName = "<your virtual machine name>"
$snapshotName = "<name of your new snapshot>"Write-Output "Getting OS Disk ID for Virtual Machine..."
$osDiskId = az vm list -g $resourceGroup --query "[?name == '$virtualMachineName'].storageProfile.osDisk.managedDisk.id" --output tsv
Write-Output "Creating Snapshot using OS Disk Id"
az snapshot create -g $resourceGroup -n "$snapshotName" --source $osDiskId
Write-Output "Snapshot Created"
Save the file as create-snapshot-from-disk.p1 and then run it.
Step 2. Setup an Azure Resource Manager (ARM) Template
This template will include the following resources:
- Public IP Address to access via RDP
- Network Interface
- Compute Disk
- Virtual Machine
Add the following azuredeploy.jsonc file:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
// Virtual Machine Instance Details
"virtualMachineName": {
"type": "string",
"metadata": {β¦