Deployment manager: A server in GO!

Fermin Blanco
2 min readMay 27, 2020

Out there in the wild forest of the Internet, there are not much content about deploying a Go server to the cloud and neither using the deployment manager. The focus about this series is trying to keep things easy and fast to deploy it. Having said that, we are going to keep the whole configuration in just one file.

STOP THIS MADNESS AND SHOW ME THE CODE!

We continue to use us-central1-a as a zone for the examples.

There are approaches that attempt to use too many configuration files (.schema, .jinja, .yaml), although I recognize its sense of separations of concerns, I do not think it will the best for this article.

A startup script, running a go server in Google Cloud

To enable our handwritten server to run at boot time (when the compute instance starts) we need to define a set of instructions to tell compute engine what to run. From google docs

Startup scripts can perform many actions, such as installing software, performing updates, turning on services, and any other tasks defined in the script. You can use startup scripts to easily and programmatically customize your VM instances, including on new instances at creation time

Then let us do it, install software and turn on our server app. As a first step, let us create the folder where to host our server.

Folder for our app

And as second step, we should install golang but To be fairly enough we do not really need the go runtime to be installed. As an alternative approached we could had compile the server in our local environment and then submit it to a location where we can download it into our machine instance (we will do it in another article) but this require an extra step. This approach is one step faster since with just one command you can deploy the whole system (but this does not work well at scale)

Go compiles to a single binary for an operating system. This binary contains the Go runtime, all the imported packages, and the entire application

As we said (or typed it 🤔) we need to make some software installation, the environment for go. Currently no compute instance has a pre-installed version for golang. So we should install it, (this example uses the version 1.14.2)

Installing go (1.14.12)

if we trying to execute go build server.goit will generate no output since in order to use go build command, we need to set go cache env vars first.

set COCACHE env variable
Our little and sweet server

Provide the whole path for our go installation since we did not define GOPATH neither GOROOT env variables.

go build main.go

Since the main process exits when it finishes executing the script, command go run main.go would not work to preserve our server running. Let us try a way to accomplish this task. Systemd unit files gives a way to treat our server app as a service (unit) that the OS can manage for us.

Our service definition

Let us start our service and logs their state.

run server and print status

The command that is going to make everything works,

--

--