Resources



Articles and Tutorials

Hosting Facebook Applications on Amazon EC2

Click for a printer friendly version of this document Printer Friendly Save to del.icio.us
Average Review:

Popular social networking applications are often victims of their own success, creating a surge of traffic to the application hosts. In this tutorial, Facebook application developer Nikki Everett shows how to take advantage of Amazon EC2's computing power for Facebook applications.

AWS Products Used: Amazon EC2
Language(s): PHP, Ruby
Date Published: 2007-11-09

By Nikki Everett

In this tutorial, I will go through the basic steps to teach you how to host a Facebook application using the Amazon EC2 and Amazon S3 services. I use my application, Quote Boat, as an example.

Getting Started

Read the Getting Started Guide. I'll follow the general steps in that guide, adding in what I think is missing or relevant, and of course, telling you how to do it Facebook-style.

Setting up the Tools

  • Go to aws.amazon.com and sign up for an Amazon EC2 account. You're going to get an x.509 certificate and a private key file. For the love of God, don't give these things out to strangers in the street. Save them in a special place, like ~./ec2.
  • Next, download the tools.The tools require java to run, so make sure you have Java 5 (Mac OS X users, this should already be on your computer). You'll also need an SSH client.
  • Note: There is a difference between the AMI command line tools and the API bundling tools. One is a set of command line utilities written in java that contain shell scripts for accessing and describing AMIs and instances. The other is a set of Ruby scripts for bundling up your instance and producing a shiny new AMI. Installing the bundling tools on your Macs, while not impossible, is really annoying because the native file format--.rpm--is not supported by that machine. If you look carefully, you will notice that you only need the command line utilities on YOUR machine and the Ruby tools live on the EC2 machines. You are not going to be doing any bundling on your machine, so don't fret over how to keep Real Player from trying to open your scripts. Follow the instructions on the AWS command line tools tutorial.

  • Set up the tools. This requires putting the tools in your path (This is also in the Getting Started Guide). Make sure you edit your .bashrc file so that your environment variables persist...some of you might know this, but probably some of you do not. Java is a little funky on the Mac. You probably need to set the variable to /System/Library/Frameworks/JavaVM.framework/Home/. It's not obvious on the guide.

Running an Instance

You're almost ready to rock and roll. Refer to the guide for full instructions. The basic outline is as follows ( Amazon's guide has these same directions in a slightly different order; this is just the order that seemed clearest to me):

  • Generate a keypair.

    Open up a terminal window and type ec2-add-keypair. Open a text editor on the command line and paste the key from "start rsa private key" to "end rsa private key", inclusive. Save it as rsa_id-gsg-keypair. Then you need to change the file permissions on your key, otherwise Amazon will squawk at you when you try to run instances. Staying on the command line, type chmod 600 id_rsa-gsg-keypair. This gives you and only you the power to read and write to your key file.
  • Authorize traffic on your instances to ports 80 and 22 (HTTP and SSH, respectively) with the commands ec2-authorize default -p 22 and ec2-authorize default -p 80.

    The basic idea behind this part is that every instance you run is a member of some authorization group that has certain, well, authorizations. Amazon starts you off with an authorization group with no authorizations (called "default"), and you have to manually add them in.

  • Bring up a list of available AMIs.

    Type ec2-describe-images -o self -o amazon on the command line. This will bring up your images and Amazon's images.

  • Run an instance of an AMI.

    Once you find an image you want to run, type ec2-run-instances -k gsg-keypair [AMI] (I use ami-25b6534c, which is a basic LAMP stack using Fedora Core 4). This will return to you metadata about the image, such as the reservation number, your amazon id, and the instance number. You need the instance number in order to access the instance. The instance number is identified by i-[some number]. Next type in the command ec2-describe-instances [instance id]. this will give you the instance's public hostname. To log in to the instance, type ssh -i id_rsa-gsg-keypair root@[public hostname].

Modifying Your Instance, Facebook-Style

