Sitemap

Deploying a Go project on EC2

3 min readJun 9, 2014

I’ve been playing around with Go lately and spent a few hours figuring out how to deploy a project on Amazon EC2. I’m going to walk you through the steps I took to hopefully save you time if you’re a first time Go/EC2 user like myself.

Launch new EC2 instance

First thing you’ll need is an EC2 instance where you can put all your stuff:

  1. Go here
  2. Choose EC2 and click Launch Instance
  3. Select Ubuntu Server
  4. Micro instance should already be selected so just click Review and Launch
  5. Click Launch
  6. If you don’t have an existing key pair, choose Create new key pair. Give it a name and click Download Key Pair. This will give you a .pem.txt file which you should then move into your ~/.ssh folder and remove the .txt extension.
  7. Run chmod 400 your-key-pair.pem to make sure your private key isn’t publicly viewable.

8. (optional) To make life easier add the following to your .ssh/config file

https://gist.github.com/nathanborror/4657c5be9dab9d33eb23

This makes it so you can just type ssh e to login to your EC2 instance without having to type ssh -i your-key-pair.pem ubuntu@54.208.83.100. Also remember to replace the 54.208.83.100 with the public IP address of your EC2 instance.

Preparing the EC2 instance

Now lets install all the software we need to make our Go project work:

  1. Run sudo apt-get install build-essential golang git mercurial
  2. If you’re project is using anything else you’ll need to install it. I like to use sqlite so I also ran sudo apt-get install sqlite3.
  3. Set your GOPATH by opening your .profile file with your editor of choice. For simplicity just add export GOPATH=$HOME/go to the bottom of the file. Save the file and run source ~/.profile to pick up the changes.
  4. If your Go project is on Github all you’ll need to do is run go get github.com/your-username/your-project-name and Go will install everything in your ~/go directory.
  5. Navigate to your project folder cd ~/go/src/github.com/your-username/your-project-name and type go build to build your project. Then you can try running it by typing ./project-name. Now we need to hop back over to the Amazon console.

Editing security groups

By default Amazon does not allow incoming web traffic so we need to open up ports 80 (and 8080 to test our Go project). To do this we need to open up the EC2 Dashboard and click Instances in the left sidebar. Choose your instance and click the item (i.e. launch-wizard-*) right next to Security groups. This will take you to the Security Groups section.

Get Nathan Borror’s stories in your inbox

Join Medium for free to get updates from this writer.

Click the Inbound tab at the bottom and click the Edit button. Click Add Rule and choose HTTP from the menu on the left. Click Add Rule again and this time keep Custom TCP Rule and change Port Range to 8080. Click Save.

If you kept your project running from step 5 of the last section you should be able to navigate to port 8080 of your instance’s public IP address in your browser and see your project working.

We’re almost done. At this point if we logout of our shell the program will terminate. Using Upstart we can ensure our program is running at all times.

Running our program with Upstart

First lets create a new file in the /etc/init/ folder called project-name.conf. Open it in your favorite editor and add the following:

https://gist.github.com/nathanborror/89f5d10e5e6e3c1243a2

Now run sudo start your-project-name to get your program up and running. You should now be able to close your shell and still view the project in your browser from anywhere.

That’s it!

--

--

Nathan Borror
Nathan Borror

Written by Nathan Borror

Design Generalist living in San Francisco specializing in interaction design and software engineering.