Fresh off learning some of the practical applications of Golang, I ventured on my own to create a working project. I then stumbled upon a weird issue, the steps to deploying an application written in Golang to Heroku were scattered everywhere. From the official documentation to some source code, I even stumbled upon an outdated way of doing things.
The purpose of the article is to put everything in one place, all the commands I used and everything I learnt while trying to push the application into the wild. Before we proceed, this article is starting with the assumption that you have already created a Heroku account AND you're already familiar with the Git Version Control system. If not, you can install Git here and you can create your Heroku account here
To make this as fun and structured as possible, we’ll dive into the steps I took in the sections.
Installation
The first thing I needed to do was to install Heroku CLI. A CLI is a command line interface tool that can be used to communicate with a particular service, in this case, that service is Heroku. Since I’m using a Mac machine, I was able to install this CLI via Homebrew using a variant of the following command:
brew install heroku
It’s also worth mentioning, that since I’m using a silicon-based machine to be exact, this command had to be run with that in mind. It leads to this variant.
arch -x86_64 brew install heroku
You can find how to install Heroku CLI on your OS here.
Authentication
To deploy your application, you need to be logged into your account. You need to “cd”
into your project (i.e open a terminal in the base of your project) for the next commands to work as they should. To check whether or not you are authenticated. The following command is called:
heroku auth:whoami
To log into your account, the following commands can be used:
heroku login //This opens a login page on your browser
heroku login -i //This allows you to log in using the terminal itself
A successful login response will be displayed in the terminal after you have inputted your correct credentials (email and password).
Create Heroku Application
We’ve logged into your account and it’s time to create our application. To do this, we call in our terminal:
heroku create
This will create an application with a particular URL which you can use to access it. This application is your main
application and subsequent changes that need to be deployed should be done so using the main
branch name.
Other branches/environments can be set up using Heroku’s pipeline system but that is a topic for another day. Right now, we’ll focus on the basics. Note: I found that the port to use to access your application after deployment i.e the port to listen to in your code, can be retrieved using the os.Getenv("PORT")
A snippet will look like this:
// I am using fiber as for my http server
router := fiber.New()
//When deployed this makes sure to listen to the port on which
//your application is deployed
router.Listen(":" + os.Getenv("PORT"))
Deploying Changes
Suddenly, you realise you’re not logging some important information, or you’re a little wrong on some logic. You can make the change, commit it to git and then call:
git push heroku main
This will push the change to your already deployed application and update the logic there.
Logging
When your deployed application doesn’t work the way it’s supposed to, you can check what is wrong by logging. Heroku prints very verbose logs and this can be used to your benefit. A simple log.Println(“”)
will also show up in logs printed from the application deployed to heroku.
To read these logs, you can call this command:
heroku logs --tail
You can always Ctrl + C
after reading the logs to do some work. But it’s better to run these logs in another terminal to be in the loop at all times. This will show you the logs connected to your main application since it's the only one we have deployed.
Renaming URL/Application
By default, Heroku gives you a randomised name for your application once it is created. I prefer to give this application names that I prefer. I did this with the command:
heroku apps:rename my-real-api-name
The names have to follow a certain convention and they cannot contain dots, for one, which was a bit of a letdown in my case but dashes would do.
Retrieving Variables from ConfigVars
When I read about Heroku’s ConfigVars
, I knew I needed to use them. So I had to rework my system which was very dependent on .env files. It was straightforward to read these values from my .env file using Viper and a struct, but reading from ConfigVars was even simpler.
ConfigVar variables are set up in the settings bar of your Heroku dashboard. After adding a key and a value, you can access them in your code using os.Getenv("EXTERNAL_API_KEY")
. A snippet would look like this:
//This will return an empty string if the environment variable doesn't exist
apiKey := os.Getenv("EXTERNAL_API_KEY")
//To distinguish between an unset variable and an empty variable,
//you can use LookupEnv instead
You can set various config vars for different kinds of environments setup via a Heroku pipeline, but once again, that is a story for another day :)
Conclusion
No other steps are needed to create and deploy a basic Heroku application. Some of these commands were simple but obscure and I’m happy to be able to put them all in one place so other beginners like me can see them. Please don’t forget to share and follow me on Twitter @neo_femo
P.S.: I am looking for a junior/internship backend developer position with Golang so please do not hesitate to reach out.
Thanks for reading.