Virendra Oswal's Blog

Virendra Oswal's Blog

Golang  deployment on Heroku

Golang deployment on Heroku

Deploy your golang web application on Heroku PaaS.

Subscribe to my newsletter and never miss my upcoming articles

In this blog post, we will deploy a simple golang HTTP service on Heroku which is one of the widely used PaaS.

Pre-requisities:

  • go 1.12+ (this blog post is done with 1.16, but shouldn't matter, Use go version to find the version locally)
  • Heroku Account. (Sign up if not done, no credit card or anything required during sign-up)
  • Heroku CLI. (Helps for better administration.)
  • Go HTTP Service (We will create one minimal one for demonstration)

Go HTTP Service Locally (Optional)

We will create a simple HTTP Service in golang using Go Fiber which is a web framework similar to Express in NodeJS for fast bootstrapping of the web app.

Let's get straight into this.

First, we initialize our Go module using the below command for managing dependency:

go mod init github.com/cycorax12/golang-heroku

Create an entry point file for gofiber initialization, we will name it main.go:

touch main.go

We will now install GoFiber using the below command:

go get github.com/gofiber/fiber/v2

We will run locally by running the below command to see if the application is working fine and we are able to hit the root endpoint:

go run main.go

We will add the below code to our main.go, does nothing but initializes the new Fiber application, and setup root(/) endpoint

package main

import (
    "fmt"
    "os"
    "github.com/gofiber/fiber/v2"
)

const PORT = "3000"

func main() {
    port := getEnv("PORT", PORT)
    app := fiber.New()

    app.Get("/", func(c *fiber.Ctx) error {
        return c.SendString("[golang] Hello from Heroku!")
    })

    app.Listen(fmt.Sprintf(":%s", port))

}

// Gets default value passed if no value exist for given environment variable.
func getEnv(key, fallback string) string {
    if value, ok := os.LookupEnv(key); ok {
        return value
    }
    return fallback
}

We are able to get HTTP Response for our service local_wworking.JPG

Heroku Procfile

The startup process for Heroku deployments is wired using Procfile, so we will create one adhering to start our golang web application in our root of the web application.

touch Procfile

WWE will add a web process and give the below command:

web: bin/golang-heroku
  • web: Specifies we will have inbound HTTP traffic via Heroku Routers.
  • bin/golang-heroku: golang-heroku is the name of the module we gave as part of initialization as shown in the steps above. Its binary file is created as a part of the go build command to output binary file into bin/

Heroku Deployment

We now have everything in place to Create the Heroku application and deploy it. Create New Application and give a meaningful name as per your application as below: create-new-app.JPG create-new-app-dialog.JPG

Once the application is created, follow the steps on Deploy using Heroku git as below, most of the time it's the same, the Application name changes that's it. heroku-deploy.JPG

Once git push heroku master is done, we will see deployment logs as below representing successful deployment on a local terminal. deployment_logs.JPG We can see URL created to access externally, in this case, it was golang-heroku-blog.herokuapp.com

Voila, we have completed our golang web app deployment to Heroku. This now can be shared with clients for accessing our service.

However, if you see our deployment logs above, in yellow even though we used 1.16 version of golang it used 1.12. Because if we don't pass any golang version explicitly, it selects the default version as per this.

Ideally, there is no impact, but sometimes we want our local and Heroku golang runtime to be the same. So Heroku provides configuration to override the golang version used during compilation and deployment.

For that, open your Heroku application created via heroku.com, then Click select Settings tab. Reveal Config Vars. And then add config variable GOVERSION as below. Override_go_version.JPG

If we re-trigger deployment, by pushing changes back to heroku, we see in logs as per below our golang version of 1.16 used this time. override_1.16_deployment.JPG

The application will have no impact, just want to cover how to override golang version used.

Resources:

Thank you for reading, If you have reached it so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback and suggestions!

I would love to connect with you at Twitter | LinkedIn.

 
Share this