Deploy Play Framework Scala Apps effortlessly to AWS

When it comes to creating modern JVM-based applications we have a number of excellent choices including Spring Boot, Dropwizard, Tomcat, TomEE and the Play Framework.

Today we're going to look at deploying Play applications written in Scala effortlessly to AWS using CloudCaptain.

We'll do so using the 3 core principles behind CloudCaptain:

1. Immutable Infrastructure Creating servers and never modifying them again by treating a server as one immutable unit that is regenerated after every change and promoted unchanged from environment to environment to eliminate drift and increase reliability by ensuring you run the exact same code in production as the one you tested in test.
2. Minimal Images Analysing your application and generating minimal tailor-made Linux-based images on the fly that are 100x smaller than a typical Linux system and take just seconds to produce.
3. Blue/Green deployments Deploying a new version of an app in parallel to the existing one and only making the switch at the elastic IP or elastic load balancer level once the configured health checks of the new version have passed. Deployments are fully automated and effectively transactional, providing you with zero-downtime updates.

Prerequisites

Before we get started ensure you have created a CloudCaptain account (it's free, just log in with your GitHub id). Make sure to associate it with your AWS account in the CloudCaptain console to deploy on EC2.

You will also need a JDK, the Typesafe Activator and VirtualBox installed on your machine.

Step 0: Creating the Play application

Start by creating a Play Scala application using the Typesafe Activator:

> activator new hello play-scala

And navigate to the newly created directory:

> cd hello

Then set the version in build.sbt to 1.0:

version := "1.0"

Finally build a distribution zip:

hello> activator dist

Great. Your Play application distribution zip is now available under target/universal/hello-1.0.zip.

Step 1: Fusing a CloudCaptain image and running it on VirtualBox

Play Framework App to CloudCaptain Bootable App

Now it's time to fuse your application into a CloudCaptain image and launch an instance of it on VirtualBox:

hello> boxfuse run

This command will analyse your application and generate a minimal Linux-based image for it, which we call a Bootable App. It does this by combining your application itself with a JRE and a Linux kernel from the CloudCaptain Component Inventory. It will then launch an instance of your new image on VirtualBox. When it completes you should see a message like this:

Payload started in 00:06.688s -> https://127.0.0.1:10080

Now open your browser and navigate to this address to see your new application up and running within the new VirtualBox VM:

You can also see your newly created image:

hello> boxfuse ls

Images available locally:
+------------------+---------------+-------+---------+-----------+------------+---------+---------------------+
| Image            |    Payload    | Debug |  Java   | AppServer |   Ports    |  Size   |    Generated at     |
+------------------+---------------+-------+---------+-----------+------------+---------+---------------------+
| myuser/hello:1.0 | hello-1.0.jar | false | 8.60.22 | Play      | http -> 80 | 75950 K | 2015-11-12 14:57:50 |
+------------------+---------------+-------+---------+-----------+------------+---------+---------------------+
Total: 1

As well as the instance that is running:

hello> boxfuse ps

Running Instances on VirtualBox in the dev environment :
+-------------+------------------+---------------------+------------------------+---------------------+
|  Instance   |      Image       |        Type         |           URL          |     Launched at     |
+-------------+------------------+---------------------+------------------------+---------------------+
| vb-b18d6746 | myuser/hello:1.0 | 4 CPU / 1024 MB RAM | https://127.0.0.1:10080 | 2015-11-12 14:57:56 |
+-------------+------------------+---------------------+------------------------+---------------------+
Total: 1

Step 2: Deploying your application to AWS

Now let's deploy your image to AWS. As CloudCaptain works with your AWS account, it first needs the necessary permissions to do so. So if you haven't already done it, go to the CloudCaptain Console and connect your AWS account now.

Every new CloudCaptain account comes with 3 environments: dev, test and prod. dev is your local VirtualBox environment and test and prod are on AWS.

So let's deploy our application to the prod environment on AWS:

hello> boxfuse run -env=prod

Pushing myuser/hello:1.0 ...
Verifying myuser/hello:1.0 ...
Waiting for AWS to create an AMI for myuser/hello:1.0 in eu-central-1 (this may take up to 50 seconds) ...
AMI created in 00:34.394s in eu-central-1 -> ami-ed988b81
Creating Elastic IP ...
Mapping hello-myuser.boxfuse.io to 52.28.107.167 ...
Creating security group boxsg-myuser-prod-hello-1.0 ...
Launching t2.micro instance of myuser/hello:1.0 (ami-ed988b81) in prod (eu-central-1) ...
Instance launched in 00:26.922s -> i-5de042e1
Waiting for AWS to boot Instance i-5de042e1 and Payload to start at https://52.28.121.55/ ...
Payload started in 00:51.516s -> https://52.28.121.55/
Remapping Elastic IP 52.28.107.167 to i-5de042e1 ...
Waiting 15s for AWS to complete Elastic IP Zero Downtime transition ...
Deployment completed successfully. myuser/hello:1.0 is up and running at https://hello-myuser.boxfuse.io/

Notice that CloudCaptain has reused your image unchanged instead fusing a new one.

With that one command CloudCaptain has automatically pushed your image to the CloudCaptain Vault, our secure only image repository, as well as provisioned, configured and secured all necessary AWS resources including a domain name, an elastic IP, a security group and your instance itself. There is no manual work necessary on your behalf.

All you need to do is simply navigate to your new domain to see your Play application in action on AWS:

For our Play Framework backend, we used CloudCaptain to minimize the effort required to deploy updates and hotfixes to AWS. Now we can focus fully on improving the application itself. Further, the support is fantastic - quick, friendly and helpful. More than one would expect for such a low price.

Konrad Peters, Lead Developer, Appguru

Step 3: Updating your application using blue/green deployments

Now let's take things one step further and deploy an update of your application with zero downtime using blue/green deployments.

Start by modifying app/controllers/Application.scala with a simple change:

package controllers

import play.api._
import play.api.mvc._

class Application extends Controller {
  def index = Action {
    Ok(views.html.index("Updated by CloudCaptain with zero downtime!"))
  }
}

then bump the version in build.sbt:

version := "1.1"

and rebuild the dist:

hello> activator clean dist

Finally, deploy the new version of your application to AWS:

hello> boxfuse run -env=prod

Fusing Image for hello-1.1.zip ...
Image fused in 00:09.817s (75949 K) -> myuser/hello:1.1
Pushing myuser/hello:1.1 ...
Verifying myuser/hello:1.1 ...
Waiting for AWS to create an AMI for myuser/hello:1.1 in eu-central-1 (this may take up to 50 seconds) ...
AMI created in 00:34.152s in eu-central-1 -> ami-8b988be7
Creating security group boxsg-myuser-prod-hello-1.1 ...
Launching t2.micro instance of myuser/hello:1.1 (ami-8b988be7) in prod (eu-central-1) ...
Instance launched in 00:35.372s -> i-ebea4857
Waiting for AWS to boot Instance i-ebea4857 and Payload to start at https://52.29.129.239/ ...
Payload started in 01:05.316s -> https://52.29.129.239/
Remapping Elastic IP 52.28.107.167 to i-ebea4857 ...
Waiting 15s for AWS to complete Elastic IP Zero Downtime transition ...
Terminating instance i-5de042e1 ...
Destroying Security Group sg-48ea9921 ...
Deployment completed successfully. myuser/hello:1.1 is up and running at https://hello-myuser.boxfuse.io/

And there it is:

Summary

In this post, we saw how to deploy and update a Play Framework Scala application to AWS in 3 easy steps. First we fused our application into an minimal image called a Bootable App and ran an instance of it on VirtualBox. We then deployed the same image unchanged to AWS. And finally we updated our application on AWS with zero downtime using blue/green deployments.

To do so we used CloudCaptain and its 3 core principles: Immutable Infrastructure, Minimal Images, Blue/Green deployments.

So if you haven't already, sign up for your CloudCaptain account now (simply log in with your GitHub id, it's free) and start deploying and auto-scaling your Play applications effortlessly on AWS today.

« It's Auto-Scaling time!
Provisioning temp space »