Ever wonder if it’s possible to automatically scale your app dynos up and down at specific hours or days? It totally is, and by the end of this post, you will find out exactly how it can be done.

Scaling dynos is just one of many possible actions available when scheduling Heroku CLI commands. In addition, you can also restart  dynos, run automated Heroku Postgres backups and restores, monitor and send alerts regarding Postgres issues (such as blocked or long running processes) or change your app configuration dynamically. There are just a few useful actions off the top of our heads that can be automated using the Heroku CLI.

In this guide, we’ll go through the steps required to schedule Heroku CLI commands quickly and efficiently.

Let’s get started, shall we?

Step 1 - Heroku CLI buildpack

First and foremost, you'll need to add the Heroku CLI buildpack to your app. Heroku Buildpacks are sets of open-source scripts used for compiling apps on Heroku, which allow you to expand the libraries and commands available to your app. Ultimately, they give you access to convenient and efficient code in the language and framework options that suit you best.

The Heroku CLI (Command Line Interface) buildpack is used to manage Heroku apps from the command line and allows you to run the heroku CLI in your dyno context alongside your app code.

Before adding the Heroku CLI buildpack, please make sure you have it installed locally, as we’ll be using it to add the buildpack. Then, run the following commands in your terminal:

  1. Add an environment variable that contains your Heroku API key.
$ heroku config:set HEROKU_API_KEY=`heroku auth:token`
  1. Add the Heroku CLI buildpack to your app:
$ heroku buildpacks:add heroku-community/cli [-a <your-app-name>]
  1. Finally, push your local repo to heroku:
$ git push heroku master

This should now grant you the ability to run Heroku CLI commands within the context of your dyno. To ensure that it is working properly, run the following command:

$ heroku run 'heroku version'

If the output is the Heroku CLI version (e.g. heroku/7.47.5 darwin-x64 node-v12.16.2) - great job!

On the other hand, if there is a resulting/output error, (e.g. bash: heroku: command not found), go back and make sure you’ve done everything according to the steps specified above.

Step 2 - Install Cron To Go, the scheduler add-on

For a brief background on Cron, we’ve covered the basics. Cron is a time-based job scheduler used in *nix systems to automate and schedule repetitive tasks to run routinely at fixed times, dates, or intervals.

When it comes to a time-based job scheduler, Cron To Go provides you with a fully managed, accurate, reliable, flexible, and scalable cloud solution. Cron To Go’s Heroku add-on allows you to utilize cron expressions when scheduling and automating tasks, track job execution, and receive email and webhook notifications regarding job status.

To install it in your app, you can use the option of the Heroku CLI:

heroku addons:create crontogo

Another option is to follow these steps through the UI:

  1. Go to your app dashboard.
  2. Click the resources tab.
  3. Type in cron to go under the addons section.
  4. Choose your plan and click submit order form.

It’s quick and simple, look for yourself:

Step 3 - Add a job to automate your Heroku CLI

Now that you’ve got Cron To Go ready at your command, it’s time to put it to work and get  some jobs going. In the following example, we’ll be adding 2 jobs which automatically scale dynos up in the morning and down in the evening for an imaginary app that’s used only during workdays.

Open Cron To Go’s dashboard via your Heroku app dashboard or by using the following command line:

$ heroku addons:open crontogo
Opening crontogo for sharp-mountain-4005

Next, click the little pink Add job button located right in the middle of the page:

Fuchsia, to be exact

Name the first job “Dyno scale down” and use the following cron expression and command:

  • Cron expression: 0 18 * * 1-5
  • Command: heroku dyno:scale web=1:Standard-1X

This job will run on weekdays (Monday to Friday) at 6pm and scale the app down to 1 Standard-1X dyno.

Click Add job to add this job and create another job. This time, name it “Dyno scale up” and use the following cron expression and command:

  • Cron expression: 0 9 * * 1-5
  • Command: heroku dyno:scale web=3:Standard-1X

This job will run on weekdays (Monday to Friday) at 9am and scale the app up to use 3 Standard-1X dynos. This way, you can save a whopping 57% of your dyno costs by running 2 of your 3 dynos for 9 hours a day (and only on weekdays since they like their weekends off too!), while still providing some availability during off-hours and weekends with a single dyno.

A whole new world of scheduling commands is now at your disposal; use it wisely, will you?

If you have any questions or suggestions in mind(or would just like to share your experience with Heroku CLI commands), do not hesitate to reach out to us via the chat button.


Post photo by NeONBRAND on Unsplash