How to deploy a Hapi application to AWS

Hapi is a framework designed for building applications and service. It allows developers to focus on writing applications instead of spending building infrastructure.

In this tutorial you'll take an existing Hapi application, add an nginx reverse proxy, and configure the app for deployment on AWS.

Prerequsites

It's assumed that you have a basic knowledge of Node.js and Hapi.js, along with an existing application to deploy.

This tutorial uses the Nanobox Hapi.js quickstart, however, the process will be the same for any existing Hapi application.

We'll use Nanobox for deployment because it makes the process much quicker and easier. You can create an account (it's free!) and download Nanobox to get started.

Configure Hapi for Production

We'll cover three steps to preparing your application for production:

  • Adding an environment config file
  • Connect your database (if you have one)
  • Setup the nginx reverse proxy

Environment Config - The boxfile.yml

Nanobox uses a simple config file, the boxfile.yml, to provision both your applications development and production environments.

Create a boxfile.yml at the root of your project and specify everything your application needs to run:

run.config:  
  engine: nodejs

  # add nginx package
  extra_packages:
    - nginx

# add a postgres database
data.db:  
  image: nanobox/postgresql

# add a web component and give it a "start" command
web.main:  
  start:
    nginx: nginx -c /app/config/nginx.conf
    node: npm start

# add a worker component and give it a "start" command
# worker.main:
#   start: node worker.js

The boxfile.yml is used to provision both a local development environment and your production environment.

Note: This example uses a postgres database. Make sure that you specify the correct data component for your application.

Connecting the Database

To maintain the portability of your environment and the security of your application, it's recommended that you use environment variables (evars) when connecting to services such as a database.

Make sure that you have any dependencies your database requires installed. Since this application is using postgres it needs the pg-promise module installed:

nanobox run yarn add pg-promise

You can then add/update your database.js with the following:

var pgp = require("pg-promise")(/*options*/);  
var db = pgp("postgres://${process.env.DATA_DB_USER}:${process.env.DATA_DB_PASS}@${process.env.DATA_DB_HOST}/gonano");

Configure Nginx as a Reverse Proxy

To add an nginx reverse proxy to your application, create a /config directory at the root of your project and add the following nginx.conf:

config/nginx.conf
worker_processes 1;  
daemon off;

events {  
    worker_connections 1024;
}

http {  
    include /data/etc/nginx/mime.types;
    sendfile on;

    gzip              on;
    gzip_http_version 1.0;
    gzip_proxied      any;
    gzip_min_length   500;
    gzip_disable      "MSIE [1-6]\.";
    gzip_types        text/plain text/xml text/css
                      text/comma-separated-values
                      text/javascript
                      application/x-javascript
                      application/atom+xml;

    # Proxy upstream to the node process
    upstream hapi {
        server 127.0.0.1:3000;
    }

    # Configuration for Nginx
    server {

        # Listen on port 8080
        listen 8080;

        root /app/public;

        try_files $uri/index.html $uri @hapi;

        # Proxy connections to hapi
        location @hapi {
            proxy_pass         http://hapi;
            proxy_redirect     off;
            proxy_set_header   Host $host;
        }
    }
}

Stage a Production Deploy

Nanobox allows you to stage a production deploy on your local machine with the dry-run command.

Add a DNS alias for easy access from the browser and then do a test deploy:

# Add a DNS alias for easy access
nanobox dns add dry-run hapi.stage

# Deploy to a local staging environment
nanobox deploy dry-run

This will deploy your app locally, simulating a production deploy. Once complete, you can visit your app at hapi.stage/.

Deploy Hapi to AWS

After staging your app to verify it works, it’s ready to deploy to production. If you haven't already, create a free account with Nanobox.

Launch an App on AWS

Before you're able to deploy you'll need to add AWS as a hosting provider.

add-aws-as-provider

Once you've linked your AWS account you can launch a new app.

Deploy to AWS

Once your app is created, add it as a remote (by name) and deploy to it.

nanobox remote add app-name  
nanobox deploy  

When you deploy, Nanobox builds your codebase locally and sends it to the server, then it create containers for each of the app’s components (web and database), and starts your application.

That’s it! Your app is live!

Update NODE_ENV

One last thing you'll want to make sure you do is set your app to run in production mode.

From your dashboard go to your app -> config -> environment variables. There add a new evar with they key NODE_ENV and the value of production.

For this change to take place you'll need to redeploy your application (there is handy link right there to do so).

redploy-after-adding-evar

Once your app is redeployed you'll be running in production.

What next?

Setting up an Hapi development environment (coming soon)
Optimizing Hapi in production (coming soon)
Managing Hapi in production (coming soon)

Need help?

Let us know if you have any questions, or run into any issues. We love helping you get your apps running so much that we do it for free!

You can reach us at help@nanobox.io or join our Slack team and reach out.

Posted in Node.js, Hapi.js, Deployment, AWS