From Erlang, through Elixir with Phoenix Framework to Heroku by Travis CI

Introduction

Recently, I’ve been playing with Phoenix Framework a lot. It’s written in and for the Elixir Language that has a very similar syntax to Ruby.

A note from the Author of the language:

Elixir is a dynamic, functional language designed for building scalable and maintainable applications.

Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.

A word about the framework itself:

Phoenix is a framework for building HTML5 apps, API backends and distributed systems. Written in Elixir, you get a beautiful syntax, productive tooling and a fast runtime.

Phoenix leverages the Erlang VM ability to handle millions of connections concurrently while also enjoying the VM guarantees for building fault-tolerant systems.

As you already know, I’m a fan of automation so I wanted to show you how to setup a Phoenix project from nothing at all to a continuously deployed web application hosted on Heroku.

# Erlang

1. Download Erlang Solutions repository

2. Add the repository entry to the registry

$ rpm -Uvh erlang-solutions-1.0–1.noarch.rpm

3. Install Erlang

$ sudo yum install erlang

You can run now:

$ erl

Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace] Eshell V6.4 (abort with ^G)

1>

# Elixir lang

1. Download precompiled Elixir package

2. Unzip the downloaded binaries

$ unzip Precompiled.zip

You can run now:

$ iex

Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace] Interactive Elixir (1.0.4) — press Ctrl+C to exit (type h() ENTER for help)

iex(1)>

or:

$ elixir

Usage: elixir [options] [.exs file] [data] -v Prints version and exit

-e “command” Evaluates the given command (*)

-r “file” Requires the given files/patterns (*)

-S “script” Finds and executes the given script

-pr “file” Requires the given files/patterns in parallel (*)

-pa “path” Prepends the given path to Erlang code path (*)

-pz “path” Appends the given path to Erlang code path (*)

— app “app” Start the given app and its dependencies (*)

— erl “switches” Switches to be passed down to erlang (*)

— name “name” Makes and assigns a name to the distributed node

— sname “name” Makes and assigns a short name to the distributed node

— cookie “cookie” Sets a cookie for this distributed node

— hidden Makes a hidden node

— detached Starts the Erlang VM detached from console

— no-halt Does not halt the Erlang VM after execution * Options marked with (*) can be given more than once

** Options given after the .exs file or — are passed down to the executed code

** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTIONS or — erl

or even:

$ mix new app

* creating README.md

* creating .gitignore

* creating mix.exs

* creating config

* creating config/config.exs

* creating lib

* creating lib/app.ex

* creating test

* creating test/test_helper.exs

* creating test/app_test.exs

# Phoenix framework

1. Install the Hex package manager

$ mix local.hex

2. Fetch the Phoenix installer

You can run now:

$ mix phoenix.new app

* creating app/config/config.exs

* creating app/config/dev.exs

* creating app/config/prod.exs

* creating app/config/prod.secret.exs

* creating app/config/test.exs

* creating app/lib/app.ex

* creating app/lib/app/endpoint.ex

* creating app/priv/static/robots.txt

* creating app/test/controllers/page_controller_test.exs

* creating app/test/views/error_view_test.exs

* creating app/test/views/page_view_test.exs

* creating app/test/support/conn_case.ex

* creating app/test/support/channel_case.ex

* creating app/test/test_helper.exs

* creating app/web/controllers/page_controller.ex

* creating app/web/templates/layout/application.html.eex

* creating app/web/templates/page/index.html.eex

* creating app/web/views/error_view.ex

* creating app/web/views/layout_view.ex

* creating app/web/views/page_view.ex

* creating app/web/router.ex

* creating app/web/web.ex

* creating app/mix.exs

* creating app/README.md

* creating app/lib/app/repo.ex

* creating app/test/support/model_case.ex

* creating app/.gitignore

* creating app/brunch-config.js

* creating app/package.json

* creating app/web/static/css/app.scss

* creating app/web/static/js/app.js

* creating app/web/static/vendor/phoenix.js

* creating app/priv/static/images/phoenix.png

# Heroku

1. Create a Heroku application

$ heroku create phoenix-dci — buildpack “ https://github.com/HashNuke/heroku-buildpack-elixir.git

2. Configure a Heroku buildpack

3. Set Heroku environment variables

$ heroku config:set MIX_ENV=prod

$ heroku config:set SECRET_KEY_BASE=…

4. Create a Procfile

$ echo ‘web: mix phoenix.server’ > Procfile

5. Create an Erlang config

$ echo ‘

erlang_version=17.5

elixir_version=1.0.4

always_rebuild=true

config_vars_to_export=(DATABASE_URL)

‘ > elixir_buildpack.config

6. Deploy to Heroku

$ git push heroku master

7. Scale dynos on Heroku

$ heroku ps:scale web=1

# Travis CI

To configure .travis.yml you can run:

$ travis setup heroku

Generated .travis.yml should look like:

language: elixir otp_release:

— 17.4 deploy:

provider: heroku

api_key:

secure: SOME_GENERATED_KEY

app: app-name

on:

repo: user-name/app-name

And then, after each push, Travis will perform mix test and, on success, it will deploy the application to Heroku.

Summary

As you can see, everything went fine. It was very easy to set up and the only thing you need to do now is to cover these steps to have your app up and running!

Ansible

If you want to have everything installed on your own server, you may wanna check my Ansible role for that:

https://github.com/KamilLelonek/ansible-playground/tree/master/roles/phoenix