Django Aware

« | Home | »

Amazon EC2 Basics For The Curious

By Paul Kenjora | June 3, 2008

For those of you wondering what it would be like to host and maintain a Django application (or any application) on Amazon’s Elastic Computing Cloud (EC2) here is a basic list of daily operations. These steps assume you’ve set up your Amazon account. This is the basic set of commands you can use to fully administer an EC2 server. A note to some of you, don’t soil your hacking trousers, the keys and file names below are fake.

  1. Show all instances that are currently running, useful if you want to connect to your server.

    ec2-describe-instances

  2. Show all images so you can pick one to run. With EC2 you can choose an image and run it on a variety of machine configurations. Great for scaling. Creating images is as simple as picking an existing one and editing it, EC2 comes with a few out of the box images. Default images are covered in the oficial Amazon EC2 getting started guide.

    ec2-describe-images

  3. Run an image. This is how you start a server. Same as hitting the power on button on a physical server.

    ec2-run-instances ami-4850b521 -k my-gsg-keypair

  4. Connect to an image, and do anything you want. You can use a key or log in and enter the password on the command line. Just like any other server.

    ssh -i my-gsg-keypair root@ec2-75-101-194-32.compute-1.amazonaws.com

  5. Attach a static IP to the new instance. This is great if youre running a website, your instance now has a static IP you can point any DNS at. Advantage here is you retain static API even if you upgrade your instance to a more powerful machine.

    ec2-associate-address -i i-b148b5gt 75.101.145.136

  6. If creating a new AMI or backing up existing instance you need to upload your key and certificate so the bundle utility knows how to encrypt your stored backup of an image.

    scp -i my-gsg-keypair pk-2WWJUDIZFYTYZN4JEBGSR5Z5E3SABDT7.pem cert-2WWJUDIZFYTYZN4JEBGSR5Z5E3SABDT7.pem root@ec2-75-101-194-32.compute-1.amazonaws.com:/mnt

  7. Once your instance (server) is running you can do things within it as root. It looks exactly like any other server I’ve seen. I even installed Django and some python libraries using yum on a fedora instance. So after you log into a running instance…

    • Fetch code from a repository and install it to home for example.

      cd /home

      svn co some project>/p>

      export PYTHONPATH=/home

    • Fetch database from your current server and install it on your instance. I picked a start image that already had Apache and MySQL.

      mysql

      CREATE DATABASE project;

      GRANT ALL ON project.* TO user@localhost IDENTIFIED BY ’some_password’;

      mysql -D project < project.mysql

    • After setting your instance up or maybe just to back it up, you need to save it to an image. EC2 instances loose ALL data if they are shut down or crash. The problem is not as bad as you think, a simple cron job to back up the instance to Amazon Simple Storage Service (S3) is all it takes. This is actually better than a typical server because you’re guaranteed to have a backup. So think of it as something you should be doing anyway. This requires step 6 above…

      rm -f /mnt/image

      ec2-bundle-vol -d /mnt -k /mnt/pk-2WWJUDIZFYTYZN4JEBGSR5Z5E3SABDT7.pem -c /mnt/cert-2WWJUDIZFYTYZN4JEBGSR5Z5E3SABDT7.pem -u 451222037441 -r i386

      ec2-upload-bundle -b project -m /mnt/image.manifest.xml -a 13BNUHIBGPAQ48JQ3NV1 -s grAbTUXLUjHnuff7Hpl/hNd3qTSH0WdklZZ2UB+j

  8. Every time you save an instance to an image you have to register it before launching that instance. So if your server crashes you will have to run the following command before launching it again from a backup.

    ec2-register project/image.manifest.xml

  9. When you first set up your account you should probably open up a few ports. Opening a port to allow ssh or ftp etc…

    ec2-authorize default -p 22

Well thats about it for EC2. I hope the whole thing does not seem as daunting anymore. My experience with the entire system including billing has been great. I set up my first EC2 instance in 35 minutes after hearing about it at a conference. Since then I’ve only used the commands to bundle and backup my instance. I strongly suggest Django developers who outgrow shared hosts move to this service, coupled with some of the tools for auto launching instances and scaling to handle load this is the best hosting option I’ve ever seen.

The intent of this article is to scratch the surface and boil EC2 down for anyone considering it, with only a handful of commands to completely control and back up a server I think the case is solid. For more advanced projects Amazon offers so many tools it would be best to visit the official Amazon Web Services page.

Topics: Tech News, Tutorial | Comments

  • In your Django EC2 instances, what do you use for your datastore? MySQL, Postgres, or Amazon's SimpleDB? Does your choice of datastore affect how you do your backups? (Are your data backups "live" -- while users are on your site?)

    Thanks!
  • That sounds great!

    Since one of the main benefit of EC2 is to be able to scale by adding more machine, did you try to use Django with more than one machine on EC2? Is it hard to separate the DB part from the front-end part?
blog comments powered by Disqus