What?! You’re building an awesome mobile app, like this one -
iOS 7 Dynamic Type, and iOS 7 UserXListView,
and get to the point where you need a backend.
Oh no! Your complexity just increased exponentially.
Luckily the latest cloud computing XaaS (Everything as a service) is called a
BaaS (Backend as a service).
BaaS offerings include Parse (which was acquired by Facebook earlier this year),
and the open source BaasBox.
The idea is that a BaaS abstracts the complexity of building a backend.
But is building a backend really all that difficult using cloud computing?
Let’s find out and build a cloud backend using node.js on AWS.
Let’s get started.
Node.js
First download and install Node.js, which is a platform for building applications in JavaScript.
It’s powered by the Google Chrome V8 JavaScript engine.
Isn’t JavaScript slow? No!
”V8 compiles JavaScript source code directly into machine code when it is first executed.
There are no intermediate byte codes, no interpreter.”, https://developers.google.com/v8/design
Node.js is fast.
We’re going to use Node.js and node packages to expose web services for our awesome mobile app.
Why? There are lots of good articles that explain what Node.js is and what it’s good for.
Here’s my simple take:
The web has changed from consumption (viewing web pages) to interaction (apps, social media, chat). We need better client/server technology.
Node.js is JavaScript on the server, which lowers the learning curve for developers.
Node.js has a strong open source community. NPM (Node Package Manager) is a repository
of reusable components. There are currently over 50 thousand, with over 3 million downloads per day.
The goal is to build a cloud backend - expose a RESTful web API that supports OAuth 2.0, with a database.
This blog post should serve as the foundation to start you on that journey.
We will be using OS X Mavericks (Version 10.9), Terminal, and TextWrangler
which is available free from the App Store.
Create and change to a new folder called cloudbackend from your Documents folder.
Create a file called package.json and put the following JSON data object in it.
Use npm (Node Package Manager) to install Express, which is the
dependency you identified in the above package.json file. Express is a web
application framework. Enter ‘npm install’.
Create a file called app.js with the following content.
All we are doing here is exposing one HTTP GET method request, available at ‘/test’,
which returns a JSON data object. A simple test.
Congratulations! You’ve just successfully built a RESTful web API with a single resource
called test which is responding to a HTTP GET method request, and is returning a
JSON data object.
Let’s deploy our backend to the cloud.
AWS
With AWS (Amazon Web Services) we can easily deploy our backend to the cloud while
still keeping flexibility and control - things that a BaaS takes away by abstracting the complexity.
First sign up for the very generous AWS Free Usage Tier.
This simple tutorial should not result in charges to your account.
We are going to deploy cloudbackend as an Application into EB (Elastic Beanstalk)
which is an AWS Application Container. The Architectural Overview shows and
explains what Elastic Beanstalk is.
An Auto Scaling Group is created with at least 1 EC2 Instance (basically a VM - Virtual Machine).
Traffic to your application URL first goes through the ELB (Elastic Load Balancer), and then to an EC2 Instance.
Once setup all AWS resources are automatically provisioned and configured.
You have a fully auto load balanced, auto scaled cloud application environment.
Initialise your cloudbackend Git repository. Enter ‘git init’, followed by
the other commands in the following console code block.
Next download and extract the AWS Elastic Beanstalk Command Line Tool to
your Documents folder (the cloudbackend folder should be in your Documents folder).
Next add the appropriate EB Python folder to your PATH.
If you have OS X Mavericks (Version 10.9) then you already have Python 2.7.5, else you
will need to download and install it.
Next we’re going to create and configure the cloudbackend AWS Elastic Beanstalk
Application Container. You basically enter ‘eb init’ and then follow the prompts.
You will need to enter your AWS Access Key ID and Secret Access Key, which
can be found at the URL given after you enter ‘eb init’.
Select an AWS service region. I entered ‘7’ for ‘Asia Pacific (Sydney)‘.
Accept the default application and environment names by pressing return.
Enter ‘8’, for ‘64bit Amazon Linux 2013.09 running Node.js’.
Enter ‘1’, for ‘LoadBalanced’.
Enter ‘n’, to skip creating an ‘RDS DB Instance’.
Enter ‘1’, for ‘Create a default instance profile’.
Now we just start it. Be patient as this will take a few minutes.
Enter ‘eb start’.
Enter ‘y’, to ‘deploy the latest Git commit to your environment’.
Replace the domain part of the above URL with the domain you were given at the end of the ‘eb start’ process …
Application is available at “cloudbackend-env-*.elasticbeanstalk.com”.
Congratulations! Your cloud backend is now working.
Now to work on that awesome mobile app!
Lastly if you want to stop and delete the environment, use the following EB command.
Enter ‘eb stop’, and then ‘y’ to terminate the environment.
I may cover topics like adding a custom domain name, SSL, OAuth 2.0 support, a
database, or web pages to your cloud backend in future posts if there is interest.