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.
Submitted By: Jinesh@AWS
AWS Products Used: Amazon EC2
Language(s): PHP, Ruby
Created On: November 09, 2007
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 https://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 https://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 https://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.