How to deploy an AdonisJS app and Redis to Digital Ocean
Tags: dohackathon, redis, node, tutorial
Published: 2021-01-09 21:24:09

## Introduction
The Digital Ocean App Platform simplifies deployment to four steps.
1. Choose repo
2. Set app name and region
3. Set environment variables
4. Choose plan

In this blog, we will learn how to deploy an AdonisJS app which requires Redis to Digital Ocean.

## Getting started
- If you don't have a repo already, fork and clone this one
```bash
$ gh repo fork https://github.com/vicradon/tic-tac-toe.git
``` 
You can do it manually if you don't have the Github CLI.

- After cloning, install the adonis CLI globally (use `sudo` for Linux)
```bash
$ npm i -g @adonis/cli
```
- Afterwards, create a .env file
```bash
$ cp .env.example .env
```
- Finally, generate an app key
```bash
$ adonis key:generate
```
This key will be copied to the .env file. Take note of it. We will make use of it later.

## Deploying a Redis Cluster
Since our app needs Redis, we have to deploy Redis separately. 
### Step 1: Navigating to the databases page
Navigate to the [new database page](https://cloud.digitalocean.com/databases/new) to create a Redis Cluster

![New database page](https://dev-to-uploads.s3.amazonaws.com/i/uqhbny2mvh89whcz5ynn.png)
 
### Step 2: Choose a Datacenter
Choose a datacenter closest to most of your users.

![Data center](https://dev-to-uploads.s3.amazonaws.com/i/nsh7j4nf1h8j7br79zl1.png)
 
### Step 3: Choose a Database Cluster Name
You can either leave it as the default or choose a new name. Click the `Create a Database Cluster` button to continue.
![DB Cluster](https://dev-to-uploads.s3.amazonaws.com/i/dm6syay2m2rdgy3fqmah.png)

While Digital Ocean provisions our Redis Cluster, we will deploy our Adonis App.

## Deploying the Adonis App
### Step 1: Choose Your Repo
![choosing a repo](https://dev-to-uploads.s3.amazonaws.com/i/foh6tzmq45t1ri327k0y.png)
 
### Step 2: Choose an App Name, Region and Branch
As we previously did, choose an app name and region closest to you. You can also select your branch from here.
![Selection_009](https://dev-to-uploads.s3.amazonaws.com/i/6fgfkjvuqvsxr344l8hp.png)

### Step 3: Configure Your App
Digital Ocean is able to smartly detect what our app runs on.
![Platform detect](https://dev-to-uploads.s3.amazonaws.com/i/lbpputtlnyces87cbcre.png)

We need to set a few environment variables. 
![environment variables](https://dev-to-uploads.s3.amazonaws.com/i/b8ep0xdqlko82oyrse6x.png)
 
To obtain the values of `REDIS_HOST`, `REDIS_PORT` and `REDIS_PASSWORD`, go to the database cluster you deployed earlier and skip the getting started page shown.

![skip getting started](https://dev-to-uploads.s3.amazonaws.com/i/2z0rsm69ii90n32g3brh.png)
 
In the connection details section, you should be able to see the host, port and password.
![host port and password](https://dev-to-uploads.s3.amazonaws.com/i/895g5374i71any2pw5y1.png)

Copy the values of each and set them as environment variables in your app's deployment page
![environment variables](https://dev-to-uploads.s3.amazonaws.com/i/b8ep0xdqlko82oyrse6x.png)
 
#### App Key and Redis Connection
Obtain the APP_KEY value from the project's environment file. Ensure you set `REDIS_CONNECTION` as `prod`. You should have 5 environment variables in total.

#### Setting the run command
![run command](https://dev-to-uploads.s3.amazonaws.com/i/5z9sksnwary16hyak28h.png)
 
Set the run command as `npm run prod`. This will run the app with a [disabled `.env` file](https://adonisjs.com/docs/4.1/configuration-and-env#_disabling_the_env_file). 

### Step 4: Setting the app's plan
Select the basic plan and click the `Launch Basic App` button.
![Selection_013](https://dev-to-uploads.s3.amazonaws.com/i/g2wk0kp384rt4wozxadz.png)
 
Your app should be deployed within a few minutes. Here's my own deployed app.
![deployed app](https://dev-to-uploads.s3.amazonaws.com/i/reo7l87aago8fp46b92w.png)

## A note about TLS 
Redis clusters on Digital Ocean have SSL enabled. This means you won't be able to connect to it using the `redis-cli` without configuring [stunnel](https://www.digitalocean.com/community/tutorials/how-to-connect-to-managed-redis-over-tls-with-stunnel-and-redis-cli). Alternatively, you can use [redli](https://github.com/IBM-Cloud/redli/releases) to connect to a Redis cluster over SSL. 

Adonis uses `ioredis` as its default `redis` client. `ioredis` requires a tls host in its config to connect to a cluster over SSL. If you check `config/redis.js` file in the app, you'll see the `tls` object in the prod variables.
```js
  prod: {
    host: Env.get("REDIS_HOST"),
    port: Env.get("REDIS_PORT"),
    password: Env.get("REDIS_PASSWORD"),
    db: 0,
    keyPrefix: "",
    tls: {
      host: Env.get("REDIS_HOST"),
    },
  },
``` 


## Conclusion
In this tutorial, you learned how to deploy an AdonisJS app and a Redis cluster to Digital Ocean. You experienced the ease of deployment Digital Ocean offers for app developers. You also learned more about Transport Layer Security on Redis clusters.

If you enjoyed this tutorial, you can subscribe to my newsletter on [my website](https://osi.codes) and also follow me on [Twitter](https://twitter.com/vicradon). Thanks for reading. Adios ✌🏾🧡.