[Step-by-Step] How to import/export EC2 Image from/to S3 bucket
Tags: tutorial, aws, beginners
Published: 2023-07-07 07:25:10

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mef1d3sao295hy15elpa.png)
Go to **Roles**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xi9mv5ycpiv55g7e322c.png)
Press **Create Role**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qm3893xiqyk7l2m8ua66.png)
Select **Custom trust policy**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m01cha7hgcxu76dmenq9.png)
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"
            }
         }
      }
   ]
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4n8jnzvplpyowiucblgq.png)

Press **Create Policy**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hcxtw63nobmmp8u0gk2n.png)



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": "*"
      }
   ]
}
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3yltnov0l4tty1g0vhp.png)

Name the IAM policy and Press **Create Policy**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pk9pqo258rqdd5olnhkk.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nw22vtnvsy3g6z3d52lq.png)

Select the created IAM Policy and Press **Next**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0n5t82y8sezc1d8tml9.png)
Name the IAM Role and Press **Create Role**


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0c03lj7xs7638ma36ki.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1bc15xx0x0us85ycghpo.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i7l9o2s1hiulvtl0bcls.png)
### Step 2: Create S3 bucket for storing the EC2 Image File.
Go to S3

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11h0zk1gs9jjazkc7dnc.png)
Press **Create bucket**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xk5d9e46d07iz9o1gxnm.png)

Name the S3 bucket and select the region


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fs6i8grxjekg50zdjvoq.png)


Disable **Block all public access** and acknowledge it

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxrbffwzyth7hcvef0tv.png)

Press **Create bucket**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66peiecgag5uvhuvk2g7.png)

Now, you have created the S3 bucket for storing the EC2 Image File
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8xgeke9jwnfzrlw9lw6k.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u82egi201k5de9r650x1.png)



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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bece1owawj0kld2vp3p9.png)
Press **Upload**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pin4dn6hstssp51x2afx.png)
Press **Add files**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k2wosb83o30wywnbuivz.png)
Select the downloaded VMDK file

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ux5br3diwvjy85f9xfz.png)


Press **Upload**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tdg04pryprfj0ske3bv.png)



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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5jpusm2o6gbu4g0hs9t2.png)







### Step 4: Create AMI Image via EC2 Image Builder
Go to **EC2 Image Builder**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hff2kupbiatpw72azp99.png)

Go to **Images**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a90fj8khshx11kai35q5.png)

Press **Import Image**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3a0zosxnrdsdj7u7kv1.png)
Name the EC2 Image and Version


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2g893946cb2ffu9dzzw8.png)


Select the nearest base image operating system


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ce63odpfdn994j0jpx4n.png)


Browse S3 to select the uploaded Image file


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/alvhdr41l62vgqn4eujc.png)



Select the created IAM Role and Press **Import Image**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4101yogy2wsfvjmt1iv6.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8iq7af1julwhlzfrpthh.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/setlc7yiuoc3s4wevuyr.png)



Go to AMIs

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pok2kv573i0v5w33mudz.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6e0s47sonawqme36xvys.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwpbjc9xfza8vxe43evb.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/itd12qoj3vdssw8qqh19.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufvk43f47i2h4h1z7wfy.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lnsnuv40ekljsnkxh5oc.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhf6dpafvhl8708mp723.png)
Name the AMI Image and Press **Create Image**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/req4bvf1wmu4urh09ul0.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/335xz8xnxtb16j34pigu.png)

Now. you have created the new AMI Image from EC2

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wc0kgz8gwlkosz2pc948.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xtue1lky2zlvonlirr98.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5bj9d1hr0zp5xxoe6mm.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pz0xxxvv36ay8tjtw0wj.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubh2gc79e6j09dnffzb1.png)

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
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5txh7rnh7kb1uo22xq3l.png)

### Step 6: Downloading the Image from S3

Once the EC2 Image has exported, you can find it in the S3 Bucket
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h0ufq76fbyretduknw78.png)

Go to **exports/**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9praakkut52ipatq7o8q.png)

Select the exported EC2 Image and Press **Download**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijs7tefph7p4scuvm5io.png)

**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