How to save Heroku dyno formation with the CLI

Loïc CHOLLIER
Tech at Trax
Published in
2 min readFeb 1, 2017

At ̶Q̶u̶r̶i̶ Trax Retail (Quri was acquired by Trax Retail in January 2018) we use Heroku to power most of our back-end systems. (tl;dr down there)

One of the powerful features with Heroku is that you can easily scale up and down your different services independently. Here is what the Heroku Resources Dashboard looks like:

Screenshot of the Dyno Formation UI

Now let’s imagine you want to do some maintenance on your app, or simply shutdown your staging or sandbox servers during the night.

For every single row in the above the screenshot, you would need to turn down the dyno count to 0, then click confirm:

This gets cumbersome, especially when you are scaling the dynos back up and would need to look at a screenshot like the first one to remember what was the dyno formation.

Here comes the Heroku CLI:

Most — if not all — the actions you can perform in the UI can be performed with the command line tool:

$ heroku help
Usage: heroku COMMAND [--app APP] [command-specific-options]
Primary help topics, type "heroku help TOPIC" for more details: addons # manage add-on resources
apps # manage apps (create, destroy)
auth # authentication (login, logout)
config # manage app config vars
domains # manage domains
logs # display logs for an app
ps # manage dynos (dynos, workers)
releases # manage app releases
run # run one-off commands (console, rake)

We will focus here on the heroku ps command that lets us manage the dynos and workers. With heroku ps:scale, we’re able to get the same screenshot that the first one I shared describing the dyno formation but as text:

$ heroku ps:scaleclock=1:Standard-1X console=0:Standard-1X mq_workers_high_priority=1:Performance-L mq_workers_low_priority=2:Performance-M mq_workers_photo=10:Standard-2X rake=0:Standard-1X renderer=1:Performance-L scheduler=1:Standard-1X web=3:Performance-M

Now heroku ps:scale can also take as input the exact same format it outputs. This opens an interesting perspective: instead of having to manually scale every single dynos or worker, we would be able to automate that.

Saving your dyno formation to a text file can be achieved with:

$ heroku ps:scale > mydynoformation

Scaling all dynos to zero with some regular expression magic becomes as simple as:

$ heroku ps:scale $(heroku ps:scale | sed -E 's/=([0-9]+):/=0:/g')

Scaling back your app to it’s initial state becomes:

$ heroku ps:scale `cat mydynoformation`

TLDR;

--

--