Deploying Parse Server on AWS

Parse.com has announced shut down a few months ago, and they are planning complete the process by the end of the year 2016. They have done a pretty good job in helping developers migrating to other platforms, including Amazon Web Services. I have experimented with their automated tools, and One-Click deployment solution, and this article discusses about what I think the best way to setup a web-app from a Parse.com background.

Baby Steps

At the minimum, here is what we are going to do. These steps assumes that, you already have an AWS account setup.

Elastic Beanstalk

Elastic beanstalk is a wrapper offering from AWS. It basically includes a compute instance (t2.small is recommended by Parse.com), an S3 bucket and which runs in a load balancing, autoscaling group. EB is apparently one of the best way to deploy and manage web apps on AWS. To get started, you may need to install the Elastic Beanstalk Command Line Interface, which lets you create environments and deploy code.

1. Creating an IAM Role

To get started, you need to create a use and IAM role assigned to that user, so that you can create EB instances. Go to https://console.aws.amazon.com/ and select Identity and Access Management. Select Users from the left pane and create a new user. Once created you will see the access-key-id and access-secret. Note these two strings down. Now, click on the username, and navigate to Permissions. Click on Attach Policy and select AWSElasticBeanstalkFullAccess and click save. Now, this user has full access to EB service.

2. Installing EB CLI

Depending on your operating system, choose one of the following commands to install the elastic beanstalk CLI.

# Linux
$ pip install awsebcli

# Mac OSX
$ brew install awsebcli

3. Creating an Elastic Beanstalk Environment

Create a folder and name it appropriately. Clone the Parse-Server-Example repository into this folder.

$ git clone https://github.com/ParsePlatform/parse-server-example.git <your app name>

Run $ eb init to initialize the directory and login using the access-key-id and access-secret created from the earlier step. Run $ eb create to create an EB app and run $ eb deploy to upload the code and run your app on AWS.

Note : You may choose the appropriate availability region on AWS to have the lowest latency

Mongolab setup

Go to http://mongolab.com and sign-up for a free account. Click Create new database, select AWS, and choose a single-node sandbox for now. You may migrate to a better deployment for a production app. Give a name to your database and click Create. This will leave you with a new MongoDB deployment. Now, create a new DB user, and make sure NOT to select read-only. Everything is good from the Mongo standpoint. Your mongoDB URI will be something approximately like the following.

mongodb://<db_user>:<db_password>@ds011472.mlab.com:11472/<db_name>

Setting up Environment Variables

Now, that we have all the basic components, you may go ahead and configure the EB environment variables. Parse server uses environment variables to configure and run the web server. Go to Elastic Beanstalk on AWS console, select the newly created app (the app you created through the CLI should appear here). Click on Configuration on the left pane, and click on the small gear icon on Software Configuration and scroll down to the bottom of the screen, you may see Environment Properties. Add the following three properties and values for each of them.

  1. APP_ID : An alpha-num string that is sufficiently long
  2. MASTER_KEY : Another alpha-num string that is sufficiently long
  3. DATABASE_URI : The MongoLab URI you have just generated

Press Save, and we are ready to run our app on the parse instance now.

Saving the first object and retrieving

To create and save an object to the Parse server, use the same approach we used on Parse.com.

$ curl -X POST \
    -H "X-Parse-Application-Id: <APP_ID>" \
    -H "Content-Type: application/json" \
    -d '{"score":1337,"playerName":"John Doe","cheatMode":false}' \
    http://<BEANSTALK_URL>/parse/classes/GameScore

{"objectId":"cuD8K9vbVm","createdAt":"2016-05-11T12:29:36.176Z"}

To retrieve also, the same method. Note that for creating an object just the APP_ID is enough.

$ curl -X GET \
    -H "X-Parse-Application-Id: <APP_ID>" \
    -H "X-Parse-Master-Key: <MASTER_KEY>" \
    http://<BEANSTALK_URL>/parse/classes/GameScore

{"results":[{"objectId":"cuD8K9vbVm","score":1337,"playerName":"John Doe","cheatMode":false,"updatedAt":"2016-05-11T12:29:36.176Z","createdAt":"2016-05-11T12:29:36.176Z"}]}

You may get the beanstalk instance’s publick url from the beanstalk app page on the aws console. App key and master key should be the one used in the environment properties.

Further Improvements

1. Adding Cloud Code Endpoints

Your sample app already contains index.js which is nothing but the good old app.js from Parse.com terminology. You may add new endpoints and write cloud functions there, or split into multiple modules if required.

2. Using S3 buckets for file upload

By default, files goes to the mongoDB. You may change this functionality, and direct file upload into S3 buckets. This is particularly useful, because S3 is comparatively cheaper and you may use convenient Lambda functions with them, say for example for scaling images. Parse server comes with built in support for S3. Read More.

3. Parse Dashboard

One of the best thing Parse.com had is their nice looking app dashboard, where you can inspect database tables and review other things related to your app. You may install it as a component on your local machine and run it using the beanstalk server URL. Read More.

4. Using API Gateway Service

AWS also provides an API Gateway service. If you are planning to expose a REST api from your web app, this would be an ideal choice. API Gateway comes with caching and several other features to improve the performance of your API. Read More.

Thanks for Reading.