Amazon Elastic Compute Cloud Walkthrough
Jack Amazon Web Services has recently made available a beta service called the Amazon Elastic Compute Cloud which allows for rapid deployment of virtual servers. As some of the documentation was wrong and some of the resource materials were incomplete, we thought that it might be useful to create a walkthrough for the entire process of developing server images, transferring them to Amazon’s Simple Storage Service, and subsequently deploying instances of those server images.
First, you’ll need to sign up for an Amazon Web Services account. Once you have an account, you will be provided with a link to your “AWS Access Identifiers” page. From here, you’ll need an X.509 Certificate which you can create and download at the bottom of the page. You will get a public certificate and a private key. I saved mine to:
~/ec2/auth/cert-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem
~/ec2/auth/pk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem
I also saved my Access Key ID and Secret Access Key to
~/ec2/auth/access_key_id
~/ec2/auth/secret_access_key
You’ll also need your Account Number, which can be found at the top of your Account Summary. Of course, I saved mine to:
~/ec2/auth/account_number
Next, there are two sets of API tools that you’ll need to download from the Resource Center: The “Amazon EC2 AMI Tools” which will help you create, bundle, and upload your custom server images, and the “Amazon EC2 Command-Line Tools” which allow you to manage your uploaded AMI images as well as instances of those images. Note: The AMI Tools are currently only available as an RPM. I installed my Command Line Tools under
~/ec2/api
These tools tend to be a bit finicky, as they need environment variables set in order to run correctly. Customize the following according to your setup, and then you can either paste it into your shell, or paste it into the bottom of your ~/.bash_profile if you’d like them set every time you login:
export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun-1.5.0.06/ export EC2_HOME=~/ec2/api export PATH=$PATH:$EC2_HOME/bin export EC2_PRIVATE_KEY=~/ec2/auth/pk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem export EC2_CERT=~/ec2/auth/cert-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem
My java binary is in /usr/lib/jvm/java-1.5.0-sun-1.5.0.06/bin/java , yours may be somewhere else.
If everything has been set up correctly up to this point, run ~/ec2/api/bin/ec2-describe-images, and you should get the following output after a couple of seconds:
IMAGE ami-5bae4b32 ec2-public-images/getting-started.manifest 206029621532 available public IMAGE ami-68ae4b01 ec2-public-images/fedora-core4-base.manifest 206029621532 available public IMAGE ami-69ae4b00 ec2-public-images/fedora-core4-apache-mysql.manifest 206029621532 available public IMAGE ami-6dae4b04 ec2-public-images/fedora-core4-apache.manifest 206029621532 available public IMAGE ami-6fae4b06 ec2-public-images/fedora-core4-mysql.manifest 206029621532 available public
This is a listing of the publicly available images on the EC2 service. You COULD instantiate any one of them into a virtual server. Instead, we’re going to create our own custom image!
dd if=/dev/zero of=ubuntu.fs count=1024 bs=1M
Creates an empty 1 gig loopback file called “ubuntu.fs”mke2fs -F -j ubuntu.fs
Creates a filesystem for the filesudo mount -o loop ubuntu.fs /mnt
Mounts the loopback file under /mntsudo debootstrap dapper /mnt
Uses the debootstrap utility to install ubuntu’s core packages into /mnt. If you don’t have debootstrap, you can install it withapt-get install debootstrap.sudo cp /etc/apt/sources.list /mnt/etc/apt/sources.list
Copy your apt source list to the target filesystemsudo chroot /mnt
Effectively change the target filesystem (/mnt) to be your new root (/). This is where we’ll be doing most of the setup for the server image.- Use
passwdto update the image’s root password. Don’t lose it. apt-get updateand thenapt-get upgrade
Updates apt’s package cache and then updates any of the base packages installed by debootstrap.localedef -i en_US -c -f UTF-8 en_US.UTF-8
This sets your locale variables.apt-get install openssh-server nano subversion rsync man
Install some base packages. Tweak this according to your needs.- If you want to install Ruby on Rails, do these as well:
apt-get install ruby ri rdoc mysql-server libmysql-ruby lighttpd
sudo wget http://rubyforge.org/frs/download.php/11289/rubygems-0.9.0.tgz
tar -xvzf rubygems-0.9.0.tgz
cd rubygems-0.9.0
sudo ruby setup.rb
sudo gem install rails --include-dependencies
And maybe install some of the awesome gems that go along with it:
sudo gem install capistrano redcloth bluecloth --include-dependencies - Paste the following code into the file
/etc/fstab/dev/sda2 /mnt ext3 defaults 1 2 /dev/sda3 swap swap defaults 0 0
- Paste the following code into the file
/etc/network/interfacesauto lo iface lo inet loopback auto eth0 iface eth0 inet dhcp
- Next, I go about setting up my non-root user accounts with
adduser. If there are any remaining packages or configuration steps you want in your default image, go ahead and do them now. - Note: Make sure that you don’t have any processes running from within the /mnt filesystem. You might need to use
/etc/init.d/lighttpd stopor/etc/init.d/mysql stop. Once you’ve confirmed this, useexitto back out of the mounted /mnt filesystem, and thensudo umount /mntto unmount it.
Your base server is now fully contained in the “ubuntu.fs” loopback file! Now, it’s time to use the Amazon AMI tools to begin the bundling process.
ec2-bundle-image -i ubuntu.fs -u [user account number from ~/ec2/auth/account_number]
This will take a while. The tool will break your ubuntu.fs file up into 10 meg pieces and encrypt them.ec2-upload-bundle -b test-image -m image.manifest -a [key from ~/ec2/auth/access_key_id] -s [secret key from ~/ec2/auth/secret_access_key]
This will upload the bundles from the previous step to Amazon’s S3 service and put them in the “test-image” bucket.ec2-register test-image/image.manifest
This will register your image with EC2 in order to make it available for instantiation.
Congratulations! You’ve created an image file, bundled it, uploaded it to S3, and registered it on the EC2 system. In the next entry, we’ll show you how to make virtual servers from your image.
Immense thanks to Doug Winter for getting us going on the basics, most of which have been replicated and expanded here.
Posted in ec2, walkthrough |
October 6th, 2006 at 2:07 am
The regular umount didn’t work for me, instead I had to do a lazy umount:
sudo umount -l /mnt
October 6th, 2006 at 1:54 pm
Some other details that may be useful to future readers:
Step: Install/Test/Configure API tools
————————————–
EC2 is kind of redhat specific; it only makes things available as rpms currently. Since we’re using ubuntu, there are some prep steps.
Install Curl
————
* sudo apt-get install curl
Install Ruby
————
From synaptics package manager, select the following
ruby
* libruby1.8
* ruby1.8
* libopenssl-ruby
* libopenssl-ruby1.8
Define RUBYLIB environment variable in .bashrc
* export RUBYLIB=/usr/lib/ruby/1.8:/usr/local/lib/1.8/i486-linux:/usr/lib/site_ruby
Install alien package converter application
——————————————-
Use https://ubuntu.wordpress.com/2005/09/23/installing-using-an-rpm-file/ to find out how to install rpm files
* sudo apt-get install alien
Download ec2 tools rpm
———————-
* http://developer.amazonwebservices.com/connect/entry.jspa?entryID=368&ref=featured
Convert rpm to deb using alien
——————————
* sudo alien -k ec2-ami-tools.noarch.rpm
* result: ec2-ami-tools_1.2-5674_all.deb generated
Install deb package
——————-
* sudo dpkg -i ec2-ami-tools_1.2-5674.deb
Test ec2 ami tools works
————————
* ec2-bundle-image –help
** if error received for finding crypto.rb, then your RUBYLIB doesn’t contain /usr/lib/site_ruby
** if error received for openssl, then you didn’t install package libopenssl-ruby and libopenssl-ruby1.8
Other Details
————-
ec2-upload-bundle
* this step took many hours to complete. I recommend you kick this step off before you go to bed and check it in the morning :). My install added another 100M+ on top of the standard install and I wound up with 30 part files in my directory.
October 6th, 2006 at 2:44 pm
Thanks for taking the time to record this. It will be a big help to a lot of people who aren’t as familiar with EC2 or building images as you are!
One little thing, however. You might want to mention that these are Ubuntu-specific instructions so that someone running a different O/S (anything from Mac OS X to just plain Windows) won’t be able to follow these. Perhaps instructions on how to build this on the Fedora image on an actual EC2 machine might be more helpful =)
October 28th, 2006 at 8:52 pm
This is FANTASTIC. What’s wrong with ubuntu? It’s so much more friendlier than FC. Take an x86 at home and use this to make an Ubuntu EC2 machine. Then you can use that machine to make more machines! Yeah!
I just want to be able to work on a machine and suspend it to S3. Then resume later. That shouldn’t be hard to do, but nobody has done it.
April 4th, 2007 at 8:54 am
Hi,
Thanks for the writeup, but am facing some problem in ec2-bundle-image command. I am trying to create a image from within a customized ubuntu fiesty public AMI. Any idea if its supported at all?
June 11th, 2007 at 10:26 am
Excellent instructions, thanks, this was really helpful for me. I created a public Ubuntu Feisty 7.04 image (which includes Ruby on Rails but that’s easily removed). It includes the all the Amazon command-line tools so it can be easily re-bundled, and the Xen version of the standard libs (improves performance).
Details at:
http://pauldowman.com/2007/06/10/public-ubuntu-ruby-on-rails-ec2-image/
November 12th, 2007 at 11:41 am
I tried to use Ubuntu 7.10 inside VMWare. I was successful up to step 7, the apt-get upgrade command failed with the follow message. If you what I did wrong, please let me know.
Preparing to replace console-common 0.7.55 (using …/console-common_0.7.69_all.deb) …
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = “en_US.UTF-8″
are supported and installed on your system.
perl: warning: Falling back to the standard locale (”C”).
locale: Cannot set LC_ALL to default locale: No such file or directory
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = “en_US.UTF-8″
are supported and installed on your system.
perl: warning: Falling back to the standard locale (”C”).
Unpacking replacement console-common …
Preparing to replace vim 1:6.4-006+2ubuntu6 (using …/vim_1%3a7.1-056+2ubuntu2_i386.deb) …
Unpacking replacement vim …
Preparing to replace vim-common 1:6.4-006+2ubuntu6 (using …/vim-common_1%3a7.1-056+2ubuntu2_i386.deb) …
Unpacking replacement vim-common …
dpkg: error processing /var/cache/apt/archives/vim-common_1%3a7.1-056+2ubuntu2_i386.deb (–unpack):
trying to overwrite `/usr/share/vim/vimcurrent’, which is also in package vim-runtime
Preparing to replace libgpmg1 1.19.6-21ubuntu1 (using …/libgpmg1_1.19.6-25_i386.deb) …
Unpacking replacement libgpmg1 …
Preparing to replace vim-runtime 1:6.4-006+2ubuntu6 (using …/vim-runtime_1%3a7.1-056+2ubuntu2_all.deb) …
Unpacking replacement vim-runtime …
dpkg: error processing /var/cache/apt/archives/vim-runtime_1%3a7.1-056+2ubuntu2_all.deb (–unpack):
trying to overwrite `/usr/share/man/man1/vimtutor.1.gz’, which is also in package vim-common
Preparing to replace libattr1 2.4.25-1 (using …/libattr1_1%3a2.4.32-1.1ubuntu1_i386.deb) …
Unpacking replacement libattr1 …
Preparing to replace libacl1 2.2.34-1ubuntu1 (using …/libacl1_2.2.42-1ubuntu1_i386.deb) …
Unpacking replacement libacl1 …
Errors were encountered while processing:
/var/cache/apt/archives/vim-common_1%3a7.1-056+2ubuntu2_i386.deb
/var/cache/apt/archives/vim-runtime_1%3a7.1-056+2ubuntu2_all.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)
May 14th, 2008 at 7:02 pm
I’ve been maintaining a set of updated Ubuntu AMIs with support from the community. They are listed here with pointers to the public AMI docs on Amazon:
http://alestic.com
You can also join a growing community of folks using Ubuntu on EC2 here:
http://groups.google.com/group/ec2ubuntu
Enjoy!