How to deploy a Play application to AWS

Amazon Web Services – AWS – is one of the most popular Cloud platforms. It hosts over 200 unique services including docker container hosting, load balancer, database hosting, file storage.

CloudCaptain – Boxfuse

CloudCaptain (previously known as Boxfuse) is a tool that creates minimal immutable instances of your application and allows you to easily deploy them to AWS. Because they are basically VM instances, they ensure that whole environment is the same, no matter if you run it on your local machine, test or production environment.

First things you need to do, to start using CloudCaptain are:

  1. Creating CloudCaptain account
  2. Connecting your AWS account to CloudCaptain account
  3. Installing CloudCaptain client
  4. Installing VirtualBox

Setting up connection to database

If your project uses PostgreSQL or MySQL JDBC driver, CloudCaptain will automatically detect it, create database by “Database auto-provisioning” and configure data source in Play’s config. In the dev environment, it will launch a VM with a configured database. For test and prod environments it will automatically connect to a single or multi-AZ AWS RDS instance.

If you wan’t to explicitly set a database connection to CloudCaptain‘s data source, you can use environment variables passed to the created VM.

  • BOXFUSE_DATABASE_URL – database JDBC url
  • BOXFUSE_DATABASE_USER – username
  • BOXFUSE_DATABASE_PASSWORD – password

One more useful variable is BOXFUSE_HOST_IP. It represents your host machine IP address. This will allow you to connect to a database hosted on your machine using LAN – so it will only work in dev environment. If you won’t use “Database auto-provisioning”, you should add -db.type=none to run command.

Play configuration

Since CloudCaptain runs Play application in production mode you will need to set Play’s application secret in application.conf. Next thing that needs to be set is allowed hosts filter. You must allow connections from all IPs. It should look like this:

play.filters.hosts.allowed = ["."] 

 

Building Play application

Next, after configuration it’s time to build your application. You need to cd into your application’s main directory and run:

sbt clean dist 

This command removes previous build data and creates .zip archive with your current application version. It will also include all configuration files.

Running and deploying application to AWS

It’t time for running your application in dev environment – locally on your machine. You need to run command presented below from your project main directory.

boxfuse run -env=dev 

This command fuses your packaged Play application with CloudCaptains minimal VM image and runs it locally. As mentioned previously, if you don’t want to use “Database auto-provisioning”, you should add -db.type=none to command above.

When you’re ready to deploy your application to the prod environment on AWS, you just need to run:

boxfuse run -env=prod 

 

Application updates on AWS

CloudCaptain supports blue/green deployment. This method of releasing applications allows for zero downtime updates. It will create a new node, running a new version of your application and gradually move its whole traffic to it.

To update your application in production, all you need to do is bump up the apps version number in config file, build it using the same command as previously and run:

boxfuse run -env=prod 

 

Important note!

Make sure that you’re running your application on the same Java version that you used to build it. CloudCaptain by default will use the newest OpenJDK version. You can override it by adding -components.openjdk=x.x.x to the boxfuse runcommand where x.x.x stands for the OpenJDK version.