How To: Deploy Reaction Commerce on AWS Elastic Beanstalk

This guide will cover deploying the official release build of reaction commerce. If you wish to add a custom theme or plugins, you will need to fork the GitHub repo and create an image on Docker Hub.

The configuration includes a load balancer with elastic beanstalk to support auto scaling. Additionally we will use an external mongo database from a provider. This setup has the potential to suit most size stores. If you want to run a small store, using Heroku may be better.

So this guide will cover:

If you are new to AWS

Firstly you need to consider price. Realistically the smallest server instance you would want to run would be a single t2.small (2GB Ram) and this isn’t included in the free tier. Pricing varies by location: EC2 Pricing

In addition this guide will run the mongo database separately so that will add in extra costs.

AWS Regions, source: https://aws.amazon.com/about-aws/global-infrastructure/

If you want to continue, sign up for AWS; ready for the next part.

A Domain Name…

You will need two things; The domain name, and an AWS account with a Hosted Zone created in Route 53. Once you create a hosted zone it will provide you with a list of name servers that you can enter into your domain name provider’s console.

Setting up a hosted zone

To setup a hosted zone you need your domain name ready.

  • In the AWS console click on services and type Route 53 in the search bar.
  • In the left hand menu select Hosted Zones
  • Press the Create Hosted Zone on the top left
  • In the newly opened right hand pane, enter your domain name (example.com) and press Create.
AWS Route 53 Nameservers for your domain
  • Now you have a list of name servers, add these to your DNS management with your domain name provider. Not sure how ?: Change my name servers

That bit is done for now, but we will be back; to point the domain to our servers.

Setting up a Mongo Database

Next we need to create a Mongo database. You can use a one click setup to deploy your own Mongo database on AWS EC2. (AWS Console>EC2>Launch Instance>AWS Marketplace>Search ‘mongo’). Or you can use any of these providers: mongo Atlas mlab compose

We will look at:

  • A single database created in mlab

General setup

The result of setting up a mongo database is: A database URL (MONGO_URL) and a database user who can read and write to the database.

Optionally we can also create an Oplog (Operations log) url (MONGO_OPLOG_URL), this is relevant to databases that use replica sets.

mlab setup

mlab.com includes a free tier called sandbox. The sandbox tier doesn’t feature any replication so we will just be connecting to one database. Sign up for an account and log in.

  • Under the Mongo DB Deployments heading click the Create New button.
  • Choose Cloud provider Amazon Web Services
  • Select your region (closest to your customers, and remember it we will run our application in the same region).
  • Choose plan as Single Node (not replica set cluster)
mlab.com free database creation
  • Enter your database name i.e. reactiondb. and click Create new Mongo Deployment
  • Once your database is finished creating click on its name in the list and open up the details page to show your MONGO_URL
Database URI
  • To use the URL we need to create a user and password to access the database.
  • Select the Users tab and click the Add database user button.
  • Choose a username and password, and press Create
  • Now we can create our MONGO_URL copy the URL from your mlab database page:

mongodb://<dbuser>:<dbpassword>@ds181.mlab.com:3951/reactiondb

  • And add in your username and password e.g:

mongodb://admin:mypassword@ds181.mlab.com:3951/reactiondb

Creating a Elastic Beanstalk Docker Application

Our application is simply a small zip file with a few files and folders in it. There are three parts to our application, they are:

  • The Dockerfile
  • The Dockerrun.aws.json file
  • The .ebextensions folder with .config files

The Dockerfile

The Dockerfile defines some aspects of your Reaction Commerce store. It uses the reactioncommerce/reaction image, then you set your Environment variables including your MONGO_URL and your default admin user. Finally, meteor runs on port 3000, so we need to expose that port on the docker container to allow nginx on the EC2 instance to connect.

The Dockerrun.aws.json File

This file configures elastic beanstalk, the main purpose for us is to set the container port. More info is here.

The .ebextensions folder

The .ebextensions folder contains scripts that we can use to make alterations to the elastic beanstalk instance. We have two problems to fix. Reaction Commerce requires a larger proxy buffer on nginx than is currently set. Additionally, we need to setup a redirect to make sure that the user is always redirected to https://www.yourdomain.com . This is because the certificate we will create with AWS Certificate Manager will only create a certificate for www and not root.

Create a directory called .ebextensions ; within that directory create a file with the code below. Elasticbeanstalk runs the files you put in here in numerical order. so we will call this 01-nginx-setup.config

/*note: this needs some work*/

The nginx installation on EB will look for files in the /etc/nginx/conf.d and add them to its configuration. So this script creates a hook that runs and creates the file we need.

All those files need to be zipped together, without a root folder. However if you are using OS X, you may find you get errors on elastic beanstalk. This is down to some hidden files that OS X includes in zip folders. Using the mac terminal open your directory and run:

zip -r -X "myzip.zip" . -x "*.DS_Store"

There is a bash script in the gitHub repo that will run that command just by typing:

./maczip

This application can be found on GitHub here.

https://github.com/JungleCatSW/reactioncommerce-aws-elasticbeanstalk

Creating a Certificate for SSL

Open AWS console and find Certificate Manager and choose Request a Certificate. Enter your desired sub domains if you aren’t sure which ones you will use, enter *.yourdomain.com. AWS will send an email to the domain owner so make sure your domain contacts are correct before starting.

Open up your inbox and you should see an email from AWS. Approve the request.

Starting the Elastic Beanstalk Environment

Open your AWS console and find the Elastic Beanstalk Service. Make sure you have your desired region selected in the app bar. In the top right corner select Create New Application and give it a name.

To create a new environment click Create one now and choose Web Server Environment.

Choose Docker as the platform and select upload your code to upload the zip file you created. Then press Configure More Options.

Configure More Options

  • Environment settings > Name, domain
  • Instances > Instance type (at least t2.small 2GB)
  • Capacity > Type = Load Balanced & Min/Max (can just be 1/1 if you don’t want to auto scale)
  • Load Balancer (Secure ELB Listener)> Port = 443, Protocol = HTTPS, SSL Certificate = *.yoursite.com
  • (Optional but useful ) Security > Setting an SSH key pair -> link here

Now wait…

Once the application is up and running with a healthy status you can do the next part.

Point your domain to your Load Balancer

Up at the top of your Application/Environment Dashboard is a URL. This is the domain you created. We will now link your domain to the EB URL.

EB Environment URL

Open Route 53 in AWS and go to Hosted Zones. Click your domain name to open the detail page.

Choose Create Record Set.

  • Enter www as the name.
  • Select Alias = Yes
  • In Alias target enter your EB URL.
  • Do this again but using a blank name for your root domain.

Now your domain should be pointing to your reaction commerce site.

This is all early stage, so if you have any ideas to make it better or issues get in touch.