Select Page

In this post I cover how to learn AWS by deploying a Go web app. Overview: Here’s what I’m doing in this post. We are going to setup a new AWS instance in a few easy steps that will be able to host a Go web server. Calling this web server will return a basic response, only a few lines of Go are required.

In general, to launch an EC2 instance there are 7 steps:

  • Choose AMI (Amazon Machine Image)
  • Choose Instance Type
  • Configure Instance
  • Add Storage
  • Add Tags
  • Configure Security Group
  • Review
Choose “Launch a Virtual Machine”.

Step 1: Choose an AMI

With an AWS account setup, the next step is to setup billing. Choose Basic Plan of the Free Tier. Choose Launch a virtual machine With EC2. Opening the link pulls up the EC2 Dashboard.The AMI (Amazon Machine Image) is the operating system of the virtual instance you are about to stand up. There are actually prebuilt images to choose from provided by the community. For this tutorial, you will choose a Linux AMI. Also the AMI will be “Free Tier Eligible”, because EHG is cheap. So, to explain further, this option based on these needs is going to be the Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type. The description tells us that this image comes with some tools installed.

  • AWS command line tools
  • Python, Ruby, Perl, and Java
  • PostgreSQL and others

Step 2: Choose Instance Type

Step number 2 is to pick an “Instance Type”, in other words the hardware or the instance. Each option is slightly different based on needs. This includes customization of virtual CPUs, how much memory is wanted, and the type of network performance desired. Amazon aims to please right? Choose t2.micro as it’s Free tier eligible.

Step 3: Configure Instance Details

Step 4: Add Storage

Leave the defaults.

Step 5: Add Tags

Keep the defaults for this step as they are.

Step 6: Configure Security Group

The default Rule setting is to include the SSH rule. Add two rules. HTTP and HTTPS. The Port Range field will automatically fill with the ports associated with each protocol.

Warning! There is immediately an advisory from AWS explaining that the Anywhere and Custom choices for the Source column will allow ANY IP address to access your instance via HTTP, HTTPS, and SSH. Let’s not risk it as this is just for training purposes and choose a new option. Choose now the My Ip option. AWS will instantly find your public IP address, because it’s AWS. If you feel like double checking AWS, go to ipchicken. The site will display your current public IP address.

Step 7: Review

The final step to launch the AMI is to review your settings. Then hit Launch. But wait! There is a prompt now to select an existing key pair. In other words there must be a public key that AWS keeps and a private key stored on your machine. The private key you keep locally is in the .pem format. This is a requirement to access your new instance. Key Review: Still confused on digital certificates? Give this post a read. Choose create a new pair and give the pair a useful name, such as My_First_AWS_Go_Web_App.pem and proceed by clicking on Download new key pair. Store the file somewhere safe, probably not in Downloads. Now with the new keys created, click Launch Instances to get started!

Pro Tip: At this stage you should be prompted to activate billing alerts. Turn on the Receive Free Tier Usage Alerts to get notified when you are about to exceed the free tier usage limit of your instance.

Part II: Use Your New Instance

Step 1: Connect

The instance is running and the meter is running so let’s get started. Let’s connect to the instance. Upon clicking on the new running instance choose Connect. There are two options to connect, a standalone SSH client or a Java SSH client directly from the browser. No-one wants that, so let’s choose standalone SSH client.

AWS provides instructions on what to do next to connect to the new instance. For *nix systems the default user for the AWS Linux AMI is ec2-user, therefore you have to use this to connect to your instance. The format for ssh is ssh {username}@domain.

Note: If you get lost you can always find your back by going to AWS > EC2.

# check your permissions for the .pem file
ls -al | grep "pem"
-rw-r--r--@ ... My_First_AWS.pem

# change the permissions to lock down the .pem file
chmod 400 My_First_AWS.pem

# see the changes
ls -al | grep "pem"
-r--------@ ... My_First_AWS.pem

# connect using this command (*nix systems only)
ssh -i "My_First_AWS.pem" [email protected] 

If you have a successful connection to your instance your shell prompt should read something like this: [ec2-user@{ip-address}]$. Get familiar with your new environment. In other words, get a feel of what the Linux architecture is, see what tools are installed, and check permissions of files and directories. For example:

# check the Linux distro
[ec2]$ uname -a

# check the IP address 
[ec2]$ ip a

# see if python is installed
[ec2]$ which python
/usr/bin/python

Step 2: Install Go

Now that we can access the new instance, how is new software installed? Well, according to the AWS documentation, “Amazon Linux instances manage their software using the yum package manager.” Our instance uses the yum package manager, therefore this is the one we will use to install Go.

# Must be root user 
[ec2]$ sudo su

# Grab Go 
[ec2]$ yum install -y go

# Check where Go is
[ec2]$ which go
/usr/bin/go

Step 3: Code Your Web App

A Go web app could be anything that serves a web page, therefore for this tutorial we will code a basic app. In order to make web requests to our instance’s app you will use the curl utility native to *nix systems.

package main

import(
"log"
"net/http"
"fmt"
)

// Take params http.ResponseWriter and pointer to http.Request
func rootURLHandler(wr http.ResponseWriter, r *http.Request){
   fmt.Fprintf(wr, "I handle %s/n!", r.URL.Path[1:])
}

func main(){
   // map custom handler to root URL pattern
   http.HandleFunc("/", rootURLHandler)
   log.Fatal(http.ListenAndServe(":80", nil))
}

Step 4: Deploy App to AWS & Launch!

This is the next and final step, to deploy the new code to the instance. After the transfer is completed, I make a web request to the new web server. The special URL illustrates how the server works. “Pineapples” is the circumstantial web node I am accessing in this example.

# secure copy go web server file to instance
:~ ehg$ scp -I My_First_AWS/pem server.go [email protected]:/home/ec2-user/webapp/server.go

# run the go webserver
:~ ehg$ go run server.go

# make a web request!
:~ ehg$ curl -X GET http://ec...us-north-2.compute.amazonaws.com:80/pineapples
I handle pineapples!
error: