DEV Community

Cover image for [Step-by-Step] How to import/export EC2 Image from/to S3 bucket
Lam Wing Chun
Lam Wing Chun

Posted on

[Step-by-Step] How to import/export EC2 Image from/to S3 bucket

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:

  1. 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/
  2. 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
Image description
Go to Roles
Image description
Press Create Role
Image description
Select Custom trust policy

Image description
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"
            }
         }
      }
   ]
}
Enter fullscreen mode Exit fullscreen mode

Image description

Press Create Policy

Image description

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": "*"
      }
   ]
}
Enter fullscreen mode Exit fullscreen mode

Image description

Name the IAM policy and Press Create Policy

Image description

Image description

Select the created IAM Policy and Press Next

Image description
Name the IAM Role and Press Create Role

Image description

Image description

Now, you have created the IAM Role for importing and Exporting EC2 Image

Image description

Step 2: Create S3 bucket for storing the EC2 Image File.

Go to S3

Image description
Press Create bucket

Image description

Name the S3 bucket and select the region

Image description

Disable Block all public access and acknowledge it

Image description

Press Create bucket
Image description

Now, you have created the S3 bucket for storing the EC2 Image File
Image description

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

Image description

Donwload Link: https://cloud-images.ubuntu.com/bionic/current/
Go to the created S3 bucket

Image description
Press Upload

Image description
Press Add files

Image description
Select the downloaded VMDK file

Image description

Press Upload

Image description

Now, you have uploaded the image file to the S3 bucket

Image description

Step 4: Create AMI Image via EC2 Image Builder

Go to EC2 Image Builder

Image description

Go to Images
Image description

Press Import Image

Image description
Name the EC2 Image and Version

Image description

Select the nearest base image operating system

Image description

Browse S3 to select the uploaded Image file

Image description

Select the created IAM Role and Press Import Image

Image description

Once the import image task has finished, we can see the EC2 image in AMI.

Image description

Image description

Go to AMIs

Image description
In this page, you can see the created EC2 Image and you can deploy the EC2 Image by pressing Launch instance from AMI

Image description

Image description

Image description

Image description

Image description

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
Image description
Name the AMI Image and Press Create Image

Image description

Image description

Now. you have created the new AMI Image from EC2

Image description

In order to export the AMI Image to S3, we are going to use the AWS Cloudshell
Go to AWS Cloudshell

Image description

Image description

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/
Enter fullscreen mode Exit fullscreen mode

Paste it in AWS Cloudshell

Image description

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

Step 6: Downloading the Image from S3

Once the EC2 Image has exported, you can find it in the S3 Bucket
Image description

Go to exports/
Image description

Select the exported EC2 Image and Press Download
Image description

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)