This requires only a few tweaks. Here are the things you need to consider:

  • The Facebook client library.

    Visit developers.facebook.com and download your client library of choice. I wrote my application in php, so I got the php libraries.

  • Your app's conf file might need the following:
    • Amazon identifiers: I personally didn't need to include these values in my conf file, but if you have an app that references Amazon S3, you might. This AMI includes an example of an app that needs Amazon identifiers; run an instance of it and check out the conf file in the helloawsworld directory (/var/www/html/helloawsworld/).
    • Database configuration:You must edit your conf file to include database configuration parameters, which will be pretty much the same as any other database conf. If you've never done that sort of thing before, I wrote up a little section on MySQL below.
  • PHP info file.

    If you write your app in PHP, you need to make sure that the LAMP stack you are running is using a current version of PHP 5. For those unfamiliar, you can check this by making a phpinfo file. Then type curl http://169.254.169.254/latest/meta-data/public-ipv4 on the command line, get the IP address of your current instance, open a browser window, and go to [that IP addess]/phpinfo.php. The output will show you what version of PHP you are using.
  • MySQL squirreliness.

    The LAMP stack that I use comes with MySQL and can act as a MySQL server. The database for Quote Boat lives on the same AMI that contains my app files and whose instances serve my app. When you edit your conf file to include database configuration parameters, do not set the user as root! Instead, set up a 'facebook' user for your datbase with special permissions to read from (and write to, if necessary) your application's specific tables. When it comes time to fill in your hostname, fill in '[user]@localhost' if your mysql server is on the same instance as your app.

    Note: please please please sanitize any database inputs your app will be receiving, especially if you're letting your users type in whatever they want.

  • Dynamic DNS configuration.

    Here's the slightly tricky part that's not in the Guide: As long as an instance is running, its IP address is static and you can put technically use it as the callback url when you're filling out the settings for your Facebook application (developers.facebook.com). However, it's sort of pointless to pay for only one instance when the real power of EC2 is in launching multiple instances. The problem is that every new instance of your image has a different IP address, but Facebook only has one line for a callback URL. I got around this by using EasyDNS with Dynamic DNS and the Ruby script and conf file at spatten's blog (scroll down to the post on updating dynamic DNS on amazon EC2). Here are the steps I took:
    • Sign up for a domain through a DNS provider and enable Dynamic DNS on your domain.
    • Install ez-ipupdate on your instance (if you use something else, it has directions for those things in particular, but this is what i used) by using yum install ez-ipupdate on the command line of your instance.
    • Save the ruby script and the conf files into appropriate files.
    • Write a shell script to run the ruby script upon starting the instance and save it in /etc/init.d.
    • Test out the Ruby script by running ./update_dynamic_dns.rb and then pinging your domain in another terminal window from your own computer.

Bundling the Instance

At this point, you are ready to bundle up your instance as an AMI. scp your app's files and the client library (php or java) you want to use to the instance, follow Amazon's bundling instructions (notice a trend here?), fill out your app settings on Facebook, and take 'er out for a spin.

And now...You're done! Launch your app, get famous, run a hundred and one instances, pay for what you use. Keen!

Resources

Nikki Everett is a developer and writer for Renkoo, the creators of Booze-Mail and Haikoo Zoo. She is also one of those rare employed philosophy majors.



Related Documents
Type: AMIs HelloWorld Facebook Application AMI
Type: AMIs Quote Boat -- Hosting Facebook Applications on Amazon EC2

Discussion

The 5 most recent discussion messages. View full discussion.

E. Tisdale
RealName(TM)
Posts: 1
Registered: 5/8/09
Hosting Facebook Applications on Amazon EC2
Posted: May 8, 2009 5:45 PM PDT
 
  Click to reply to this thread Reply

This page at http://developer.amazonwebservices.com/connect/entry.jspa?entryID=1044 seems to be geared towards Linux users. Where is the Windows version?

PLease email me if you can. edwardtisdale_2004@yahoo.com



Reviews
Create Review Write a Review

Thank you!!, Nov 17, 2007 5:44 PM
Reviewer: joel1198
This is great!! Thank you for taking the time to help your Facebook developers colleagues.

Thanks for the link :), Nov 27, 2007 8:50 AM
Reviewer: Scott A Patten
The permanent link for the blog entry on updating dynamic DNS on EC2 is http://spattendesign.com/2007/10/10/updating-dynamic-dns-on-amazon-ec2. Scott Patten http://spattendesign.com

Persistant Database Data?, Jan 25, 2008 1:28 PM
Reviewer: "boxcarwilli"
First, I appluad you for this writeup and AMI's as I'm currently evaluating this. However, you dont elaborate at all about how when an EC2 instance goes down, so does all of your data and any changes you made to that instance. How are you dealing with that? Also, one could use WeoCeo instead of the DynDNS option you touched on. WeoCeo allows you to automatically scale up and down your instances based on CPU usage, and, takes care of the DNS issues as well.

Tips on hosting a public Web site on Amazon EC2 and S3, Mar 13, 2008 4:18 PM
Reviewer: Rajiv Pant
Given the difficult to predict viral nature of traffic and usage growth on social networking applications like Facebook applications, AWS is a great platform for hosting social networking applications. I found this document useful in porting my personal Web site http://www.rajiv.com/ to AWS. Here are some tips on hosting a public Web site on Amazon EC2 and S3 from my experience: http://www.rajiv.com/blog/2008/02/04/amazon-ec2/

If you need PHP 5.2.x or just JSON, Apr 17, 2008 7:05 PM
Reviewer: appmail
This is definitely a good start but I spent a fair time trying to gey my PHP in order. The first thing you need to do is get everything updated. yum update To get JSON support in your PHP yump install php-json If PHP 5.0.4 is not cutting it for you and you want PHP 5.2 cd /etc/yum.repos.d wget http://remi.collet.free.fr/rpms/remi-fedora.repo yum --enablerepo=remi update mysql yum --enablerepo=remi update php apachectl restart Enjoy!

very helpful - but here are more details, Feb 19, 2009 7:12 PM
Reviewer: Luke Barrington
Thanks for this information! I found it very useful in getting my Facebook game, Herd It, hosted on AWS (www.herdit.org). However, even with this page, there are still a lot of details that are left for the reader to figure out, particularly regarding the Elastic DNS. I've tried to give a more complete description on my blog: http://doubtfulsound.blogspot.com/2009/02/herd-it-on-amazon-web-services.html Thanks again, Luke
Welcome, Guest Help
Login Login