Using ShuttleOps to Deploy a Load-Balanced Go Microservice
In order to follow along with this tutorial, you’ll need to have the following things installed and setup.
- Habitat – https://www.habitat.sh/docs/install-habitat
- A Habitat origin created on
- An AWS account
- A github/gitlab/bitbucket account with
- Go – https://golang.org/doc/install
- Docker for Mac (only if you’re a MacOS user) – https://store.docker.com/editions/community/docker-ce-desktop-mac
If you’d like to skip building a go app, you can fork my repo instead: https://github.com/gscho/simple-go-app
First, we’ll start by creating a new go application called
simple-go-app in our
$GOPATH. This app will be a
gin web service that returns the hostname of the server. Since I’m going to store my source code on GitHub, I’m going to use a subdirectory that matches my VCS.
Next we will create our web service by updating the contents of
Test that everything worked by running the application and checking the result using
Building a Habitat Package
Now that we have a working go app, we will create a Habitat package that will easily allow us to version, deploy and manage our app within ShuttleOps! Luckily Chef Habitat makes this easy with
From within the root of the source code directory, run the following.
The contents of your plan file should look like this. Change your
pkg_origin to the one you have setup on
And that’s the entire plan file! Now we just need to define a run hook so Habitat knows how to run our application. Since we’ve added
pkg_bin_dirs=(bin) to our plan file, the binary our go app produces will be available on the path of our package already. That means our run hook will look like this.
hooks/run looks like this.
Adding our code to git
At this point, we have everything we need to deploy a go application, so let’s add our code to our VCS (if you haven’t already).
Building our application using ShuttleOps
To build our application, head over to ShuttleOps and login with your VCS provider.
Once logged in we will add our Habitat Builder token to the ShuttleOps vault via the
Connect page so that ShuttleOps can upload and promote our packages. Make sure you have also created an origin on Habitat Builder so we can upload your package!
Let’s create a build pipeline to build and upload our code. From the
Build page, we will create a new pipeline. The default
Build pipeline has a
code task already added for us so open it up and select the repo you want to build.
And that’s it! Just click
launch to build your go app.
Deploying to AWS using ShuttleOps
To deploy our application to AWS, we’ll head back to the
Connect page and add some AWS credentials to the ShuttleOps vault. For your convenience, there is a
launch stack button that will create a Shuttleops IAM user in your account with the minimum permissions required.
Finally, let’s create a
Deploy pipeline to deploy our app to AWS. A deploy pipeline will have 2 tasks already added for you, a
payload task where you can select the application to deploy and a
destination task that will define the deployment destination, in our case AWS.
payload task, select the output of our
Next, we’ll update the destination task to tell ShuttleOps how to deploy our application to AWS. We will deploy 3 instances on t3.nano instances.
Since we don’t want anyone accessing our instance except through the load balancer, let’s remove all the security rules from the
Security Rules tab.
Lastly, add the load balancer with a listener on port 80 that forwards traffic to our go microservice on port 8080.
Save and click the
launch button to deploy your app!
Viewing our app running in AWS
After the application has finished deploying, head over to the
Manage page to find the link to your load balancer.
When you visit the load balancer endpoint, refresh the page a few times to see that the requests are being served by multiple servers.
And that’s it! With ShuttleOps, its easy to deploy a load-balanced golang microservice to AWS. Take a look at the video demonstration of this below, or login and try ShuttleOps free today.