Get Started with CloudCaptain & Go

 

This tutorial will get you started with CloudCaptain and Go. It should take you about 5-10 minutes to complete.

Prerequisites

Before you begin, ensure you have an x64 machine and have successfully:

  1. created a CloudCaptain Account (simply log in with your GitHub account, it's free)
  2. downloaded and installed the latest CloudCaptain Client
  3. downloaded and installed the latest version of Go
  4. downloaded and installed the latest version of VirtualBox

Creating the Go application

In this tutorial we are going to create and deploy a simple Go application.

Start by creating a directory for our app:

> mkdir getstarted-go

And navigate to it:

> cd getstarted-go

Now create a file called main.go with the following contents:

package main

import (
    "fmt"
    "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi there, I love CloudCaptain!")
}

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

Now let's make sure we are compiling for Linux x64. If you are on Linux already, you can safely skip this step.

Windows users should invoke:

getstarted-go> set GOOS=linux

And Mac OSX users should invoke:

getstarted-go> export GOOS=linux

Now go ahead and compile the app into a single executable:

getstarted-go> go build -ldflags="-s"

Fusing a CloudCaptain image and running it locally on VirtualBox

Now it's time to fuse your application into a CloudCaptain image and launch an instance of it on VirtualBox:

getstarted-go> boxfuse run -ports.http=8080 -image=getstarted-go:1.0

Creating getstarted-go ...
Mapping getstartedgo-dev-myuser.boxfuse.io to 127.0.0.1 ...
Created App getstarted-go (single-instance / none)
Fusing Image for getstarted-go (ELF64) ...
Image fused in 00:00.517s (7170 K) -> myuser/getstarted-go:1.0
Launching Instance of myuser/getstarted-go:1.0 on VirtualBox ...
Forwarding http port localhost:8080 -> vb-f6dba4e5:8080
Instance launched in 00:03.055s -> vb-f6dba4e5
Waiting for Payload to start on vb-f6dba4e5:8080 (expecting HTTP 200 at / within 300s) ...
Payload started in 00:00.009s -> https://127.0.0.1:8080

In just a few seconds CloudCaptain found your application, detected its type, generated an image for it and launched an instance of that image on VirtualBox.

Now open your browser and navigate to this address to see your new application up and running within the VirtualBox VM:

You can also see your newly created image:

getstarted-go> boxfuse ls

Images available locally:
+--------------------------+---------------+-------+---------+--------------+--------+---------------------+
| Image                    |    Payload    | Debug | Runtime |    Ports     |  Size  |    Generated at     |
+--------------------------+---------------+-------+---------+--------------+--------+---------------------+
| myuser/getstarted-go:1.0 | getstarted-go | false | ELF64   | http -> 8080 | 7170 K | 2016-08-08 19:38:56 |
+--------------------------+---------------+-------+---------+--------------+--------+---------------------+
Total: 1

As well as the instance that is running:

getstarted-go> boxfuse ps

Running Instances on VirtualBox in the dev environment :
+-------------+--------------------------+---------------------+-----------------------+---------------------+
|  Instance   |          Image           |        Type         |          URL          |     Launched at     |
+-------------+--------------------------+---------------------+-----------------------+---------------------+
| vb-f6dba4e5 | myuser/getstarted-go:1.0 | 4 CPU / 1024 MB RAM | https://127.0.0.1:8080 | 2016-08-08 19:38:59 |
+-------------+--------------------------+---------------------+-----------------------+---------------------+
Total: 1

Deploying your application to AWS

Now let's deploy the image to AWS. As CloudCaptain works with your AWS account, it first needs the necessary permissions to do so. So if you haven't already done it, go to the CloudCaptain Console and connect your AWS account now.

Every new CloudCaptain account comes with 3 environments: dev, test and prod. dev is your local VirtualBox environment and test and prod are on AWS.

So let's deploy our application to the prod environment on AWS:

getstarted-go> boxfuse run getstarted-go:1.0 -env=prod

