Introduction
While there are plenty of Virtual Machine Image available in EC2 and AWS Marketplace, sometime there is a need to set up the custom virtual machine image such as server migration and autoscaling template.
In such case, when the source images are from other platforms such as VMware vCenter, Hyper-V, and other cloud platforms, we need an approach to upload the virtual machine image. At the same time, we need to export the virtual machine image from EC2 and download it to our on-premises environment for backup and other reasons. In this tutorial, we are going to import/export EC2 Image from/to S3 bucket.
For Importing EC2 Image from S3 bucket, we first upload the source virtual machine image to S3 bucket. After that, we will use the EC2 Image Builder to create the custom virtual machine image and register it as AMI for EC2 to deploy.
For Exporting EC2 Image to S3 bucket, when we have made some configuration changes in EC2 and we want to export it, we will use the AWS CLI export-image command to export the image to S3 bucket and we can download it once it is ready.
Remark:
- When downloading files from S3 bucket to on-premises, there will be the data transfer charge based on the amount of data. [First 100GB of data transfer out to the internet free each month] https://aws.amazon.com/s3/pricing/ https://aws.amazon.com/blogs/architecture/overview-of-data-transfer-costs-for-common-architectures/
- AMI Operating Systems List for AWS EC2: https://aws.amazon.com/marketplace/b/c3bc6a75-0c3a-46ce-8fdd-498b6fd88577?ref_=hmpg_categories_c3bc6a75-0c3a-46ce-8fdd-498b6fd88577&category=c3bc6a75-0c3a-46ce-8fdd-498b6fd88577
Step Summary
Step 1: Create IAM Role for importing and Exporting EC2 Image
Step 2: Create S3 bucket for storing the EC2 Image File.
For Importing EC2 Image from S3 bucket:
Step 3: Uploading the Image to S3
Step 4: Create AMI Image via EC2 Image Builder
For Exporting EC2 Image to S3 bucket:
Step 5: Start the EC2 image exporting task via AWS CloudShell
Step 6: Downloading the Image from S3
Step Details
Step 1: Create IAM Role for importing and Exporting EC2 Image
Go to IAM
Go to Roles
Press Create Role
Select Custom trust policy
Copy the following trust policy to the policy editor and Press Next
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "vmie.amazonaws.com" },
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals":{
"sts:Externalid": "vmimport"
}
}
}
]
}
Press Create Policy
Copy the following IAM policy to the policy editor
Press Next
{
"Version":"2012-10-17",
"Statement":[
{
"Effect": "Allow",
"Action": [
"s3:GetBucketLocation",
"s3:GetObject",
"s3:ListBucket",
"s3:PutObject",
"s3:GetBucketAcl"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"ec2:ModifySnapshotAttribute",
"ec2:CopySnapshot",
"ec2:RegisterImage",
"ec2:Describe*"
],
"Resource": "*"
}
]
}
Name the IAM policy and Press Create Policy
Select the created IAM Policy and Press Next
Name the IAM Role and Press Create Role
Now, you have created the IAM Role for importing and Exporting EC2 Image
Step 2: Create S3 bucket for storing the EC2 Image File.
Go to S3
Name the S3 bucket and select the region
Disable Block all public access and acknowledge it
Now, you have created the S3 bucket for storing the EC2 Image File
For Importing EC2 Image from S3 bucket:
Step 3: Uploading the Image to S3
In this tutorial, Ubuntu 18.04 LTS (Bionic Beaver) VMware VMDK Image File is selected as the EC2 Image file
Donwload Link: https://cloud-images.ubuntu.com/bionic/current/
Go to the created S3 bucket
Select the downloaded VMDK file
Press Upload
Now, you have uploaded the image file to the S3 bucket
Step 4: Create AMI Image via EC2 Image Builder
Go to EC2 Image Builder
Press Import Image
Name the EC2 Image and Version
Select the nearest base image operating system
Browse S3 to select the uploaded Image file
Select the created IAM Role and Press Import Image
Once the import image task has finished, we can see the EC2 image in AMI.
Go to AMIs
In this page, you can see the created EC2 Image and you can deploy the EC2 Image by pressing Launch instance from AMI
For Exporting EC2 Image to S3 bucket:
Step 5: Start the EC2 image exporting task via AWS CloudShell
After changing or deploying some application and configuration in the EC2, we can export it to S3 to back up the current application and configuration status of the virtual machine image.
Select the EC2 Instance and Press Create Image
Name the AMI Image and Press Create Image
Now. you have created the new AMI Image from EC2
In order to export the AMI Image to S3, we are going to use the AWS Cloudshell
Go to AWS Cloudshell
Copy the IAM Role Name (role-name), the AMI id from the created AMI image (image-id) and the S3 Name (s3-export-location) as follows:
aws ec2 export-image \
--role-name EC2ImageImportExportRole \
--image-id ami-0dbeabfbb124c3ae5 \
--disk-image-format VMDK \
--s3-export-location S3Bucket=ec2imagebucketstorage123,\
S3Prefix=exports/
Paste it in AWS Cloudshell
While we can monitor the exporting task by using the following command (export-image-task-ids)
(export-image-task-ids) can be found in ExportImageTaskId Value in the above JSON response when starting the export task
aws ec2 describe-export-image-tasks \
--export-image-task-ids export-ami-0510f25de97082fe0
Step 6: Downloading the Image from S3
Once the EC2 Image has exported, you can find it in the S3 Bucket
Select the exported EC2 Image and Press Download
Reference:
https://docs.aws.amazon.com/vm-import/latest/userguide/vmimport-image-import.html
https://docs.aws.amazon.com/vm-import/latest/userguide/vmexport_image.html
https://docs.aws.amazon.com/cloudshell/latest/userguide/supported-aws-regions.html
https://docs.aws.amazon.com/cli/latest/reference/ec2/export-image.html
Top comments (0)