Migrate OnPrem VM to AWS EC2 using AWS VM Import/Export

0 / 81
Migrate OnPrem VM to AWS EC2
   

In this article we will learn how to migrate on prem VM to AWS EC2. In this example we will migrate VMDK VM image to AWS EC2

You can import a virtual machine (VM) into your Amazon EC2 instance using one of two methods:

 
  1. AWS VM Import/Export
  2. AWS Server Migration Service
 

AWS VM Import/Export allows you to import virtual machine (VM) images from another virtualization environment into EC2.

VM Import/Export allows you to import virtual machine (VM) images from your virtualization environment into Amazon EC2 as Amazon Machine Images (AMI), which you can then use to launch instances. You can then export the VM images from an instance to your virtualization environment.

 

By moving your VMs into Amazon EC2, you can utilize your investments in them to satisfy your IT security, configuration management, and compliance requirements.

Steps –

 
  1. Create a RHEL VM and export it as OVA.
  2. On your other Linux system install aws cli
  3. Create s3 bucket and upload OVA image
  4. Create required roles and policies
  5. Import using aws ec2 import-image
  6. Deploy EC2 instance using AMI
  7. Check EC2 instance has all the data and services running fine
 

Procedure –

 

1.Create a RHEL8 VM and export it as OVA. 

 

In this example RHEL8 VM is installed with a LAMP stack. VM is having dhcp-client package installed

 
# dnf -y install dhcp-client

After installing LAMP and dhcp-client export VM as OVA template

2. On your other Linux system install aws cli

# mkdir aws-install

# cd aws-install

# curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

# unzip awscliv2.zip

# sudo ./aws/install

# ./aws/install -i /usr/local/aws-cli -b /usr/local/bin

# which aws

# aws --version

Configure AWS and provide access key and secret key when prompted –

# aws configure

3. Create s3 bucket and upload OVA image

# aws s3 ls

# aws s3api create-bucket --bucket images-19159 --region us-east-1

# aws s3 ls

# aws s3api put-object --bucket images-19159 --key VM-Import/

# aws s3 ls s3://images-19159/VM-Import/

# aws s3 cp lamp-stack.ova s3://images-19159/VM-Import/

Using above commands we have created bucket images-19159 and have uploaded lamp-stack.ova to s3 bucket

 

4. Create required roles and policies

 

Create role for VM import

 
cat > "trust-policy.json" << "EOF"

{

   "Version": "2012-10-17",

   "Statement": [

      {

         "Effect": "Allow",

         "Principal": { "Service": "vmie.amazonaws.com" },

         "Action": "sts:AssumeRole",

         "Condition": {

            "StringEquals":{

               "sts:Externalid": "vmimport"

            }

         }

      }

   ]

}

EOF

 
# aws iam create-role --role-name vmimport --assume-role-policy-document "file://trust-policy.json"

Using above command we have created a role for VM Import

Create Policy, which will be attached to Role which we created in previous step

 
echo '{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Allow",
         "Action":[
            "s3:GetBucketLocation",
            "s3:GetObject",
            "s3:ListBucket" 
         ],
         "Resource":[
            "arn:aws:s3:::images-19159",
            "arn:aws:s3:::images-19159/*"
         ]
      },
      {
         "Effect":"Allow",
         "Action":[
            "ec2:ModifySnapshotAttribute",
            "ec2:CopySnapshot",
            "ec2:RegisterImage",
            "ec2:Describe*"
         ],
         "Resource":"*"
      }
   ]
}
' | sudo tee role-policy.json

echo '{

   "Version":"2012-10-17",

   "Statement":[

      {

         "Effect":"Allow",

         "Action":[

            "s3:GetBucketLocation",

            "s3:GetObject",

            "s3:ListBucket" 

         ],

         "Resource":[

            "arn:aws:s3:::images-19159",

            "arn:aws:s3:::images-19159/*"

         ]

      },

      {

         "Effect":"Allow",

         "Action":[

            "ec2:ModifySnapshotAttribute",

            "ec2:CopySnapshot",

            "ec2:RegisterImage",

            "ec2:Describe*"

         ],

         "Resource":"*"

      }

   ]

}

' | sudo tee role-policy.json

Attach policy to role

# aws iam put-role-policy --role-name vmimport \

                        --policy-name vmimport \

                        --policy-document "file://role-policy.json"

5. Import using aws ec2 import-image

We will create containers.json file which has details of Image to be imported in s3 bucket

 
echo '

  {

    "Description": "rhel8-lamp-stack",

    "UserBucket": {

        "S3Bucket": "images-19159",

        "S3Key": "VM-Import/lamp-stack.ova"

    }

}]

' > containers.json[

