Let’s deploy a GoLang service to the cloud.

Mahesh H P
codeburst
Published in
5 min readOct 19, 2019

--

Sometime back, I created a tiny URL generator using GoLang, PostgreSQL and Redis (More about it here). Now, it’s time to deploy it on the cloud.

Photo by Robert V. Ruggiero on Unsplash

In this article, I will be giving a quick run-down of steps I followed to deploy my GoLang service to the cloud. My cloud platform of choice is Heroku. It is simple, provides the required database and cache services out of the box and has a free tier!

Let’s begin by provisioning the required services on Heroku. Firstly, create an account, setup the CLI tools on your computer and create a new app from the website. The process is fairly simple and explained here. Once we have the app ready, we will add the required database and cache services for our application to run. The plugins I have chosen are:

a. Heroku Postgres: A Postgres database service.

b. Heroku Redis: A Redis cache service.

c. LogDNA: A log monitoring service.

Though all the plugins above offer a free tier, we have to be careful about usage limits as they have a pay-as-you-go charge/billing process. Feel free to explore and use plugins from the Heroku add-ons marketplace as there are aplenty!

Now that our Heroku application and required services are ready, we can deploy the code. Let’s start by refactoring the existing code to access the database and cache services, run on the port exposed by Heroku, add a Procfile to tell Heroku the library to deploy and add a build pack to build our app. We would also want to add a dependency manager to our code to make sure dependencies used locally and during deployment are consistent. This is also required by the build pack to build our app successfully.

Step 1: Setup Go build pack on Heroku

We can set the build pack by choosing the add build pack option in the app settings page:

Step 2: Choose the deployment method as GitHub

I will be using the GitHub branch-based deployment option provided by Heroku. To set this up, choose GitHub as the deployment option from the deployment config page of your application and connect your GitHub code repo by providing its URL and specifying the branch to be used for deployment.

I will be manually deploying my app. You can choose to automatically deploy as soon as commits are pushed to a branch as well.

Step 3: Add Procfile

This tells Heroku which binary to deploy. Since our app will compile as a binary with the name server inside the bin directory, our Procfile will look something like this:

Step 4: Refactor the code to connect to the external service through environment variables and expose it to external access

The connection strings of the Postgres database and Redis service which we are using as an add-on are exposed to the app through environment variables, which can be seen in the settings page of our Heroku app.

List of environment variables exposed to the app

I will be changing the code for the database and Redis client to pick the values from these variables shown above.

Using environment variables in code

We would have to import the os package to use its Getenv method to get the values from the environment variables.

Once we are able to access external services, it’s time to expose our application to the outside world. To achieve this, we will have to make our application server run on localhost and the port number defined in the PORT environment variable.

Configuring the application server

Step 5: Add a dependency manager

We will add a dependency manager to our go application to maintain the versions of required libraries and provide compile instructions to the ‘go build pack’ on Heroku. I will be using Godep as the manager. We can initialize/migrate our existing project to a Godep project by installing godep using this command: go get github.com/tools/godep , and running godep save. This will generate a Godeps.json file which contains the config. We will have to modify it according to our project structure to define the correct libraries to be compiled. In our case, since our main package to be compiled is inside the src directory, we define the same inside the Packages array.

The application’s Godeps.json file with modified Packages array for compilation

Note: Please make sure to commit the vendor directory created after running the godep initialization. This directory contains the libraries being used by the application and are needed by godep when compiling on Heroku.

Now that the code changes are done, let’s commit them and push them to the GitHub repository. Post that, let us go ahead and deploy it.

Manual deployment on Heroku

If the correct branch is configured and automatic deployments are enabled, this step can be skipped.

Post successful deployment, we should be able to access our application through a client app or Postman. Let’s test it by generating a tiny URL and getting the long URL for the same.

Generate tiny URL for hpmahesh.me
Get long URL from the tiny URL

It works!! Now let’s make sure the entries are being made in database and cache. We can do that by accessing the database and cache services remotely using the client provided by Heroku CLI.

Entry on Redis cache connected to the app
Entry in remote Postgres DB connected to the app

The instructions for setting them up and using them can be found here:

I hope you got some insights on how to deploy a Go application to Heroku. Please share your interesting stories about your journey with GoLang and please do leave your comments about the article!

--

--

Full time bug creator | Hobby Photographer | Occasional Blogger | Love ☕ | Opinions here are my own | https://hpmahesh.me