How to Save a Snapshot of your Amazon EC2 Instance

May 19, 2009

This how to article will go over saving a snapshot of your Amazon Elastic Compute Cloud (EC2) Machine Image (AMI). This is very useful for taking an existing AMI and modifying it to customize your needs into a new separate AMI.

For my intents and purposes I have a base CentOS 5.3 AMI with only the bare essentials. I then use this snapshot method to create different class AMI’s for different server functions ie. (webserver, memcached server, etc)

First thing is first you are going to need to have an existing AMI created, you can read how to do so in my other blog article How to create an Amazon Elastic Compute Cloud EC2 Machine Image (AMI). Once you have this AMI created and you launch the instance the fun begins to modify it and then take a snapshot for a new AMI based on the original AMI.

So once you launch your instance off your base AMI, you simply connect to your Amazon EC2 instance and install all the software you need to serve your purpose as you would any other server. After you have completed all the software installs and customizations, you then can begin the snapshot process.

The first step is to create a directory and download the Amazon EC2 Tools

[root@ec2instance]$ mkdir /mnt/EC2TOOLS
[root@ec2instance]$ cd /mnt/EC2TOOLS
[root@ec2instance]$ wget
[root@ec2instance]$ unzip
[root@ec2instance]$ cd ec2-ami-tools-1.3-31780
[root@ec2instance]$ mv * ../
[root@ec2instance]$ cd ../
[root@ec2instance]$ rmdir ec2-ami-tools-1.3-31780/

Now scp over your AWS private and public certificate


You want to setup your .bashrc to know where your tools are

[root@ec2instance]$ vi ~/.bashrc
export PATH=$PATH:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin
export EC2_HOME=/mnt/EC2TOOLS
[root@ec2instance]$ source ~/.bashrc

Now you will want to download the correct kernel modules:

If your creating a snapshot of a 32bit AMI and don’t have the proper xen kernel modules do the following:

[root@ec2instance]$ mkdir /mnt/MOD
[root@ec2instance]$ cd /mnt/MOD
[root@ec2instance]$ wget
[root@ec2instance]$ gunzip -c kernel-modules2616-xenu.tgz | tar -xvf -
[root@ec2instance]$ cd /mnt/MOD/lib/modules
[root@ec2instance]$ mv 2.6.16-xenU /lib/modules
[root@ec2instance]$ depmod -ae 2.6.16-xenU

If your creating a snapshot of a 64bit AMI and don’t have the proper xen kernel modules do the following:

[root@ec2instance]$ mkdir /mnt/MOD
[root@ec2instance]$ cd /mnt/MOD
[root@ec2instance]$ wget
[root@ec2instance]$ gunzip -c kernel-modules-261633-xenu.tgz | tar -xvf -
[root@ec2instance]$ cd /mnt/MOD/
[root@ec2instance]$ mv /lib/modules
[root@ec2instance]$ depmod -ae

Now you will want to create a location to store the image files

[root@ec2instance]$ mkdir /mnt/EC2IMAGE

At this step you are ready to create the EC2 image files and upload them to S3

[root@ec2instance]$ cd /mnt/EC2TOOLS
[root@ec2instance]$ bin/ec2-bundle-vol -d /mnt/EC2IMAGE --privatekey pk-yourprivatekey.pem --cert cert-yourcertificate.pem -u amazonaccountnumber -r i386 or x86_64 -p imagename
[root@ec2instance]$ bin/ec2-upload-bundle -b yours3bucketname -m /mnt/EC2IMAGE/whatever.manifest.xml -a accesskeyid -s secretaccesskey

Now on your local machine that you start instances from register the new AWS AMI

[phil@client]$ /Users/phil/EC2/bin/ec2-register yourbucketname/thenameofthenewami.manifest.xml

At this point you are done, and ready to fire up your new AMI instance whenever you like with your changes. Note the below directories have been excluded during the bundle process so your certs and secret info will be excluded, but also note anything you installed in these directories will not be kept though as well.


Happy snapshotting your AMI 🙂

Also a great resource for free Realtime AWS uptime data is Systems Watch

Comments for “How to Save a Snapshot of your Amazon EC2 Instance”

  1. Carl Nelson commented on October 29, 2009

    I followed this guide (thanks!) but when I try to launch an EC2 from it, it only lets me choose a 64-bit instance. What gives? I used 32 bit to create it…

    Thanks again, I’ll keep an eye on this while I try to figure out what went wrong 😉

  2. Carl Nelson commented on October 29, 2009

    I just tried again, and it’s 32-bit now. I didn’t change anything…

  3. one thing i was wondering if you would answer for me…i am having some troubles now using this new AMI that I’ve created. mind you, I’ve rolled in mysql and some tables and put some data into those tables…

    when i try to instantiate the AMI it starts up, but when i visit the public URL, i’m getting slow response times, and important parts of the site (stylesheets & images) seem to go missing.

    i wonder if i’m bumping up against that 1.7GB limit i see in the EC2 web interface? Would it be best to maybe look into Simple DB instead of putting my MySQL data into the AMI?

  4. Very nice tutorial. Thanks heaps for this.
    Followed it step by step and voila, AMI snapshot saved to S3, registered. And when I launched it: it worked.

  5. the0ther, I tend to stay away from making an EC2 instance run MySQL. You might want to try Amazon’s Relational Database Service (Amazon RDS)

    Personally I run my databases still in the data center, not in the cloud. I might change that soon but still haven’t made the leap.

  6. binbintriangel commented on February 18, 2010

    thanks for your tutorial, really helpful

    would like to ask: is bundling the correct kernel modules in to AMI really necessary??

    i had a websphere AMI running, took the snapshot as your steps except the kernel module, it still works

    note that you do need the same keypair file as the AMI took snapshot before, i tried using different keypair, it doesn’t work

  7. Matt commented on November 23, 2010

    If amazon doesn’t jive well with mysql, then what good is it? Many webapps use php and MySQL. Maybe it is time to start shopping around for alternatives :-/

    • Hi Matt,

      It isn’t that Amazon doesn’t jive well with MySQL, but more that in my opinion since EC2 is designed to be a bit more transient in nature, the only method I would recommend is using a EBS backed AMI so that your MySQL data is still saved in a permanent nature. The other alternative which is a bit expensive is RDS which has built in read replica functionality, and more robust data integrity. EC2 can be whatever you want it to be, but I tend to be cautiously moving into the cloud starting with the low hanging fruit of services that can fail but are clustered versus dealing with MySQL replication issues and other things of that nature.

  8. Awesome, this helped out alot!!

  9. styelz commented on February 18, 2012

    After trying to boot from an EC2 instance created from a snapshot. I couldnt not boot it due to missing xen modules.

    I found that I needed to select the same “Kernel ID” as the original Instance “Kernel ID” when creating an AMI from a snapshot.

    Then, when you goto create an Instance from the AMI, the “Kernel ID” is already set. And the Instance boots as expected.