cc by-sa flurdy

Heroku Tips

Quick tips and commands for Heroku.

Heroku themselves do have excellent documentation. This page is more extracts that I use myself frequently.

Sign up

Go to www.heroku.com and sign up with Heroku. Remember to also upload your ssh public key.

Install

(Heroku's own documentation)

(I will assume you already have Git installed)

First of all install the Heroku toolbelt:

On a Mac with Homebrew:

brew install heroku-toolbelt

On Ubuntu either:

wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh

or:

# add heroku repository to apt
echo "deb http://toolbelt.heroku.com/ubuntu ./" > /tmp/heroku.list
sudo cp /tmp/heroku.list /etc/apt/sources.list.d/heroku.list

# install heroku's release key for package verification
wget -O- https://toolbelt.heroku.com/apt/release.key | apt-key add -

# update your sources
apt-get update

# install the toolbelt
apt-get install -y heroku-toolbelt

Create

Go to your project's root folder, which I will assume is version controlled with Git.

The basic command to create a Heroku application is just:

heroku create

Which creates a heroku remote in git:

git remote -v

However you may want to specify further by optionally adding:

Heroku Stack

heroku create -s cedar

At the moment it is unnecessary as the previous stack is not an option and a new one has not been announced.

Specify App name

heroku create foobar

Heroku then creates foobar.herokuapp.com instead of their random but cute naming strategies.

Staging (Multiple remotes)

A very handy option is the ability to specify remote names. This allows you to have multiple application running on Heroku for the same project. Which is then very handy if you want a staging server to test your project on before deploying to your live instance.

heroku create --remote foobar

You can see your git remotes now have a foobar remote and not a heroku one:

git remote -v

As a consequence all heroku commands for this instance will need to be appended with, e.g.

heroku config --remote foobar

For more information read the documentation

All options together

I usually follow this pattern when I create my applications:

heroku create -s cedar foobar-live --remote live;
heroku create -s cedar foobar-staging --remote staging

Which means I have to subsequently consciously specify which remote I want a heroku command to run against. However for smaller project or at the start of project I only run with the default options.

Configure

You can configure your application easily, which allows nice external configuration. You don't however have an external configuration file, but instead use environment variables.

The environment variables may be urls, paths, passwords etc. A number of add-ons will automatically add entries for your application to use them.

To see which environment variables is currently set for your application:

heroku config

To add a varible:

heroku config:set FOO=bar

This will add a variable of FOO with the value bar.

heroku config

To remove a variable:

heroku config:unset FOO

Variable FOO now no longer exist.

heroku config

Play framework configuration example

In my Play & Heroku howto I show how I involve a Heroku only configuration file inside the application, and then in the Procfile I specify the path to this file. The configuration file then pulls the environment variable if present into the application using Typesafe config properties.

Add-ons

Heroku comes with a number very usefull add-ons. They are extremely easily integrated, and all offer a free tier.

Custom domains

The custom domains add-on lets you specify alternative domain name for your application. The default foobar.herokuapp.com might be good, but you can also let the application respond to www.foobar.com if you prefer.

The custom domains add-on is lately included by default with all instances so you will not need to add the add-on itself anymore.

To add a domain for the application to respond to:

heroku domains:add www.example.com

To list which domaind the application to responds to:

heroku domains

To remove a domain from the application:

heroku domains:remove www.example.com

For more information read the documentation

Database

A number of add-ons are available for a number of databases: PostgreSQL, MySQL, MongoDB, Redis, Riak, etc, etc.

PostgreSQL

Heroku supplies a free PostgreSQL database for your app to use.

To add it to your application:

heroku addons:add heroku-postgresql:dev

You can see in your applications environment variables if one is associated:

heroku config

Look for the variable DATABASE_URL. This is the url you application needs to use. For an example read my Play & Heroku howto's database settings.

You can also use the command line client to query the database:

heroku pg:psql

To reset the database and if you do not care about the data:

heroku pg:reset DATABASE_URL
Database backup

It is also wise to add the free database backup add-on:

heroku addons:add pgbackups:plus

To take a backup:

heroku pgbackups:capture
MongoDB

Pick either MongoLab or MongoHQ

heroku addons:add mongohq:sandbox

And use e.g. for MongoHQ the environment variable MONGOHQ_URL to connect to your Mongo node. For an example see this StackOverflow answer.

Email

A simple email client is SendMail.

heroku addons:add sendgrid:starter

This adds SENDGRID_USERNAME and SENDGRID_PASSWORD to your environment variables. You will need to use these variables and as well add smtp.sendgrid.net as your smtp host at port 465 and using ssl.

For an example see a blog post I wrote on how to send email via SendGrid on heroku,

Deploy to Heroku

Deploying to Heroku is very simple. It is simply a git push:

git push heroku master

If you have used multiple Heroku remotes:

git push foobar master

This will push the latest source code to Heroku. Heroku will try to establish wich language/framework to use, the presence of a Procfile will ensure it guesses correctly. It will then compile and if successful start your application.

When deployed ssuccessfullyand your application has started, you can quickly open your browser with:

heroku open

Logging

You have full access to the application log via

heroku logs

To continuously tail the log:

heroku logs -t

As there are often typos, misunderstanding etc that brake a deployment, (hence why I use staging environments first), I often quickly tail the log straight away after a deployment.

git push heroku master && heroku logs -t

And when the logs indicate the app is up and running again I run:

heroku open && heroku logs -t

Scale and restarts

To find out how many dynos are running:

heroku ps

To scale up or down:

heroku ps:scale web=1

Restart

If you application has crashed, but you fixed the problem without needing a redeploy you can restart the application node with:

heroku restart web.1

For more information read the documentation

Destroy

If you destroy an application, remember it might be a good idea to:

heroku destroy

In case of multiple remotes you need to specify the application name:

heroku destroy -a foobar

You will be prompted to confirm the deletion of application

Feedback

wont fix t-shirt
shirts.flurdy.com
US UK EU