Erlang is a programming language used to build massively scalable soft real-time systems with requirements on high availability.
Cowboy is a small, fast and modular HTTP server written in Erlang.
Heroku is a cloud application platform which allows you to deploy and scale your own application at pleasure.
In this post, we will write an Erlang/Cowboy Web Application from scratch, deploying it on Heroku. Since Heroku offers a free tier for deploying web apps, this will cost you nothing. Also, the entire process should take no more than half an hour.
Requirements
- You have git installed on your machine.
- You have Erlang installed on your machine. In this example I will be using R15B01.
- You are familiar with the basic concepts of Erlang and OTP
- You have a free account on Heroku.
- You have the Heroku start-kit for your OS installed.
Summary
- Create the skeleton of an Erlang app using Rebar
- Create a Heroku application
- Use Cowboy to create a simple web app
- Compile and run your application locally
- Configure your Heroku app
- Deploy your Erlang application on Heroku
- Profile your deployed Erlang application
Create the skeleton of an Erlang app using Rebar
Rebar is the de-facto standard build-tool for Erlang projects.
Fetch rebar from Github and bootstrap it:
Initialize a new git repository and use rebar to create the skeleton for a new Erlang app. I decided to call my application erlblog. Call your application differently, replacing every occurrence of erlblog with your favourite application name in the instructions below. Please note that this is not optional, since two applications cannot have the same name on Heroku and you don’t dare to clash with my own application.
Commit what you have done in git:
Create a Heroku application
Login into Heroku using the heroku command from the terminal.
Use your Heroku email and password to login.
Now create a new Heroku app, using the Erlang buildpack from @archaelus:
Use Cowboy to create a simple web app
Add the Cowboy web server as a rebar dependency:
Add cowboy to the list of applications in your .app.src file. Also, set the http_port environment variable to 8080 (see next paragraphs).
Modify the start/2 function from the erlblog_app module so that Cowboy starts a pool of acceptors when the erlblog application is started. Configure the Cowboy dispatcher with a single dispatching rule, routing all requests to ’/’ to the erlblog_handler (see below).
Heroku assigns random ports to your application and uses the OS environment variable $PORT to inform you about the port on which your web server should listen to. Therefore, in the following code we read that environment variable, defaulting to port 8080 in case the environment variable is not specified. This is useful, for example, if you want to try your web server locally before deploying it on Heroku.
Let’s now implement a basic HTTP Cowboy handler, which simply replies with a 200 status code and a notorious welcoming message to any incoming request:
Finally, let’s create an interface module which will be responsible for starting your erlblog application together with all its dependencies.
Compile and run your application locally
Compile the erlblog application using rebar:
Start the application and verify that everything works as expected:
From the Erlang shell, type:
The erlblog application should be included in the output.
Finally, point your browser to:
And verify that the string “Hello World!” is there.
You can use Ctrl-G q to exit the Erlang shell.
If everything works as expected, commit everything to git:
Configure your Heroku app
You need to tell Heroku that you’re going to deploy an Erlang Application. To do so, you need to create a Procfile file, containing your start-up script:
Commit your changes to git:
You also want to specify that your application requires Erlang R15B01:
Commit your changes to git:
Deploy your Erlang application on Heroku
That’s the beautiful part:
You should now be able to access the erlblog application at:
If something does not work as expected, you might want to verify the logs for your Heroku app:
Profile your deployed Erlang application
Let’s now verify how many requests our erlblog application can handle. Please note that to run the steps below, you need ab and gnuplot installed on your machine.
Using ApacheBench, perform 5000 HTTP requests against your new web server, using 20 concurrent requests. Store the output in the gnuplot.dat file.
Using our Heroku free tier (1 single dyno worker), our Cowboy Web Server managed to complete all 5000 requests, allowing ~70 requests per second. 90% of the requests have been served in about 300 ms. Of course, such a good result has been possible only because we surely have been hitting some kind of cache in the Heroku servers. Still, not bad for a free hosting solution for a simple Erlang applications.
We can visualize the above results using Gnuplot:
The complete source code for the erlblog application is available here.