Deploying Go App On AWS EC2 Server

In Chapter-10 of our Golang Tutorial, we touched upon ‘Go with GORM’. In this chapter, let’s explore ‘Deploying Go App on AWS EC2 Server’.

So everybody knows what is Cloud Computing? What is AWS i.e.Amazon Web Services? So basically Cloud Computing is nothing but its like to hire someone to host our application on the internet. They will provide the infrastructure on the basis of the amount paid. It is like a stay in the house by paying rent.

So Google, Amazon, Microsoft are the biggest giants in the cloud computing sector. They provide large infrastructure and services to run your web stuff.

Amazon Web Services (AWS) offers cloud computing infrastructure by leading giant Amazon Inc. that provides on-demand computing platform. These AWS are offered in almost every region in the world. It includes Amazon Elastic Compute Cloud (EC2).

AWS has more than 70+ services including compute, storage, networking, database, analytics, app services, deployment, management, mobile, developer tools and tools for IoT.

Installation Of Linux And Go

Here, I have installed Ubuntu 16.04 LTS. Go here After installing Ubuntu, open terminal by pressing Ctrl+T and enter a command

$sudo apt-get update
$sudo apt-get -y upgrade

Then, download the latest version of Go by

$sudo curl -o https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz

Next use tar to unpack the package. This tool will open and extract downloaded file, and create a folder using package name and then move it to /usr/local

$sudo tar -xvf go1.8.3.linux-amd64.tar.gz
$sudo mv go /usr/local

You can also install this Go in another folder. Here now, Go package is in /usr/local folder and it is now in $PATH for Linux.

Now, set your Go paths needed for Go project. So to set the Go paths you need to set it into .profile file. It is like setting environment variables in windows.

First, open this .profile file by entering below command

$sudo nano ~/.profile

At the end of this file add the below lines

export GOROOT=/usr/local/go [Installation path]
export GOPATH=$HOME/GoDemo [Workspace path]
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH [bin directory of both]

$sudo nano ~/.profile

After this check whether Go setup is done completely or not by entering a command

$go version

If it is showing the version of the installed Go library then it means your setup is ready to develop go applications.

Create / Launch EC2 Instance

We need to create an AWS EC2 (Elastic Compute Cloud) instance in order to host our application on the AWS cloud and we need to pay for that. You can purchase it from their official website.

Once we have instance we can create Amazon Machine Instance in AMI(Amazon Machine Image)

  • Sign in to your AWS Amazon account. Go here
  • Choose EC2
  • Launch an Instance

Prebuilt machines will show Choose an Amazon Machine Image if already you used.

Choose AMI

Choose the machine – for ex. Ubuntu Server 26.4 LTS(HVM)

Choose Instance Type

Choose “t2.micro” . Just Google the pricing of t2.micro. It is intended to provide high performance required by your workload

Configure Instance Details

  • All the details like No. of Instances, Network, Subnet, Auto-assign public IP, IAM role Advanced details UserData
  • We can put some script file to run only when your machine launched/boot

Add Tags

Key value pair kp-28-08-2017

Security Group

Assign security group, who can be able to access this machine, Here I’ve taken ssh access.

SSH
HTTP

  • Review Launch
  • Launch
  • Select existing key pair/create new one
  • Download

Will create PEM file (Public Private Key Encryption which is asymmetric encryption) and download it. So your complete instance will be created.

Note: I’ve never created the EC2 instance, so you can check more from other sources. I’ve used my organization’s ec2 instance.

Are You Looking For Golang Development Services?

Deploy Your Binary

Now, here is the process of deployment of Go application on the ec2 instance using a Linux environment.

1. Public DNS Or Ec2 IP (Any Of This)
2. Pem File

Now, we’ll look into the deployment process.

Download Pem File And Move It To .Ssh Folder

Download the .pem file from ec2 instance and move it to .ssh folder

$ mv ~/Downloads/go.pem ~/.ssh/

If .ssh folder is not present in your system then you need to create in manually and then move it. Here, I downloaded .pem file and moved it to .ssh folder

$mkdir .ssh

Now, set the permission on .pem file which is just moved to ssh folder

Build Your Binary

As we have already installed Go on the system. Here, create one go application in your workspace.

I’ve created go application in my workspace at below path

GoDemo/src/Mindbowsers/MBIS/main/main.go

Now we’ll build a binary.

So in order to build the binary using Linux environment, you need to set OS variables like GOOS=linux and GOARCH=amd64.

GOOS→ Operating System GOARCH→ Architecture

You need to set it by going to the main folder of a project where your main.go resides

$go env

This command will show you all environment related to Go.

GOOS=operating system GOARCH=architecture

So, now run below command to build your binary.

$GOOS=linux GOARCH=amd64 go build -o GoDemoProject

“GoDemoProject” is the name I’ve given. You can give any name.

After entering the command, just go and check to the main location if their binary got created.

Copy Binary To Server

In order to copy your binary to the server, you need to use the scp i.e. secure copy command.

The specifications of the command are as given below.

$scp(secure copy) -i ~/.ssh/go.pem(PEM file path) GoDemoProject(binary name) ec2-user@publicDNS:

So, the command will be like this for me,

$scp -i ~/.ssh/go.pem GoDemoProject ec2-user@13.57.33.139:

Your application will be uploading to ec2 server. ec2-user might be Ubuntu depending on your machine

SSH Into Your Server

The specifications are like this

$ssh(Secure Shell) -i ~/.ssh/go.pem(pem file location) ec2-user@publicDNS:

you will be remotely connected to aws

ec2-user@ip-172-31-1-173

Run Your Application

Before running your application, give the rwx (Read Write Execute) permissions to the application. Check your binary has executed permission by entering command

ec2-user@ip-172-31-1-173:$ls -la

Change the permission

ec2-user@ip-172-31-1-173:sudo chmod 700 GoDemoProject

Now here run your application by specifying a name

ec2-user@ip-172-31-1-173:$sudo ./GoDemoProject

Exit

For disconnecting with the remote server just press exit command. You will be disconnected from the remote server.

Keep Reading

  • Service
  • Career
  • Let's create something together!

  • We’re looking for the best. Are you in?