You can migrate an Amazon Web Services (AWS) EC2 instance to Azure by uploading the virtual hard disk (VHD). If you want to create multiple virtual machines (VMs) in Azure from the same image, you must first generalize the VM and then export the generalized VHD to a local directory. Once the VHD is uploaded, you can create a new Azure VM that uses Managed Disks for storage. Azure Managed Disks removes the need to manage storage accounts for Azure IaaS VMs. You have to only specify the type (Premium or Standard) and size of disk you need, and Azure creates and manages the disk for you.
Before starting this process, make sure that you review Plan for the migration to Managed Disks.
Before uploading any VHD to Azure, you should follow Prepare a Windows VHD or VHDX to upload to Azure.
Before you begin
If you use PowerShell, make sure that you have the latest version of the AzureRM.Compute PowerShell module. Run the following command to install it.
Install-Module AzureRM.Compute -MinimumVersion 2.6.0
For more information, see Azure PowerShell Versioning.
Generalize the VM
Generalizing a VM using Sysprep removes any machine-specific information and personal account information from the VHD and prepares the machine to be used as an image. For details about Sysprep, see How to Use Sysprep: An Introduction.
Make sure the server roles running on the machine are supported by Sysprep. For more information, see Sysprep Support for Server Roles
If you are running Sysprep before uploading your VHD to Azure for the first time, make sure you have prepared your VM before running Sysprep.
- Sign in to the Windows virtual machine.
- Open the Command Prompt window as an administrator. Change the directory to %windir%\system32\sysprep, and then run
- In the System Preparation Tool dialog box, select Enter System Out-of-Box Experience (OOBE), and make sure that the Generalize check box is selected.
- In Shutdown Options, select Shutdown.
- When Sysprep completes, it shuts down the virtual machine. Do not restart the VM.
Export the VHD from AWS
If you are using Amazon Web Services (AWS), export the EC2 instance to a VHD in an Amazon S3 bucket. Follow the steps described in the Amazon documentation for Exporting Amazon EC2 Instances to install the Amazon EC2 command-line interface (CLI) tool and run the create-instance-export-task command to export the EC2 instance to a VHD file. Be sure to use VHD for the DISK_IMAGE_FORMAT variable when running the create-instance-export-task command. The exported VHD file is saved in the Amazon S3 bucket you designate during that process.
aws ec2 create-instance-export-task --instance-id ID --target-environment TARGET_ENVIRONMENT ' --export-to-s3-task DiskImageFormat=DISK_IMAGE_FORMAT,ContainerFormat=ova,S3Bucket=BUCKET,S3Prefix=PREFIX
Download the VHD file from the S3 bucket. Select the VHD file, then select Actions > Download.
Upload the VHD
You need to log in to Azure, create a storage account and upload the VHD to the storage account before you can create the image.
Log in to Azure
If you don't already have PowerShell version installed, read How to install and configure Azure PowerShell.
Open Azure PowerShell and sign in to your Azure account. A pop-up window opens for you to enter your Azure account credentials.
Get the subscription IDs for your available subscriptions.
Set the correct subscription using the subscription ID. Replace
<subscriptionID>with the ID of the correct subscription.
Select-AzureRmSubscription -SubscriptionId "<subscriptionID>"
Get the storage account
You need a storage account in Azure to store the uploaded VM image. You can either use an existing storage account or create a new one.
If you are using the VHD to create a managed disk for a VM, the storage account location must be same the location where you create the VM.
To show the available storage accounts, type:
If you want to use an existing storage account, proceed to the Upload the VM image section.
If you need to create a storage account, follow these steps:
You need the name of the resource group where the storage account should be created. To find out all the resource groups that are in your subscription, type:
To create a resource group named myResourceGroup in the West US region, type:
New-AzureRmResourceGroup -Name myResourceGroup -Location "West US"
Create a storage account named mystorageaccount in this resource group by using the New-AzureRmStorageAccount cmdlet:
New-AzureRmStorageAccount -ResourceGroupName myResourceGroup -Name mystorageaccount -Location "West US" ` -SkuName "Standard_LRS" -Kind "Storage"
Valid values for -SkuName are:
- Standard_LRS - Locally redundant storage.
- Standard_ZRS - Zone redundant storage.
- Standard_GRS - Geo redundant storage.
- Standard_RAGRS - Read access geo redundant storage.
- Premium_LRS - Premium locally redundant storage.
Upload the VHD
Use the Add-AzureRmVhd cmdlet to upload the VHD to a container in your storage account. This example uploads the file myVHD.vhd from
"C:\Users\Public\Documents\Virtual hard disks\" to a storage account named mystorageaccount in the myResourceGroup resource group. The file is placed into the container named mycontainer and the new file name is myUploadedVHD.vhd.
$rgName = "myResourceGroup" $urlOfUploadedImageVhd = "https://mystorageaccount.blob.core.windows.net/mycontainer/myUploadedVHD.vhd" Add-AzureRmVhd -ResourceGroupName $rgName -Destination $urlOfUploadedImageVhd ` -LocalFilePath "C:\Users\Public\Documents\Virtual hard disks\myVHD.vhd"
If successful, you get a response that looks similar to this:
MD5 hash is being calculated for the file C:\Users\Public\Documents\Virtual hard disks\myVHD.vhd. MD5 hash calculation is completed. Elapsed time for the operation: 00:03:35 Creating new page blob of size 53687091712... Elapsed time for upload: 01:12:49 LocalFilePath DestinationUri ------------- -------------- C:\Users\Public\Doc... https://mystorageaccount.blob.core.windows.net/mycontainer/myUploadedVHD.vhd
Depending on your network connection and the size of your VHD file, this command may take a while to complete
Save the Destination URI path to use later if you are going to create a managed disk or a new VM using the uploaded VHD.
Other options for uploading a VHD
You can also upload a VHD to your storage account using one of the following:
We recommend using Import/Export Service if estimated uploading time is longer than 7 days. You can use DataTransferSpeedCalculator to estimate the time from data size and transfer unit.
Import/Export can be used to copy to a standard storage account. To use Premium storage, you need to copy from the standard storage to a premium storage account using a tool like AzCopy.
Create an image
Create a managed image using your generalized OS VHD.
First, set the common parameters:
$rgName = "myResourceGroupName" $vmName = "myVM" $location = "West Central US" $imageName = "yourImageName" $osVhdUri = "https://storageaccount.blob.core.windows.net/vhdcontainer/osdisk.vhd"
Create the image using your generalized OS VHD.
$imageConfig = New-AzureRmImageConfig -Location $location $imageConfig = Set-AzureRmImageOsDisk -Image $imageConfig -OsType Windows -OsState Generalized -BlobUri $osVhdUri $image = New-AzureRmImage -ImageName $imageName -ResourceGroupName $rgName -Image $imageConfig
Create VM from image
First we need to gather basic information about the image and create a variable for the image. This example uses a managed VM image named myImage that is in the myResourceGroup resource group in the West Central US location.
$rgName = "myResourceGroup" $location = "West Central US" $imageName = "myImage" $image = Get-AzureRMImage -ImageName $imageName -ResourceGroupName $rgName
Create a virtual network
Create the vNet and subnet of the virtual network.
Create the subnet. This example creates a subnet named mySubnet with the address prefix of 10.0.0.0/24.
$subnetName = "mySubnet" $singleSubnet = New-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -AddressPrefix 10.0.0.0/24
Create the virtual network. This example creates a virtual network named myVnet with the address prefix of 10.0.0.0/16.
$vnetName = "myVnet" $vnet = New-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $rgName -Location $location ` -AddressPrefix 10.0.0.0/16 -Subnet $singleSubnet
Create a public IP and NIC
To enable communication with the virtual machine in the virtual network, you need a public IP address and a network interface.
Create a public IP address. This example creates a public IP address named myPip.
$ipName = "myPip" $pip = New-AzureRmPublicIpAddress -Name $ipName -ResourceGroupName $rgName -Location $location ` -AllocationMethod Dynamic
Create the NIC. This example creates a NIC named myNic.
$nicName = "myNic" $nic = New-AzureRmNetworkInterface -Name $nicName -ResourceGroupName $rgName -Location $location ` -SubnetId $vnet.Subnets.Id -PublicIpAddressId $pip.Id
To be able to log in to your VM using RDP, you need to have a network security rule (NSG) that allows RDP access on port 3389.
This example creates an NSG named myNsg that contains a rule called myRdpRule that allows RDP traffic over port 3389. For more information about NSGs, see Opening ports to a VM in Azure using PowerShell.
$nsgName = "myNsg" $ruleName = "myRdpRule" $rdpRule = New-AzureRmNetworkSecurityRuleConfig -Name $ruleName -Description "Allow RDP" ` -Access Allow -Protocol Tcp -Direction Inbound -Priority 110 ` -SourceAddressPrefix Internet -SourcePortRange * ` -DestinationAddressPrefix * -DestinationPortRange 3389 $nsg = New-AzureRmNetworkSecurityGroup -ResourceGroupName $rgName -Location $location ` -Name $nsgName -SecurityRules $rdpRule
Create network variables
Create a variable for the completed virtual network.
$vnet = Get-AzureRmVirtualNetwork -ResourceGroupName $rgName -Name $vnetName
Get the credentials
The following cmdlet opens a window where you enter a new user name and password to use as the local administrator account for remotely accessing the VM.
$cred = Get-Credential
Set VM variables
Create variables for the VM name and computer name. This example sets the VM name as myVM and the computer name as myComputer.
$vmName = "myVM" $computerName = "myComputer"
Set the size of the virtual machine. This example creates Standard_DS1_v2 sized VM. See the VM sizes documentation for more information.
$vmSize = "Standard_DS1_v2"
Add the VM name and size to the VM configuration.
$vm = New-AzureRmVMConfig -VMName $vmName -VMSize $vmSize
Set the VM image
Set the source image using the ID of the managed VM image.
$vm = Set-AzureRmVMSourceImage -VM $vm -Id $image.Id
Set the OS configuration
Enter the storage type (PremiumLRS or StandardLRS) and the size of the OS disk. This example sets the account type to PremiumLRS, the disk size to 128 GB and disk caching to ReadWrite.
$vm = Set-AzureRmVMOSDisk -VM $vm -ManagedDiskStorageAccountType PremiumLRS -DiskSizeInGB 128 ` -CreateOption FromImage -Caching ReadWrite $vm = Set-AzureRmVMOperatingSystem -VM $vm -Windows -ComputerName $computerName ` -Credential $cred -ProvisionVMAgent -EnableAutoUpdate $vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $nic.Id
Create the VM
Create the new VM using the configuration that we have built and stored in the $vm variable.
New-AzureRmVM -VM $vm -ResourceGroupName $rgName -Location $location
Verify the VM
When complete, you should see the newly created VM in the Azure portal under Browse > Virtual machines, or by using the following PowerShell commands:
$vmList = Get-AzureRmVM -ResourceGroupName $rgName $vmList.Name
To sign in to your new virtual machine, browse to the VM in the portal, click Connect, and open the Remote Desktop RDP file. Use the account credentials of your original virtual machine to sign in to your new virtual machine. For more information, see How to connect and log on to an Azure virtual machine running Windows.