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 http://www.philchen.com/wp-content/uploads/2009/05/ec2-ami-tools.zip [root@ec2instance]$ unzip ec2-ami-tools.zip [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
/mnt/EC2TOOLS/pk-yourprivatekey.pem /mnt/EC2TOOLS/cert-yourcertificate.pem
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 http://www.philchen.com/wp-content/uploads/2009/05/kernel-modules2616-xenu.tgz [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 http://www.philchen.com/wp-content/uploads/2009/05/kernel-modules-261633-xenu.tgz [root@ec2instance]$ gunzip -c kernel-modules-261633-xenu.tgz | tar -xvf - [root@ec2instance]$ cd /mnt/MOD/ [root@ec2instance]$ mv 2.6.16.33-xenU /lib/modules [root@ec2instance]$ depmod -ae 2.6.16.33-xenU
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.
Excluding:
/sys
/proc
/dev/pts
/proc/sys/fs/binfmt_misc
/dev
/media
/mnt
/proc
/sys
/mnt
Happy snapshotting your AMI

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
I just tried again, and it’s 32-bit now. I didn’t change anything…
[...] a dev-version of one of the sites I am working on. It is pretty damn slick, so far. I’ve been using these instructions to make custom AMIs at each critical point. So when I got the database working…I ran the scripts to save it off [...]
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?
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.
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) http://aws.amazon.com/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.
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
[...] I can understand some of you would like to fire up a base instance and then make modifications and snapshot them into your own custom AMI’s. So I finally found some time to build some and here they [...]