Automation and deployment of Go applications to staging and Production environments using, Docker, Terraform, AWS and Github Actions. (Part 2)

Boluwatife Fakorede
2 min readJan 1, 2021

You can find the link to the part 1 here.

In the first part of this article, we focused on creating GitHub actions for the build and lint of our Go application.

In this article, we will be containerising our application using docker and preparing it for deployment using Terraform as our tool for infrastructure as code to AWS.

Let’s create our docker file.

Dockerfile

Here we will be using a multi-build stage to significantly reduce our application size.

The stages are actually well commented but I will explain certain stages.

We are running golang:alphine as builder. Alpine Linux is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general. Hence we are starting with a really small image before building our Go project.

The command go get ./… helps to get the exact dependencies in the project going through each go file.

To build our Go project, we will run line 22.
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

This pretty much is a standard but let’s explain what it does.

The CGO_ENABLED=0 disables CGO since I am building on alpine to run on alpine and run on Linux withGOOS=linux. Disabling it removes the need to compile cross-platform dependencies. The -a flag does a force rebuild then we output to the main file meaning the main file will contain our executable program.

Multi-stage Build

Finally, we will generate clean go executable for the end user in step 31, 32 and 33 which involves copying the executable from main and the packages and the environmental variables to the alphine.

We have successfully containerise our application and it is ready for deployment.

Next we will be working with Terraform to spin up our AWS services needed.

Thanks for reading

--

--

Boluwatife Fakorede

A frontend developer, while at that, just a curious cat that loves to share what he knows.