How to Clone an Azure VM to Another Region While Maintaining Existing Configuration


1 views

When working with Azure VMs, many developers face this common scenario: you need an exact replica of your production VM in another region for disaster recovery, but Azure's built-in tools seem to focus on migration rather than true cloning. The main technical constraints are:

  • Duplicate computer names aren't allowed in the same Active Directory
  • Static IP configurations can conflict
  • Azure doesn't provide a direct "clone" button in the portal

Here's the most reliable method I've found after testing multiple approaches:

# First, create a snapshot of your source VM's OS disk
$sourceVM = Get-AzVM -ResourceGroupName "Prod-RG" -Name "WebServer01"
$snapshotConfig = New-AzSnapshotConfig 
    -SourceUri $sourceVM.StorageProfile.OsDisk.ManagedDisk.Id 
    -Location $sourceVM.Location 
    -CreateOption copy

$snapshot = New-AzSnapshot 
    -Snapshot $snapshotConfig 
    -SnapshotName "WebServer01-Snapshot" 
    -ResourceGroupName "Prod-RG"

# Create a new managed disk from the snapshot in target region
$targetDiskConfig = New-AzDiskConfig 
    -Location "westeurope" 
    -CreateOption Copy 
    -SourceResourceId $snapshot.Id

$targetDisk = New-AzDisk 
    -Disk $targetDiskConfig 
    -ResourceGroupName "DR-RG" 
    -DiskName "WebServer01-DR-Disk"

# Finally create the new VM
$newVM = New-AzVMConfig 
    -VMName "WebServer01-DR" 
    -VMSize $sourceVM.HardwareProfile.VmSize

Set-AzVMOSDisk -VM $newVM -ManagedDiskId $targetDisk.Id -CreateOption Attach

New-AzVM 
    -ResourceGroupName "DR-RG" 
    -Location "westeurope" 
    -VM $newVM

For true disaster recovery, you'll need to adjust network settings:

# Create a new NIC with different private IP
$vnet = Get-AzVirtualNetwork -Name "DR-VNet" -ResourceGroupName "DR-RG"
$subnet = Get-AzVirtualNetworkSubnetConfig -Name "default" -VirtualNetwork $vnet

$newNIC = New-AzNetworkInterface 
    -Name "WebServer01-DR-NIC" 
    -ResourceGroupName "DR-RG" 
    -Location "westeurope" 
    -Subnet $subnet 
    -PrivateIpAddress "10.0.1.101"

$newVM.NetworkProfile.NetworkInterfaces[0].Id = $newNIC.Id

After cloning, you'll need to:

  1. Update computer name (via sysprep if Windows)
  2. Reconfigure application-specific settings
  3. Set up replication between regions

For ongoing replication rather than one-time cloning:

# Initialize ASR setup
New-AzRecoveryServicesAsrFabric 
    -Name "PrimaryFabric" 
    -Type "HyperVSite" 
    -Location "eastus"

New-AzRecoveryServicesAsrProtectionContainer 
    -Fabric "PrimaryFabric" 
    -Name "ProtectionContainer01"

# Create replication policy
$policy = New-AzRecoveryServicesAsrPolicy 
    -Name "DailyReplication" 
    -ReplicationProvider "HyperVReplicaAzure" 
    -ReplicationFrequencyInSeconds 86400 
    -RecoveryPointRetentionInHours 24

html

Cloning Azure VMs across regions isn't as straightforward as one might hope. The primary obstacles include:

  • Duplicate computer names in the same Azure AD
  • Region-specific resource dependencies
  • Storage account replication limitations

The most reliable method involves creating snapshots of managed disks:


# Capture the OS disk
$vm = Get-AzVM -ResourceGroupName "SourceRG" -Name "SourceVM"
$disk = Get-AzDisk -ResourceGroupName $vm.ResourceGroupName -DiskName $vm.StorageProfile.OsDisk.Name

# Create snapshot configuration
$snapshotConfig = New-AzSnapshotConfig 
    -SourceUri $disk.Id 
    -Location $disk.Location 
    -CreateOption copy

# Create the snapshot
$snapshot = New-AzSnapshot 
    -Snapshot $snapshotConfig 
    -SnapshotName "SourceVMSnapshot" 
    -ResourceGroupName "SnapshotRG"

Once you have the snapshot, create a new managed disk in the target region:


# Create disk in target region
$targetDisk = New-AzDiskConfig 
    -Location "WestEurope" 
    -SourceResourceId $snapshot.Id 
    -CreateOption Copy

New-AzDisk -Disk $targetDisk -DiskName "TargetVMDisk" -ResourceGroupName "TargetRG"

For true disaster recovery, you'll need to replicate network settings:


az network nic create \
    --resource-group TargetRG \
    --name TargetNIC \
    --vnet-name TargetVNet \
    --subnet TargetSubnet \
    --network-security-group TargetNSG

Assemble all components in the new region:


$vmConfig = New-AzVMConfig 
    -VMName "TargetVM" 
    -VMSize "Standard_DS2_v2" 
    -AvailabilitySetId $avSet.Id

Set-AzVMOSDisk 
    -VM $vmConfig 
    -ManagedDiskId $targetDisk.Id 
    -CreateOption Attach 
    -Windows

New-AzVM 
    -ResourceGroupName "TargetRG" 
    -Location "WestEurope" 
    -VM $vmConfig

For regular synchronization, consider Azure Automation with this pattern:

  1. Create snapshot of source VM disks
  2. Copy snapshots to target region
  3. Create new disks from snapshots
  4. Deploy new VM with cloned disks

For enterprise scenarios, ASR provides continuous replication:


{
    "properties": {
        "targetResourceGroupId": "/subscriptions/xxx/resourceGroups/TargetRG",
        "targetRegion": "westeurope",
        "replicationProviderSettings": {
            "instanceType": "HyperVReplicaAzure"
        }
    }
}
  • Always modify the cloned VM's computer name before joining domains
  • Update any region-specific connection strings or endpoints
  • Consider using Terraform for infrastructure-as-code replication