Pushing myuser/getstarted-go:1.0 ...
Verifying myuser/getstarted-go:1.0 ...
Waiting for AWS to create an AMI for myuser/getstarted-go:1.0 in eu-central-1 (this may take up to 50 seconds) ...
AMI created in 00:29.835s in eu-central-1 -> ami-9cf006f3
Creating Elastic IP ...
Mapping getstartedgo-myuser.boxfuse.io to 52.28.244.5 ...
Creating security group boxsg-myuser-prod-getstarted-go-1.0 ...
Launching t2.micro instance of myuser/getstarted-go:1.0 (ami-9cf006f3) in prod (eu-central-1) ...
Instance launched in 00:17.580s -> i-921e9a2f
Creating Cloud Watch Alarm for Instance auto-recovery -> i-921e9a2f-auto-recovery-alarm
Waiting for AWS to boot Instance i-921e9a2f and Payload to start at https://54.93.51.85:8080/ ...
Payload started in 00:28.022s -> https://54.93.51.85:8080/
Remapping Elastic IP 52.28.244.5 to i-921e9a2f ...
Waiting 15s for AWS to complete Elastic IP Zero Downtime transition ...
Deployment completed successfully. myuser/getstarted-go:1.0 is up and running at https://getstartedgo-myuser.boxfuse.io:8080/

Notice that we have now specified an image, as we want to reuse our image unchanged instead fusing a new one.

With that one command CloudCaptain has automatically pushed your image to the CloudCaptain Vault as well as provisioned, configured and secured all necessary AWS resources. There is no manual work necessary on your behalf.

All you need to do is simply navigate to your new domain to see your Go application in action on AWS:

Bonus: update your application using blue/green deployments

Now let's take things one step further and deploy an update of your application with zero downtime.

Start by modifying main.go with a simple change:

...
fmt.Fprintf(w, "Hi there, I love CloudCaptain!")
fmt.Fprintf(w, "Updated by CloudCaptain with zero downtime!")
...

Then rebuild the tgz:

getstarted-go> go build -ldflags="-s"

Finally, deploy the new version of your application to AWS:

getstarted-go> boxfuse run -ports.http=8080 -env=prod -image=getstarted-go:2.0

Fusing Image for getstarted-go (ELF64) ...
Image fused in 00:00.541s (7170 K) -> myuser/getstarted-go:2.0
Pushing myuser/getstarted-go:2.0 ...
Verifying myuser/getstarted-go:2.0 ...
Waiting for AWS to create an AMI for myuser/getstarted-go:2.0 in eu-central-1 (this may take up to 50 seconds) ...
AMI created in 00:14.649s in eu-central-1 -> ami-ba8b7dd5
Creating security group boxsg-myuser-prod-getstarted-go-2.0 ...
Launching t2.micro instance of myuser/getstarted-go:2.0 (ami-ba8b7dd5) in prod (eu-central-1) ...
Instance launched in 00:19.492s -> i-d91f9b64
Creating Cloud Watch Alarm for Instance auto-recovery -> i-d91f9b64-auto-recovery-alarm
Waiting for AWS to boot Instance i-d91f9b64 and Payload to start at https://52.59.244.104:8080/ ...
Payload started in 00:30.181s -> https://52.59.244.104:8080/
Remapping Elastic IP 52.28.244.5 to i-d91f9b64 ...
Waiting 15s for AWS to complete Elastic IP Zero Downtime transition ...
Destroying Cloud Watch Alarm i-921e9a2f-auto-recovery-alarm ...
Terminating instance i-921e9a2f ...
Destroying Security Group sg-dbc040b3 (boxsg-myuser-prod-getstarted-go-1.0) ...
Deployment completed successfully. myuser/getstarted-go:2.0 is up and running at https://getstartedgo-myuser.boxfuse.io:8080/

And there it is:

Summary

In this brief guide we have seen how to:

  • create a Go application
  • fuse it into a CloudCaptain image
  • deploy the image locally on VirtualBox
  • deploy the image unchanged to AWS
  • update the application with zero downtime

Now it's your turn. Take your favorite Go application and deploy it with ease and pleasure.

CloudCaptain Go Documentation