Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / cloud

Importing Your Servers to Amazon EC2

0.00/5 (No votes)
3 Mar 2017CPOL4 min read 6.1K  
How to import your servers to Amazon EC2

As more and more infrastructure is moving to the cloud, the need to make the migration as painless as possible has increased. Typically, there are three scenarios:

  • Moving a physical machine to the cloud
  • Moving a virtual machine from local infrastructure to the cloud
  • Moving a machine from one cloud provider to another

Depending on the scenario above, the operating system and on the cloud provider, there are different options. Recently, I had to migrate some machines (Windows and Linux) from Microsoft Azure to Amazon EC2. Fortunately, Amazon EC2 provides import/export functionality (see https://aws.amazon.com/ec2/vm-import/). Unfortunately, it has limitations on what it can import and there's no straightforward way to import directly from Microsoft Azure. Since I did go through the pain of doing it, I'm sharing the necessary steps below:

  1. The first thing you have to do in any scenario is to somehow create an image of the existing machine. If your machine is in an existing local virtual infrastructure (such as HyperV or VMWare), that's easy. If not, you have to look for alternatives and depending on your OS, they are:
    1. In Windows, you can use VMWare Converter (download from http://www.vmware.com/products/converter.html) to create an image from a machine running anywhere. Just install it on the machine that you want to create an image and follow the steps. IMPORTANT: In case of importing to Amazon EC2, you should select only the OS disk, because it's not possible to import a machine with several disks. You can import the remaining disks after the machine is running in EC.
    2. In Linux, there are several tools, but you can stick with dd builtin utility. You have to do an image of the operating system disk (careful, disk not partition). If OS disk is /dev/sda, the command would be:
      dd if=/dev/sda of=/backups/os-sda.img
  2. After you have the image, the next step would be to make it compatible with Amazon EC2 import utility. According to the documentation (see http://docs.aws.amazon.com/vm-import/latest/userguide/vmimport-image-import.html#prerequisites-image), it supports OVA, VMDK, VHD and RAW format. In theory, the image created in step one should work, but since internally there are different options for VMDK or RAW format, they do not work. What I found out to work was converting them as below:
    1. In Windows, convert the VMDK file to VHD. You can use StarWind V2V Image Converter (see https://www.starwindsoftware.com/converter) to convert from VMDK to VHD. After installing it, follow the wizard and you will have your image in VHD format.
    2. In Linux, convert the RAW file to VMDK. Qemu tools (see http://www.qemu-project.org/) will do the job. After installing them, run:
      qemu-img convert -pO vmdk /backups/os-sda.img /backups/os-sda.vmdk
      When it is finished, you can transfer the vmdk file in a Windows machine and there, you can use the StarWind utility to create a VHD image ready to import in Amazon EC2.
  3. The next step is to actually import the image. For this, first you have to install and configure Amazon EC2 API Tools (from https://aws.amazon.com/items/351?externalID=351). IMPORTANT: Do not confuse them with AWS CLI Tools (https://aws.amazon.com/cli/). Documentation from reference (at https://awsdocs.s3.amazonaws.com/EC2/latest/ec2-clt.pdf) is very straightforward. After you have setup the EC2 API Tools, the command to import is:
    ec2-import-instance "D:\temp\os.vhd" -f VHD -t t2.2xlarge -a x86_64 -b test 
    -o access_key -w secret_key
    The meaning of the parameters is:
    1. -f VHD - Image format is VHD
    2. -t t2.exlarge - Type of instance to create in EC2 is t2.2xlarge
    3. -a x86_64 - Architecture is 64 bit. In case of 32 bit, you should use x86
    4. -b test - Bucket where the image will be imported is named test. You have to create a bucket first in S3 and use that name here.
    5. - o access_key and -w secret_key are the access key and secret key to access your AWS account.
    If the import is successful, you will get a message similar to:
    Average speed was 74.650 MBps
    The disk image for import-i-fh2e5d1c has been uploaded to Amazon S3
    where it is being converted into an EC2 instance. You may monitor the
    progress of this task by running ec2-describe-conversion-tasks. When
    the task is completed, you may use ec2-delete-disk-image to remove the
    image from S3.
    As explained, by using...
    ec2-describe-conversion-tasks
    ...you can check the progress of conversion. After it is finished, you will see the instance in EC2 console and you can work with it, the same as with other instances.

As you can see, there are a few steps to follow, but still it's a better way than to configure everything from scratch, especially if you're importing machines with a lot of configuration details. As a final note, the above will work even if you're running Microsoft SQL Server with databases in different drives (as I did). After starting SQL Server, some databases may have issues and you can recover them or use a backup to restore. In any case, the final step would be to use xSQL excellent Schema and Data Compare utility to synchronize the databases in new machine with the old one.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)