Deploying akka-http app to Cloud Foundry

It is easy to deploy an akka-http application to Cloud Foundry. I experimented with a few variations recently and will cover ways to deploy an Akka-http based REST app in two parts – first a simple app with no external resource dependencies, the second a little more complex CRUD app that maintains state in a MySQL database.

Pre Requisites

A quick way to get a running Cloud Foundry instance is using PCF Dev, a small footprint distribution of Cloud Foundry that can be started up on a developer laptop.

The sample app that I am using is a stock demo app available via the Lightbend Activator, if you have activator binaries available locally, you can create a quick project using the following command:

Generating the sample App and running it locally

activator new sample-akka-http akka-http-microservice

The application can be brought up by running sbt and using the “re-start” task

$ sbt
> re-start

By default the app comes up on port 9000 and can be tested with a sample CURL call – more here:

$ curl http://localhost:9000/ip/
  "city": "Mountain View",
  "query": "",
  "country": "United States",
  "lon": -122.0881,
  "lat": 37.3845

Deploying to Cloud Foundry

There is one change that needs to be made to the application to get it to work in Cloud Foundry – adjusting the port where the application listens on. When the app is deployed to Cloud Foundry, an environment variable called “PORT” is the port that the application is expected to listen on. This change is the following for the sample app:

val port = if (sys.env.contains("PORT")) sys.env("PORT").toInt else config.getInt("http.port")
Http().bindAndHandle(routes, config.getString("http.interface"), port)

Here I look for the PORT environment variable and use that port if available.

There is already the “assembly” sbt plugin available which creates a fat jar with the appropriate entries, to be able to start up the main class of the application

> assembly

Go to “target/scala-2.11” folder and run the fat jar:

$ java -jar sample-akka-http-assembly-1.0.jar

And the application should come up cleanly.

Having a fat jar greatly simplifies the deployment to Cloud Foundry – In the Cloud Foundry world, a buildpack takes the application binaries and layers in the runtime(jvm, a container like tomcat, application certs, monitoring agents etc). Given this fat jar all that is needed to deploy to Cloud Foundry is a command which looks like this:

$ cf push -p sample-akka-http-assembly-1.0.jar sample-akka-http

Assuming that you are targeting the local PCF Dev environment the application should get cleanly deployed using the appropriate buildpack( java buildpack in this instance) and be available to handle requests in a few minutes:

Which I can test using a curl command similar to what I had before:

$ curl http://
  "city": "Mountain View",
  "query": "",
  "country": "United States",
  "lon": -122.0881,
  "lat": 37.3845

That is all there is to it – if say some customizations need to be made to the application, say more jvm heap size, this can be easily done via other command line flags or using an
application manifest. The process to deploy with external resource dependencies is a little more complex and I will cover this in a follow up post.

Reference: Deploying akka-http app to Cloud Foundry from our JCG partner Biju Kunjummen at the all and sundry blog.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button