How to Deploy a Load Balanced Go Microservice to AWS

Using ShuttleOps to Deploy a Load-Balanced Go Microservice

ShuttleOps is a new No Code CI/CD platform that allows you to easily deploy applications to three major cloud providers. This post will take you through an example of how to build and deploy a load-balanced golang microservice to AWS using ShuttleOps.  You may also view a video walkthrough by clicking here or at the end of this page.

Prerequisites

In order to follow along with this tutorial, you’ll need to have the following things installed and setup.

  1. Habitat – https://www.habitat.sh/docs/install-habitat
  2. A Habitat origin created on https://bldr.habitat.sh
  3. An AWS account
  4. A github/gitlab/bitbucket account with git installed
  5. Go – https://golang.org/doc/install
  6. Docker for Mac (only if you’re a MacOS user) – https://store.docker.com/editions/community/docker-ce-desktop-mac

Getting Started

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 main.go.

Test that everything worked by running the application and checking the result using curl.

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 scaffolding.

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 bldr.habitat.sh.

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.

Our 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!

ConnectHabitat

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.

RepoSelection

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.

ConnectAWS

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.

In the payload task, select the output of our Build pipeline.

BuildPipelineOutput

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.

DeployPipelineInstance

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.

DeployPipelineSecurityRules

Lastly, add the load balancer with a listener on port 80 that forwards traffic to our go microservice on port 8080.

DeployPipelineLoadBalancer

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.

ManagePageLoadBalancer

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.

 

Log In and Try ShuttleOps Free

Related Posts

3 Different Strategies for Managing...

Kubernetes is a great abstraction for managing deployments of containerized applications. The way Kubernetes achieves this abstraction is through...
Continue reading

We’ll See You at HashiConf Digital 2020...

It’s 2020. The world is different, conferences are digital, and HashiCorp is announcing not one, but two open source...
Continue reading

ShuttleOps Introduces Enhanced Visibility to...

It’s frustrating when critical information is not available at your fingertips. For DevOps teams, having the right information available...
Continue reading