Edit this page

Deployment

When running Cube.js Backend in production make sure NODE_ENV is set to production. Such platforms, such as Heroku, do it by default.

Also, Cube.js requires Redis, in-memory data structure store, to run in production. It uses it for query caching and queue. Set REDIS_URL environment variable to provide Cube.js with Redis connection. Make sure, your Redis allows at least 15 concurrent connections.

Below you can find guides for popular deployment environments:

Cube.js could be deployed in serverless mode with Serverless Framework. The following guide shows how to setup deployment to AWS Lambda.

Create Cube.js Serverless app:

$ npm install -g serverless
$ cubejs create cubejs-serverless -d athena -t serverless

Create AWS ElasticCache Redis instance within the same region where lambda hosts.

Add Redis security group and subnet to serverless.yml vpc settings:

provider:
  vpc:
    securityGroupIds:
     - sg-12345678901234567 # Your DB and Redis security groups here
    subnetIds:
     - subnet-12345678901234567 # Your DB and Redis subnets here

When you assign vpc to a Lambda functions internet access will be disabled by default. Lambda functions require internet access to send SNS messages for query processing. Please follow this guide to set up internet access for your Lambda functions.

Please add following permissions to serverless.yml if you need Athena within your Lambda functions:

provider:
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "sns:*"
# Athena permissions
        - "athena:*"
        - "s3:*"
        - "glue:*"
      Resource:
        - "*"

$ serverless deploy -v

$ serverless logs -t -f cubejs
$ serverless logs -t -f cubejsProcess

$ cubejs create cubejs-heroku-demo -d postgres
$ cd cubejs-heroku-demo

$ git init

$ heroku create cubejs-heroku-demo

You can use any Redis server. If you don't have one, you can use a free Redis provided by Heroku:

$ heroku addons:create heroku-redis:hobby-dev -a cubejs-heroku-demo

If you use another Redis server, you should pass your Redis URL as an environment variable:

$ heroku config:set REDIS_URL:<YOUR-REDIS-URL>

Note that Cube.js requires at least 15 concurrent connections allowed by Redis server.

$ echo "web: node index.js" > Procfile

$ heroku config:set \
  CUBEJS_DB_HOST=<YOUR-DB-HOST> \
  CUBEJS_DB_NAME=<YOUR-DB-NAME> \
  CUBEJS_DB_USER=<YOUR-DB-USER> \
  CUBEJS_DB_PASS=<YOUR-DB-PASSWORD>

$ git add -A
$ git commit -am "Initial"
$ git push heroku master

$ cubejs create cubejs-docker-demo -d postgres
$ cd cubejs-docker-demo

$ touch Dockerfile
$ touch .dockerignore

Example Dockerfile

FROM node:10-alpine

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 4000

Example .dockerignore

node_modules
npm-debug.log
.env
schema

$ docker build -t <YOUR-USERNAME>/cubejs-docker-demo .

To run docker image, you need to set environment variables required by Cube.js Backend. Generate a secret for JWT Tokens as described in Security section and fill in database credentials. Also you need to provide a path to the directory with Data schema files.

$ docker run -p 49160:8080 \
  -d \
  --name cubejs-docker-demo \
  -e CUBEJS_API_SECRET=<YOUR-API-SECRET> \
  -e CUBEJS_DB_HOST=<YOUR-DB-HOST-HERE> \
  -e CUBEJS_DB_NAME=<YOUR-DB-NAME-HERE> \
  -e CUBEJS_DB_USER=<YOUR-DB-USER-HERE> \
  -e CUBEJS_DB_PASS=<YOUR-DB-PASS-HERE> \
  -e CUBEJS_DB_TYPE=postgres \
  -v <PATH-TO-SCHEMA>:/usr/src/app/schema \
  <YOUR-USERNAME>/cubejs-docker-demo

$ docker stop cubejs-docker-demo

To run the server in docker-compose we need to add a redis server and a .env file to include the environment variables needed to connect to the database, the secret api secret and redis hostname.

Example .env file

REDIS_URL=redis://redis_db:6379/0

CUBEJS_DB_HOST=<YOUR-DB-HOST-HERE>
CUBEJS_DB_NAME=<YOUR-DB-NAME-HERE>
CUBEJS_DB_USER=<YOUR-DB-USER-HERE>
CUBEJS_DB_PASS=<YOUR-DB-PASS-HERE>
CUBEJS_DB_TYPE=postgres
CUBEJS_API_SECRET=<YOUR-API-SECRET>

Example docker-compose file

redis_db:
  image: redis
  ports:
    - "6379"

cube:
  build: ./cube
  env_file: .env
  expose:
    - "4000"
  volumes:
    - ./cube/schema:/usr/src/app/schema
  command: node index.js
  links:
    - redis_db

./cube is te path to your cube js main folder, ./cube/schema is the path to your schema folder and the .env file should be at the same level of the docker-compose file.

Build the containers

$ docker-compose build

$ docker-compose up
$ docker-compose stop