Using below command we start the import

 
 # aws ec2 import-image --description "rhel8-lamp-stack" --disk-containers "file://containers.json"

From output of above command note down the “ImportTaskId”

 

Output =>

 
{
    "Description": "rhel8-lamp-stack",

    "ImportTaskId": "import-ami-0ec04dc8c82edceb8",

    "Progress": "1",

    "SnapshotDetails": [

        {

            "Description": "rhel8-lamp-stack",

            "DiskImageSize": 0.0,

            "Format": "",

            "UserBucket": {

                "S3Bucket": "images-19159",

                "S3Key": "VM-Import/lamp-stack.ova"

            }

        }

    ],

    "Status": "active",

    "StatusMessage": "pending"

}
 

Check the progress of the import job.

 
# aws ec2 describe-import-image-tasks --import-task-ids "import-ami-0ec04dc8c82edceb8"

output =>

 
{
    "ImportImageTasks": [

        {

            "Description": "rhel8-lamp-stack",

            "ImportTaskId": "import-ami-0ec04dc8c82edceb8",

            "Progress": "19",

            "SnapshotDetails": [

                {

                    "DiskImageSize": 2654299648.0,

                    "Format": "VMDK",

                    "Status": "active",

                    "UserBucket": {

                        "S3Bucket": "images-19159",

                        "S3Key": "VM-Import/lamp-stack.ova"

                    }

                }

            ],

            "Status": "active",

            "StatusMessage": "converting",

            "Tags": []

        }

    ]

}

check status again -

{

    "ImportImageTasks": [

        {

            "Architecture": "x86_64",

            "Description": "rhel8-lamp-stack",

            "ImportTaskId": "import-ami-0ec04dc8c82edceb8",

            "LicenseType": "BYOL",

            "Platform": "Linux",

            "Progress": "39",

            "SnapshotDetails": [

                {

                    "DeviceName": "/dev/sda1",

                    "DiskImageSize": 2654299648.0,

                    "Format": "VMDK",

                    "Status": "completed",

                    "UserBucket": {

                        "S3Bucket": "images-19159",

                        "S3Key": "VM-Import/lamp-stack.ova"

                    }

                }

            ],

            "Status": "active",

            "StatusMessage": "booting",

            "Tags": [],

            "BootMode": "legacy_bios"

        }

    ]

}

check status again -

{

    "ImportImageTasks": [

        {

            "Architecture": "x86_64",

            "Description": "rhel8-lamp-stack",

            "ImportTaskId": "import-ami-0ec04dc8c82edceb8",

            "LicenseType": "BYOL",

            "Platform": "Linux",

            "Progress": "51",

            "SnapshotDetails": [

                {

                    "DeviceName": "/dev/sda1",

                    "DiskImageSize": 2654299648.0,

                    "Format": "VMDK",

                    "Status": "completed",

                    "UserBucket": {

                        "S3Bucket": "images-19159",

                        "S3Key": "VM-Import/lamp-stack.ova"

                    }

                }

            ],

            "Status": "active",

            "StatusMessage": "preparing ami",

            "Tags": [],

            "BootMode": "legacy_bios"

        }

    ]

}

check status again -

{

    "ImportImageTasks": [

        {

            "Architecture": "x86_64",

            "Description": "rhel8-lamp-stack",

            "ImageId": "ami-0bdbf5f024aec408a",

            "ImportTaskId": "import-ami-0ec04dc8c82edceb8",

            "LicenseType": "BYOL",

            "Platform": "Linux",

            "SnapshotDetails": [

                {

                    "DeviceName": "/dev/sda1",

                    "DiskImageSize": 2654299648.0,

                    "Format": "VMDK",

                    "SnapshotId": "snap-041f23c5022f63b85",

                    "Status": "completed",

                    "UserBucket": {

                        "S3Bucket": "images-19159",

                        "S3Key": "VM-Import/lamp-stack.ova"

                    }

                }

            ],

            "Status": "completed",

            "Tags": [],

            "BootMode": "legacy_bios"

        }

    ]

}
 

6. Deploy EC2 instance using AMI

 

Check AWS console for AMI Image which is imported

 
No alt text provided for this image
 

7. Check EC2 instance has all the data and services running fine

 
# dmidecode -s bios-version
4.11.amazon

Using above command we confirm its ec2 instance

Also check LAMP stack services are running fine.

That’s it. I hope you found this to be useful in some way. I’ll be back with some more interesting new cloud and migration articles.

 

Comments

comments


***Linux, Cloud & Devops Architect & Technical Content Writer*** I am a Linux Enthusiast and Supporter/Promoter of Open Source Technology with over 12+ years of experience in Linux, Cloud and Devops. I am A Technical Content writer for various sites like : Hostbread & Golibrary

Related Posts