Back to clever-cloud.com

Well, if you need some logos, we provide these:

svg ·  png
Red Clever Cloud logo
svg ·  png
svg ·  png
White Clever Cloud logo
svg · png
White Clever Cloud logo
svg · png
Posted at June 9, 2022 — Engineering

Deploy a Scala / Akka application with PostgreSQL integration

When I started working at Clever Cloud, I spent some time messing around with the platform and its tooling. This blog post will present you how I managed to:

  • create a simple Scala/Akka HTTP template application with PostgreSQL persistence
  • link the application to a managed PostgreSQL
  • deploy it
  • try it!

Create a Scala/Akka HTTP template 🔨

Get the sources

Clone the project generated from Akka Github quick start template.

This service provides the ability to manage a simple in-memory user registry exposing 4 routes:

  • List all users
  • Get a specific user
  • Create a user
  • Delete a user

A thorough description of available the cURL commands may be found here.

Add extra features

A few functionalities have been added on top of the original example:

  • store the registry into a PostgreSQL database (instead of in memory)
  • read database parameters from environment variables
  • add a hardcoded basic authentication

All parameters may be specified in the application.conf file:

  • basic auth
  • database parameters

Parameters are fetched from environment variables, or the specified default value if none is found

app {
  basic-auth {
    user = "foo"
    user = ${?BASIC_AUTH_USER}
    password = "bar"
    password = ${?BASIC_AUTH_PASSWORD}
  }
  routes {
    # If ask takes more time than this to complete the request is failed
    ask-timeout = 5s
  }

  db {
    host = "localhost"
    host = ${?POSTGRESQL_ADDON_HOST}
    port = "5432"
    port = ${?POSTGRESQL_ADDON_PORT}
    database = "postgres"
    database = ${?POSTGRESQL_ADDON_DB}
    user = "login"
    user = ${?POSTGRESQL_ADDON_USER}
    pass = "pass"
    pass = ${?POSTGRESQL_ADDON_PASSWORD}
  }
}

Run the application using Clever Cloud CLI 🚀

Create a Clever Cloud application

clever create --type sbt myakka --region par --org testorg

This command creates a new application

  • of type sbt for Scala
  • named myakka
  • in Paris region
  • inside my test organisation testorg

Enable a dedicated build instance for faster build (optional pro tip)

By default a newly created application will run an XS instance which are quite small for building Scala applications fast. One way to build faster is to use a dedicated build Instance: In the Clever Cloud console, got to your application -> options, and check the box [] Enable dedicated build instance This way you can keep your XS instance for running the application, but you can choose an XL for the build time. Or with the command line tool:

clever scale -a myakka --build-flavor XL

Create a PostgreSQL database

clever addon -l myakka create postgresql-addon myakkadb

This command orders a PostgreSQL add-on instance and link it to myakka, injecting the right environment variables into the application instance to contact the database.

Database creation and migration is performed using flywaydb tool. Those script may be found in the migration/db directory of the application resources.

Migration will be automatically called once you specify the proper build hook in the environment variables

CC_POST_BUILD_HOOK with value sbt flywayMigrate

clever env -a myakka set CC_POST_BUILD_HOOK "sbt flywayMigrate"

Configure basic auth environment variables

Basic authentication login and password can be specified as environment variables using the clever env command. If none is found, basic authentication will default to application.conf file values

clever env -a myakka set BASIC_AUTH_USER <YOUR_AUTH_USER>
clever env -a myakka set BASIC_AUTH_PASSWORD <YOUR_AUTH_PASSWORD>

Link your local repo with your Clever Cloud application instance

Retrieve your application id with

clever applications

Then link the local repository to the application (creating a .clever.json file)

clever link app_<UUID>

Deploy

You are now ready to deploy and run your code, just run

clever deploy

You are now able to follow the deployment process until it completes successfully

Your application is running now!

Try it

It’s time to experiment with it using curl or your favorite GUI

Add a user

Adapt this command in order to insert a new user into the registry:

curl --request POST \
  --url https://app-<UUID>.cleverapps.io/users \
  --header 'Content-type: application/json' \
  -u '<YOUR_AUTH_USER>:<YOUR_AUTH_PASSWORD>' \
  --data '{
  "name": "Serge",
  "age": 42,
  "countryOfResidence": "Greenland"
}'

{
	"description": "User Serge created."
}

Get all users

Check that the user has been properly created, by getting the list of users:

curl --request GET \
  --url https://app-<UUID>.cleverapps.io/users \
  -u '<YOUR_AUTH_USER>:<YOUR_AUTH_PASSWORD>'

{"users":[{"age":42,"countryOfResidence":"Greenland","name":"Serge"}]}

It works, Success! 🎉

Tags:

Geoffrey GUILBON

Software engineer @clever_cloud working on scalable applications

Check out our Blog

We write about programming, security and Clever Cloud products

Join thousands of developers already using Clever Cloud. And try it for free!

Create your account now to start your apps and collaborate within a few minutes. For free with 20€ offered.

By signing up, your agree to our terms of use
Or
Sign in with Github