Deploying a Go application on AWS EC2

In this tutorial, I will show you how to deploy Golang app to Amazon EC2. You will need an AWS account. You can signing up for free.

1 — Setup EC2 instance

Once you created you account, open the AWS Management Console and click on EC2:

Then, click on “Launch Instance” :

Choose an AMI (Amazon Machine Image). We are going to choose “Amazon Linux AMI 2017.03.1 5 (HVM), SSD Volume Type” . The exact versions may change with time.

Note : There are other Linux AMIs that you can use, which have Go and other software pre-installed, which you may use as a shortcut later. But for our purposes, if you start with a clean installation and configure everything yourself, you’ll be able to better understand how to administer and maintain your instances in future.

Next, we will choose the hardware configuration for our instance. So we will go with the lowest specs and cheapest configuration “t2.micro” which is enough for this tutorial:

Then click on “Configure Instance Details”. We left everything as default then click on “Add Storage

Next, click on “Add Tags” to assigne a name to our instance:

Click on “Configure Security Group“. Then expose the port number your server will be listening on. For this demo, I have made an application which is listening on port 3000, Therefore, I enabled HTTP port 3000 and I set the source column to ‘Anywhere’ .

The settings above allows access to port 3000 from anywhere, and SSH access only from anywhere.

Now, click on “Review And Launch” then “Launch“.

When prompted for a key pair, you can either choose an existing key pair or create a new key pair. Save the private key file in a safe place. You’ll need to connect by SSH to your instance later.

When you’re ready, select “Launch Instances”:

Your instance is now launching and that may take a few minutes. Click the View your instances on the Instances page link and you will be redirected to your “My Instances” page, where you can monitor and configure your EC2 instance:

Wait until the instance state turns to “running” then use the public DNS to connect to your service via SSH.

2 — Connect to AWS EC2 via SSH

Now that we have connected to the instance, it’s time to install Golang.

3 — Install Golang on EC2

Once you’re connected to the instance, use yum configuration manager to install Go:

sudo yum update -y
sudo yum install -y golang

Now you need to set up Go environment variables for your project. Commonly you need to set 3 environment variables as GOROOT, GOPATH and PATH

# GOROOT is the location where Go package is installed on your system
export GOROOT=/usr/lib/golang
# GOPATH is the location of your work directory
export GOPATH=$HOME/projects
# PATH in order to access go binary system wide
export PATH=$PATH:$GOROOT/bin

Note: all above commands will set the environment variables for your current session only. To make it permanent add above commands in ‘~/.bash_profile’ file

At this step you’ve successfully installed and configured go language on your system.

Congratulation ! you have a working EC2 instance with Go.

4 — The Go Application

Note: You can skip this step if you have your own Go App ready to be deployed.

Put the following content in a file named “app.go” :

package main
import (
func HomeEndpoint(w http.ResponseWriter, r *http.Request) {
 fmt.Fprintln(w, “Hello world :)”)
func main() {
 http.HandleFunc(“/”, HomeEndpoint)
 if err := http.ListenAndServe(“:3000”, nil); err != nil {

Run the app using the following commands:

go run app.go

Finally, if you point your favorite web browser (not you IE) at your instance’s public DNS name you should see :

Congratulation ! you have deployed your first Go App to AWS EC2 instance.

Note: In case your app is listening on different port than 3000, you can always add it to the security group assigned to your EC2 instance. Just go to EC2 menu, then click on “Security groups

Click on “Edit” and follow the same steps described in first section of this tutorial.

5 — Clean Up your instance

After you’ve finished with the instance that you created for this tutorial, you should clean up by terminating the instance.

Show your support

Clapping shows how much you appreciated Mohamed Labouardy’s story.