Export AMI to VMDK and Store in S3 Bucket

Rushabh Mahale
7 min readApr 17, 2023

--

Create Ec2 and Configure the web server

Step 1 — Go to Ec2 in aws. And launch 2 vm 1 for aws-cli where we will configure all the steps and another one will be your data vm where we will store our data and web server and migrate it You can refer to this link.

Step 2 — Connect to the rushabh-ami-vm and install apache and also mount the EBS volume.

sudo apt-get update -y 
sudo apt-get install apache2 -y
sudo systemctl start apache2
sudo systemctl status apache2
cd /var/www/html
vi pop.html
<body bgcolor = ‘aqua’> 
<h1> Welcome to my webpage my name is Rushabh </h1>
</body>

Note — Make sure to HTTP in your security group for the Apache webserver

Step 3 Create EBS Volume of 10 GB and attach to Vm

Select Zone and vm

Step 4 Mount the EBS Voume

sudo lsblk
sudo mkfs.ext4 /dev/xvdf
sudo mkdir /rushabh
sudo mount /dev/xvdf /rushabh
df -h
cd /rushabh
mkdir pop rushabh webpage
echo “hello from aws” > test.txt
sudo nano /etc/fstab
Add this line remove bracket -> ( /dev/xvdf /data ext4 defaults 0 0 )
# Comment the given line mention above /dev/xvdf
sudo mount -a

Create Ec2 to AMI

What is AMI?

An AMI is a pre-configured virtual machine image that contains all the necessary information to launch an instance (virtual machine) in the cloud. An AMI includes an operating system, any required software, libraries, and configuration settings, and can be customized to include any specific applications or services required for a particular use case. Using an AMI, you can launch a new instance that is identical to the original instance, which can save time and effort in configuring instances for different use cases. AWS provides a large collection of pre-built AMIs for popular operating systems and applications, and users can also create their own custom AMIs for their specific needs.

Refer this link to more about ami.

Step1 go to ec2 select vm rushabh-ami-vm

Note — Also add EBS Volume while creating Image

This will take time 5–10min to create an image depending upon your data. More data more time

Export AMI to VMDK and Store in S3

What is VMDK

VMDK stands for Virtual Machine Disk, which is a file format used by virtualization software, such as VMware Workstation, VMware vSphere, and VirtualBox, to store a virtual machine’s hard disk drive.

A VMDK file contains the contents of a virtual machine’s hard drive, including the operating system, applications, and data. It is typically stored on a physical hard drive and accessed by the virtual machine as if it were a physical hard drive.

What is S3 Bucket

Amazon S3 (Simple Storage Service) is an object storage service provided by Amazon Web Services (AWS). An S3 bucket is a container for storing objects (files) in S3.Think of an S3 bucket as a virtual hard drive in the cloud where you can store and retrieve any type of data, such as documents, images, videos, audio files, and more. S3 is designed to provide high durability, availability, and scalability for storing and retrieving data from anywhere in the world.

Refer to this link to more about s3.

Step 5 Create S3 bucket in Mumbai region. Also enable ACL with Object Ownership Bucket owner preferred

Step 6 in the Permission section of S3 bucket edit ACL section and add Grantee id is mention in this link choose All other regions

Check to mark this permission For — 1) Object- Write

2)Bucket ACL — Read

Step 7 Now go to rushabh-aws-cli vm and install aws-cli using this command as mention below

sudo apt-get update -y 
sudo apt-get install awscli -y
aws -version

Step 8 Create User and create 2 inline policy

vi s3-allow-policy

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetBucketLocation",
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::mys3bucket",
"arn:aws:s3:::mys3bucket/*"
]
},
{
"Effect": "Allow",
"Action": [
"ec2:CancelConversionTask",
"ec2:CancelExportTask",
"ec2:CreateImage",
"ec2:CreateInstanceExportTask",
"ec2:CreateTags",
"ec2:DescribeConversionTasks",
"ec2:DescribeExportTasks",
"ec2:DescribeExportImageTasks",
"ec2:DescribeImages",
"ec2:DescribeInstanceStatus",
"ec2:DescribeInstances",
"ec2:DescribeSnapshots",
"ec2:DescribeTags",
"ec2:ExportImage",
"ec2:ImportInstance",
"ec2:ImportVolume",
"ec2:StartInstances",
"ec2:StopInstances",
"ec2:TerminateInstances",
"ec2:ImportImage",
"ec2:ImportSnapshot",
"ec2:DescribeImportImageTasks",
"ec2:DescribeImportSnapshotTasks",
"ec2:CancelImportTask"
],
"Resource": "*"
}
]
}

vi iam-create-policy

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"iam:CreateRole",
"iam:PutRolePolicy"
],
"Resource": "*"
}
]
}

Now add these 2 policy to your IAM user that you have created in the Permissions options select Attach policies directly

now create programmatic key access and secret key and use this command to configure

Aws configure

Add access and secret key and region also

Step 9 now let’s create role and policy to attach to role for ami to vmdk follow this link. To create the service role

vi trust-policy.json

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "vmie.amazonaws.com" },
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals":{
"sts:Externalid": "vmimport"
}
}
}
]
}

Step 10 use this command to create vm-import role

aws iam create-role — role-name vmimport — assume-role-policy-document “file://trust-policy.json”

You should see this kind of output mentioned below screenshot

Step 11 attach policy to vm-import role to perform action ami to vmdk change bucket name in policy replace <BUCKET_NAME> to rushabh-s3–1092

vi role-policy.json

{
"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": [
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:PutObject",
"s3:GetBucketAcl"
],
"Resource": [
"arn:aws:s3:::<BUCKET_NAME>",
"arn:aws:s3:::<BUCKET_NAME>/*"
]
},
{
"Effect": "Allow",
"Action": [
"ec2:ModifySnapshotAttribute",
"ec2:CopySnapshot",
"ec2:RegisterImage",
"ec2:Describe*"
],
"Resource": "*"
}
]
}

This command will create a policy and attach it to the role

aws iam put-role-policy — role-name vmimport — policy-name vmimport — policy-document “file://role-policy.json”

Step 12 last step use this command to convert ami to vmdk and store your vmdk in s3 bucket that you have mentioned in this command. Let's Start an export image task. Refer to this link.

Go to ami section copy amid and paste this command

aws ec2 export-image — image-id <ami-id> — disk-image-format VMDK — s3-export-location S3Bucket=<BUCKET_NAME>,S3Prefix=exports/

You should see this kind of output

Now copy export id and use this command to describe your job and observe the status message

aws ec2 describe-export-image-tasks — export-image-task-ids export-ami-1234567890abcdef0

This will take time depends upon your data in ami

There are two vmdk files: one is your boot disk, which contains your operating system and data, and the other is your EBS volume data disk, which you can now obtain and put into your on-premises Oracle virtual box or migrate to GCP. Where I have demonstrated the next stage

Follow this link for Migrating VMDK AWS to GCP

Conclusion

Converting an AMI to a VMDK and uploading it to AWS provides several advantages, including the ability to use the VMDK as a virtual machine for cloud testing, development, and application execution. This accomplishment also marks the effective integration of many technologies and platforms, which increases flexibility and adaptability in establishing virtual machines in the cloud and producing backups. Overall, it’s a tremendous achievement that opens up new avenues for cloud computing.

In case of any questions regarding this article, please feel free to comment in the comments section or contact me via LinkedIn.

I want to thank my team at Guysinthecloud for all of their help.

Thank You

--

--

Responses (1)