Hosting Facebook Applications on Amazon EC2

Articles & Tutorials>PHP>Hosting Facebook Applications on Amazon EC2
Learn how to launch your Facebook application on Amazon Web Services cloud infrastructure within minutes. A Facebook application—whether it is online casual game or social application—is a hosted web application that utilizes the Facebook API. You can host your Facebook applications on AWS to access a reliable, scalable, and cost-effective compute infrastructure.

Details

Submitted By: Jinesh@AWS
AWS Products Used: Amazon EC2
Language(s): PHP, Ruby
Created On: November 9, 2007 6:28 PM GMT
Last Updated: September 22, 2011 12:10 AM GMT

By Jinesh Varia, AWS & the 8KMiles team

Host Your Facebook Applications on AWS

In this article, you will learn how to launch your Facebook application on Amazon Web Services cloud infrastructure within minutes. A Facebook application—whether it is online casual game or social application—is a hosted web application that utilizes the Facebook API. You can host your Facebook applications on AWS to access a reliable, scalable, and cost-effective compute infrastructure.

The flexibility of AWS allows you to choose the programming models, languages, and operating systems that you are already using or that are best suited for your project. With AWS, you can bring your existing skills and knowledge to the platform; you don't have to learn lots of new skills.

When you host your application or game in the cloud, you can start small and then quickly scale up your infrastructure when your application is popular.

For the purpose of this tutorial, we will deploy a sample application built in PHP and host it in the AWS cloud.

To launch a sample Facebook application in the AWS cloud, all you have to do is following:

  • 1. Sign up for AWS account (there is no charge for opening an AWS account)
  • 2. Create a new Facebook app on Facebook
  • 3. Launch the Facebook sample stack with one click
  • 4. Configure the Site URL (or Canvas URL) of your app
  • 5. You are done!

Launch Facebook Sample App in the AWS Cloud in Three Simple Steps

First, you will have to sign up for an AWS account, if you don't already have one.

  • 1. Create an app on Facebook

    You can create an app on Facebook by going to http://developers.facebook.com/apps and clicking on the "Create App" button. This will create Facebook app credentials (Facebook App ID and Secret Key) that you will need in the next step.



    Add Basic Info like App Display Name, App Namespace (can only contain lowercase letters, dashes and underscores), and email address and app domain as "amazonaws.com" (More about this later).


  • 2. Launch the Facebook sample Stack with one click

    Once, you have created an app on Facebook, you need to host your application. We are going to host the application in AWS cloud. To do that, click on "Launch Stack" button.



    This will take you to the AWS Management Console's AWS CloudFormation stack wizard and load the sample template. AWS CloudFormation is a service that will gives you an easy way to create a collection of related AWS resources and provision them in an orderly and predictable fashion using a simple template (JSON).



    This sample AWS CloudFormation template will launch all the necessary cloud resources you need to launch the sample app on AWS. Since this template will automatically configure the sample PHP (File Sharing) application, you will need to input a few parameters before you can launch the stack:
    • 1. Facebook App ID, which was generated by Facebook when you created an app in Step 1
    • 2. Facebook Secret Key, which was generated by Facebook when you created an app in Step 1
    • 3. Facebook App Namespace, which is the same namespace that you used in Step 1
    • 4. AWS Access ID, which can be found in AWS Security Credentials page under "Your Account"
    • 5. AWS Secret Access Key, which can be found in AWS Security Credentials page under "Your Account"



    The sample template will not only launch and provision all the cloud resources (like an elastic load balancer, an EC2 Micro instance, Auto Scaling Group, Amazon CloudWatch Alarms, uses Amazon Machine Image, configures Security Groups etc.) but also install the latest Facebook PHP SDK (from GitHub) and the AWS SDK for PHP (from PEAR) at boot time as well as download and configure the sample application. This will take a few minutes to launch the stack. The beauty about this cloud architecture is that it will automatically scale and add more Amazon EC2 Instances when your traffic is high and also maintain a minimum number of EC2 fleet size (Min size set to 1) and launch new instances in case of the instance failed.



    Once the stack is launched (Status shows "Create_Complete") , you now have your application hosted in the AWS cloud. The architecture diagram above shows what will be launched behind the scenes by AWS CloudFormation Sample template that we used.

    Note: You can use the Facebook Sample template as-is or modify it or use it as a starting point for creating your own templates. You can find more AWS CloudFormation templates here.

    After the stack is launched, it will output the Site URL of your application which you will need for Step 3.



  • 3. Configure your Facebook application

    After you have launched your stack, you need to let Facebook know where the actual app is hosted. Go back to your application settings at http://developers.facebook.com/apps and under select how your app integrates with Facebook section, paste the Site URL under Website (if you want to have your own website or game) or Canvas URL under App on Facebook (if you want to integrate your app with Facebook's user experience).



    That's it. You are done! Within just a few minutes, you have now launched your Facebook web application in AWS cloud. To go to your app, simply point your browser to the Site URL or http://apps.facebook.com/yourFacebookNamespace/.
    When you go access the app for the first time, the app will ask for authorization and permission to access your profile:



    This sample application is simple file sharing application. With this application, you can upload any file and share it with your friends. The application is built in PHP, uses the AWS SDK for PHP and leverages Amazon S3 to store your files. It also posts a message on your wall when you upload a file and notifies your friends and leverages the Facebook's PHP SDK to do so.



    This sample application is simple file sharing application. With this application, you can upload any file and share it with your friends. The application is built in PHP, uses the AWS SDK for PHP and leverages Amazon S3 to store your files. It also posts a message on your wall when you upload a file and notifies your friends and leverages the Facebook's PHP SDK to do so.


Cost of Running the Sample Application on AWS

The Sample app (and the sample template) takes advantage of the AWS Free Usage Tier. New AWS customers will be able to run a free Amazon EC2 Micro Instance for a year, while also leveraging a free usage tier for Amazon S3, Amazon Elastic Block Store, Amazon Elastic Load Balancing, and AWS data transfer. Beyond the AWS free usage tier, you only pay for the resources you use; there are no long-term contracts or up-front commitments. You can use the AWS Simple Monthly Calculator to estimate the cost of your application.

How to Modify the Sample Application

With Amazon EC2, you get complete control of your environment. You can login access as "ec2-user" using Putty/Terminal and the SSH keypair that you used when you launched the stack. Please refer to Amazon EC2 Getting Started Guide for more information. You can modify the sample application (which can be found at /var/www/html/) or replace the sample application with your own existing Facebook app.

Credits

We would like to thank Navin Kumaran from 8KMiles. 8KMiles is a leading cloud consulting firm and an Amazon Web Services Solution Provider that offers cloud strategy, cloud engineering and cloud migration services to help businesses leverage the power of cloud computing.

Resources

Comments

very helpful - but here are more details
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
Luke Barrington on February 20, 2009 3:12 AM GMT
If you need PHP 5.2.x or just JSON
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!
appmail on April 18, 2008 2:05 AM GMT
Tips on hosting a public Web site on Amazon EC2 and S3
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/
Rajiv Pant on March 13, 2008 11:18 PM GMT
Persistant Database Data?
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.
"boxcarwilli" on January 25, 2008 9:28 PM GMT
Thanks for the link :)
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
Scott A Patten on November 27, 2007 4:50 PM GMT
Thank you!!
This is great!! Thank you for taking the time to help your Facebook developers colleagues.
joel1198 on November 18, 2007 1:44 AM GMT
We are temporarily not accepting new comments.
©2014, Amazon Web Services, Inc. or its affiliates. All rights reserved.