We will migrate on-prem VM to AWS EC2 using some simple steps
We will use VM Import
to migrate existing VM. This will not only migrate but also preserve the software and settings that we had configured already in our VM
- On-prem server/ VM (In my case , I used Virtual Machine in Oracle VirtualBox)
- AWS Account with Administrator Priveleges (we'll use AWS CLI)
- Export VM
- Upload
.vmdk
file to S3 - Global Customization Variables
- Create Trust Policy
- Create IAM Role
- Create IAM Policy
- Attach IAM Policy to IAM Role
- Import VM Image
- Check the status of VM Image
- Import Completed
- Launch EC2 Machine from AMI
Right click on your VM and click export. Export your file with proper extension either .vmdk
or .ovf
and choose location where you want to export in your Desktop or PC
Go to location where you exported the file. Select .vmdk
file and upload it to S3 bucket
bucket_name="mig-VM-to-bucket"
<!-- Add the appropriate S3 Prefix to the VM Image -->
vm_image_name="VM-Import/vKaliOS-disk001.vmdk"
Now we'll create IAM trust policy in json. Name the file as trust-policy.json
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
Make sure you create role with name vmimport
aws iam create-role --role-name vmimport --assume-role-policy-document "file://trust-policy.json"
We'll attch this policy to the above role created vmimport
Let's name this IAM Policy as role-policy.json
echo '{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":[
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket"
],
"Resource":[
"arn:aws:s3:::'${bucket_name}'",
"arn:aws:s3:::'${bucket_name}'/*"
]
},
{
"Effect":"Allow",
"Action":[
"ec2:ModifySnapshotAttribute",
"ec2:CopySnapshot",
"ec2:RegisterImage",
"ec2:Describe*"
],
"Resource":"*"
}
]
}
' | sudo tee role-policy.json
We'll attach the policy to IAM Role
aws iam put-role-policy --role-name vmimport \
--policy-name vmimport \
--policy-document "file://role-policy.json"
The following command will begin the import of the VM Image.
# Set the metadata,
echo '[
{
"Description": "kalios",
"Format": "vmdk",
"UserBucket": {
"S3Bucket": "'${bucket_name}'",
"S3Key": "'${vm_image_name}'"
}
}]
' > containers.json
Now after executing this command, you'll see StatusMessage: "pending"
The name of our VM Image is containers.json
Let's import this json file now
aws ec2 import-image --description "kalios" --disk-containers "file://containers.json"
Output
{
"Description": "kalios",
"ImportTaskId": "import-ami-0d1db1a95d061e4e3",
"Progress": "2",
"SnapshotDetails": [
{
"DiskImageSize": 0.0,
"Format": "VMDK",
"UserBucket": {
"S3Bucket": "mig-VM-to-bucket",
"S3Key": "VM-Import/vKaliOS-disk001.vmdk"
}
}
],
"Status": "active",
"StatusMessage": "pending"
}
We can see status still pending "StatusMessage": "pending"
Note down ImportTaskId
to check the status of import job
Refer the ImportTaskId
which you noted in previous step and write below mentioned command
aws ec2 describe-import-image-tasks --import-task-ids "import-ami-0d1db1a95d061e4e3"
Output
{
"Description": "kalios",
"ImportTaskId": "import-ami-0d1db1a95d061e4e3",
"Progress": "28",
"SnapshotDetails": [
{
"DiskImageSize": 877762318.0,
"Format": "VMDK",
"Status": "active",
"UserBucket": {
"S3Bucket": "mig-VM-to-bucket",
"S3Key": "VM-Import/vKaliOS-disk001.vmdk"
}
}
],
"Status": "active",
"StatusMessage": "converting"
}
This time status is converting
Wait until its completed....
[root:tmp]# aws ec2 describe-import-image-tasks --import-task-ids "import-ami-0d1db1a95d061e4e3"
{
"ImportImageTasks": [
{
"Architecture": "x86_64",
"Description": "kalios",
"ImageId": "ami-0db67e8436107b5bc",
"ImportTaskId": "import-ami-0d1db1a95d061e4e3",
"LicenseType": "BYOL",
"Platform": "Linux",
"SnapshotDetails": [
{
"Description": "kalios",
"DeviceName": "/dev/sda1",
"DiskImageSize": 877762318.0,
"Format": "VMDK",
"SnapshotId": "snap-0bc9d11c87634b2c3",
"Status": "completed",
"UserBucket": {
"S3Bucket": "mig-VM-to-bucket",
"S3Key": "VM-Import/vKaliOS-disk001.vmdk"
}
}
],
"Status": "completed"
}
]
}
Here we go.... out status is Completed now
Now when you go to AMI, you will see new AMI created
Now you can simply right click on the AMI and create EC2 machine from it
Boom!! we have successfully migrated our on-prem VM to AWS