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 file
sudo mount -o loop ubuntu.fs /mnt
Mounts the loopback file under /mnt
sudo 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 with apt-get install debootstrap.
sudo cp /etc/apt/sources.list /mnt/etc/apt/sources.list
Copy your apt source list to the target filesystem
sudo 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
passwd to update the image’s root password. Don’t lose it.
apt-get update and then apt-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/interfaces
auto 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 stop or /etc/init.d/mysql stop. Once you’ve confirmed this, use exit to back out of the mounted /mnt filesystem, and then sudo umount /mnt to 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.