How do I export an AMI to an Amazon S3 bucket?

3 minute read
0

I want to export a copy of my Amazon Machine Image (AMI) to an Amazon Simple Storage Service (Amazon S3) bucket for offline modifications or storage.

Resolution

Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.

Export the AMI to an Amazon S3 bucket

To export an AMI to an Amazon S3 bucket, you must create a store image task.

Prerequisites: The AWS Identity and Access Management (IAM) principal that you use to run the command must have the appropriate permissions. The Amazon S3 bucket policy must allow the IAM role permission to upload the AMI to the bucket. To create or edit an S3 bucket policy, see Adding a bucket policy by using the Amazon S3 console.

Run the create-store-image-task command to export the AMI to an Amazon S3 bucket in a raw format:

aws ec2 create-store-image-task \
    --image-id AMI_ID \
    --bucket BUCKET_NAME

Note: Replace AMI_ID with your AMI ID and BUCKET_NAME with the name of your bucket.

Check the progress of the export task

After the export task starts, run the describe-store-image-tasks command to check the task's status:

aws ec2 describe-store-image-tasks

Download the exported AMI from Amazon S3

After the export completes, download the raw image file from the Amazon S3 bucket. Use AWS CLI, or manually download the object from the Amazon S3 console.

To download the exported AMI, run the following command:

aws s3 cp s3://my-ami-exports/ami-backups/image.bin ./image.bin

The image.bin raw file is exported from the ami-backups prefix that's in the bucket named my-ami-exports.

Note: You can't directly modify the AMI. You must download the raw image file, and then make changes.

Restore the image from Amazon S3

To restore the image to your Amazon Elastic Compute Cloud (Amazon EC2) instance from Amazon S3, run the create-restore-image-task command:

aws ec2 create-restore-image-task \
--object-key image.bin \
--bucket BUCKET_NAME \
--name "New AMI Name"

Note: Replace image.bin with your image name and BUCKET_NAME with the name of your bucket.

If you modify the raw image file, then restore the file as a new AMI to use within your AWS environment.

For information about use cases and limitations, see Store and restore an AMI using S3.

Related information

Create store and restore image tasks

Make your AMI publicly available for use in Amazon EC2

Examples of Amazon S3 bucket policies

AWS OFFICIAL
AWS OFFICIALUpdated a month ago