Tuesday, April 12, 2016

Export a Linux virtual machine from VMWare EXSI server and Import to AWS



We can export our EXSI virtual machine images to aws and create the exact same machine as your are running on your EXSI machine.

For this process we need to do the following steps.

1. Download and install AWS CLI
2. Configure the access key with your aws console (IAM access)
3. Export the virtual machine from EXSI machine to your local machine as ova formate
4. Import the OVA image file to your aws S3 bucket


Setup AWS CLI

Download the AWS CLI here:
https://s3.amazonaws.com/aws-cli/AWSCLI64.msi
After that complete the installation.

[root@ip-172-31-56-125 ec2-user]# aws configure
AWS Access Key ID [None]: AKIAXXXXXXXWHTA
AWS Secret Access Key [None]: svcngBGXXXnijOWPLDXXXXXXXHaqs
Default region name [None]: us-east-1
Default output format [None]:
[root@ip-172-31-56-125 ec2-user]#


After the configuration we can test and confirm everything seems to be working fine.

[root@ip-172-31-56-125 ec2-user]# aws ec2 create-security-group --group-name AJVMGRP --description "testing vmtestimptest"
{
    "GroupId": "sg-2a250452"
}
[root@ip-172-31-56-125 ec2-user]#

So we have successfully configured the AWS CLI on this machine


[root@ip-172-31-56-125 ec2-user]# aws ec2 import-image --cli-input-json "{ \"Description\": \"Kalilinux-2016\", \"DiskContainers\": [ {\"Description\": \"Kalilinux-2016\", \"UserBucket\": { \"S3Bucket\": \"vmimportajeesh\", \"S3Key\" : \"CentOS-6.4-i386-minimal.ova\" } } ]}"

{
    "Status": "active",
    "Description": "Kalilinux-2016",
    "Progress": "2",
    "SnapshotDetails": [
        {
            "UserBucket": {
                "S3Bucket": "vmimportajeesh",
                "S3Key": "CentOS-6.4-i386-minimal.ova"
            },
            "DiskImageSize": 0.0
        }
    ],
    "StatusMessage": "pending",
    "ImportTaskId": "import-ami-fggrwl9r"
}
[root@ip-172-31-56-125 ec2-user]#

On Our EXSI Box
We need to create a vm. The VM should be capable of accepting DHCP assign IP address and remove vmware-tool if we installed any.

Export VM in OVA format

File > Export > Export OVF Template [Single format]

 Create a S3 bucket

 Create a S3 bucket and make note the ARN

Import the OVA file to S3 Bucket
You can upload the particular OVA file to your S3 bucket

We need to change the service name to vmie.amazoneaws.com(Trust Relationship) and add the following condition

Create a new role[eg: vmimport] and assign administrative privilege
 {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "vmie.amazonaws.com"
},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"sts:ExternalId": "vmimport"
}
}
}
]
}
 


Import the OVA to AWS


[root@ip-172-31-56-125 ec2-user]# aws ec2 import-image --cli-input-json "{ \"Description\": \"Kalilinux-2016\", \"DiskContainers\": [ {\"Description\": \"Kalilinux-2016\", \"UserBucket\": { \"S3Bucket\": \"vmimportajeesh\", \"S3Key\" : \"CentOS-6.4-i386-minimal.ova\" } } ]}"
-----
{
    "Status": "active",
    "Description": "Kalilinux-2016",
    "Progress": "2",
    "SnapshotDetails": [
        {
            "UserBucket": {
                "S3Bucket": "vmimportajeesh",
                "S3Key": "CentOS-6.4-i386-minimal.ova"
            },
            "DiskImageSize": 0.0
        }
    ],
    "StatusMessage": "pending",
    "ImportTaskId": "import-ami-fggrwl9r"
}
---

We will get a image ID [ import-ami-fggrwl9r ]

to check the status::
aws ec2 describe-import-image-tasks --image-task-ids “import-ami-fggrwl9r”


[root@ip-172-31-56-125 ec2-user]# aws ec2 describe-import-image-tasks --import-task-ids "import-ami-fggrwl9r"
{
    "ImportImageTasks": [
        {
            "Status": "active",
            "Description": "Kalilinux-2016",
            "Progress": "28",
            "SnapshotDetails": [
                {
                    "UserBucket": {
                        "S3Bucket": "vmimportajeesh",
                        "S3Key": "CentOS-6.4-i386-minimal.ova"
                    },
                    "DiskImageSize": 346110976.0,
                    "Format": "VMDK"
                }
            ],
            "StatusMessage": "converting",
            "ImportTaskId": "import-ami-fggrwl9r"
        }
    ]
}
[root@ip-172-31-56-125 ec2-user]#


then this will change to

   "StatusMessage": "updating",
   "ImportTaskId": "import-ami-fggrwl9r"

   "StatusMessage": "booting",
   "ImportTaskId": "import-ami-fggrwl9r"

  "StatusMessage": "preparing ami",
  "ImportTaskId": "import-ami-fggrwl9r"

[root@ip-172-31-56-125 ec2-user]# aws ec2 describe-import-image-tasks --import-task-ids "import-ami-fggrwl9r"
{
    "ImportImageTasks": [
        {
            "Status": "completed",
            "LicenseType": "BYOL",
            "Description": "Kalilinux-2016",
            "ImageId": "ami-4e6e7924",
            "Platform": "Linux",
            "Architecture": "x86_64",
            "SnapshotDetails": [
                {
                    "UserBucket": {
                        "S3Bucket": "vmimportajeesh",
                        "S3Key": "CentOS-6.4-i386-minimal.ova"
                    },
                    "SnapshotId": "snap-649ee37d",
                    "DiskImageSize": 346110976.0,
                    "DeviceName": "/dev/sda1",
                    "Format": "VMDK"
                }
            ],
            "ImportTaskId": "import-ami-fggrwl9r"
        }
    ]
}
Finally this image is available on your AWS console. Using this you can launch a new vps.