How to Quickly Clone a VM in Azure !

Abhimanyu Garg
Mar 14, 2018 · 4 min read
Image for post
Image for post

Creating a clone/copy of an existing VM should ideally be done by creating an Image out of the VM and then using it to create a new VM. This is currently not the way we can do it in Azure because once you generalize the existing VM for capturing Image, the VM gets deallocated and you cannot login to it again. If you don’t generalize it and still go ahead with capturing the Image, still it will mark the VM as generalized (it basically assumes the administrator has run the steps at the OS level) and deallocates it.

So in this article we are going to see how we can clone a VM by creating Snapshot of the managed disk and then creating a new VM with it using Powershell.

Steps

  • Login to your Azure Portal and go to the VM you wish to clone.
  • Select the attached disk(s) and open it. Use the ‘Create snapshot’ option in the portal to create a snapshot of the disk. This way we get a copy of the managed disk in our Resource Group. Do this for OS as well any Data Disks which you have with the VM.
  • Now, click on Cloud Shell icon on the upper right corner of your Azure Portal and select Powershell from the dropdown. Let CloudShell load the Powershell module till you see the command prompt ready.
  • Execute the below Powershell commands to create Managed Disk from the snapshot we captured earlier. Repeat these steps in case you have multiple disk snapshots.
#Enter name of the ResourceGroup in which you have the snapshots
$resourceGroupName ='<Resource Group Name>'

#Enter name of the snapshot that will be used to create Managed Disks
$snapshotName = '<Snapshot Name>'

#Enter name of the Managed Disk
$diskName = '<Name of the Disk to be created>'

#Enter size of the disk in GB
$diskSize = '128'

#Enter the storage type for Disk. PremiumLRS / StandardLRS.
$storageType = 'Premium_LRS'

#Enter the Azure region where Managed Disk will be created. It should be same as Snapshot location
$location = 'westus'

#Set the context to the subscription Id where Managed Disk will be created
Select-AzureRmSubscription -SubscriptionId '<Subscription ID>'

#Get the Snapshot ID by using the details provided
$snapshot = Get-AzureRmSnapshot -ResourceGroupName $resourceGroupName -SnapshotName $snapshotName

#Create a new Managed Disk from the Snapshot provided
$disk = New-AzureRmDiskConfig -AccountType $storageType -Location $location -CreateOption Copy -SourceResourceId $snapshot.Id

New-AzureRmDisk -Disk $disk -ResourceGroupName $resourceGroupName -DiskName $diskName
  • Now that we have our managed disks ready, we can go ahead and create a new VM and attach the disks to it.
#Enter the name of an existing virtual network where virtual machine will be created
$virtualNetworkName = '<VN Name>'

#Enter the name of the virtual machine to be created
$virtualMachineName = '<VM Name>'

#Provide the size of the virtual machine
$virtualMachineSize = 'Standard_DS4_v2_Promo'

#Initialize virtual machine configuration
$VirtualMachine = New-AzureRmVMConfig -VMName $virtualMachineName -VMSize $virtualMachineSize

#(Optional Step) Add data disk to the configuration. Enter DataDisk Id
$VirtualMachine = Add-AzureRmVMDataDisk -VM $VirtualMachine -Name $dataDiskName -ManagedDiskId <DataDisk ResourceID> -Lun "0" -CreateOption "Attach"

#Use the Managed Disk Resource Id to attach it to the virtual machine. Use OS type based on the OS present in the disk - Windows / Linux
$VirtualMachine = Set-AzureRmVMOSDisk -VM $VirtualMachine -ManagedDiskId $disk.Id -CreateOption Attach -Windows

#Create a public IP
$publicIp = New-AzureRmPublicIpAddress -Name ($VirtualMachineName.ToLower()+'_ip') -ResourceGroupName $resourceGroupName -Location $snapshot.Location -AllocationMethod Dynamic

#Get VNET Information
$vnet = Get-AzureRmVirtualNetwork -Name $virtualNetworkName -ResourceGroupName $resourceGroupName

# Create NIC for the VM
$nic = New-AzureRmNetworkInterface -Name ($VirtualMachineName.ToLower()+'_nic') -ResourceGroupName $resourceGroupName -Location $snapshot.Location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicIp.Id

$VirtualMachine = Add-AzureRmVMNetworkInterface -VM $VirtualMachine -Id $nic.Id

#Create the virtual machine with Managed Disk
New-AzureRmVM -VM $VirtualMachine -ResourceGroupName $resourceGroupName -Location $snapshot.Location

New VM will get allocated in the ResourceGroup and VNet provided. Use the same credentials (as original VM) to login to the new VM.

For more details and other articles on Azure -

TechManyu Azure

TechManyu

TechManyu is a great platform to share your technical…

Abhimanyu Garg

Written by

Cloud and DevOps Professional with keen interest in System Design & Architecture. SRE | MultiCloud | Kubernetes (CKA) | Microservices | Automation | Ops

TechManyu

TechManyu

TechManyu is a great platform to share your technical knowledge and expertise to help millions of other developers and technical guys out there. Building an interactive community is our ultimate goal through which everyone can share and benefit at the same time. www.techmanyu.com

Abhimanyu Garg

Written by

Cloud and DevOps Professional with keen interest in System Design & Architecture. SRE | MultiCloud | Kubernetes (CKA) | Microservices | Automation | Ops

TechManyu

TechManyu

TechManyu is a great platform to share your technical knowledge and expertise to help millions of other developers and technical guys out there. Building an interactive community is our ultimate goal through which everyone can share and benefit at the same time. www.techmanyu.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store