How to Quickly Clone a VM in Azure !
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.
- 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.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.
Comment below in case of any issues / queries.
Thanks for checking out !