How To Run A Go Web Server On AWS

Posted on

Amazon Web Services (AWS) offers free tier web servers where you can run simple go programs for API backends or websites. In this guide we will make a simple web server using a go program. This guide can also be used as a reference to set up AWS instances for larger go services.

To begin you will need to set up a new instance on AWS. To do this simply login to your AWS account and click on Services at the top of the screen and then on EC2. EC2 should be the first option under the Compute category.

Click on Instances on the left hand side and then in the top left there should be a large button called Launch Instance which will start a creation wizard. I typically search for Ubuntu and pick 18.04 as shown in the screenshot below.

Don’t change anything else and just click next on the right side of this instance. For the type we can choose the free tier t2.micro instance as we won’t be running anything resource heavy. If you are going to be running large applications you should probably select something larger. Next click on Next: Configure Instance Details, do not click on Launch Instance directly. On this page we can keep everything the same but we want to click next again and head to our storage options.

On the storage page I prefer to change it from 8GB to 30GB just so that we have a bit more room to play with in case we should need it. Click next two times as we can skip the Tags page, this is not needed. Now we should end up on the security group page. Here it is important to set SSH to only be open to your own IP so that your server is less likely to be attacked via the SSH port. If you are on a dynamic IP you may have to go into AWS and update this every time your IP changes and if that is the case you may need to allow a range of IPs. We also need to open port 80 and 443 for our web server. Your configuration should look like the following image.

Once that is done click Review and Launch. Review everything on the final page and click Launch. Now you may be presented with a popup asking about key pairs which looks like this.

This is a very important step as we will use this key pair to authenticate to the server via SSH. If you don’t have a key pair you need to generate a new set and download them. If you already have a key pair feel free to select the key pair you have. Once this is done we can click on Launch Instances and wait for the instance to launch.

Getting Go up and running on AWS

Now that we have an instance running we will first need to ssh into the instance. If you are on Mac OS or Linux you should be able to edit ~/.ssh/config with a configuration like this one.

    ForwardAgent yes
    User ubuntu
    IdentityFile ~/.ssh/awsgophp.pem

If you look at your instance page you should see a public IP listed there. This is what I am using for the host. I renamed my pem file which is my key pair and placed it under ~/.ssh/awsgophp.pem.

When running an instance for a production system you should go to Elastic IPs and assign a static IP for your instance. I also recommend assigning an IPv6 address.

Now we can test the ssh configuration by running ssh but replace my server ip with your ip. If you are on Windows I recommend looking at something like Putty or Git Bash.

I run into two issues when doing this the first time. The first problem is that the ECDSA fingerprint is not trusted. Generally, it is safe to say yes to this the first time but be cautious if you get prompted about this at any other time. The second issue is the permissions for my .pem file. You will need to run chmod 400 ~/.ssh/yourkey.pem in order to fix the permissions.

After that you should have full SSH access to your server. Please go ahead and install Go on this server. I have previously written a guide on how to install Go on Ubuntu 18 here.

Testing our server

Once that is set up and running go ahead and make the go directory inside your home directory. You will also need a project folder. I created my folder under ~/go/src/gophp/webserver. Inside this folder you can make a file called main.go and add the following code.

package main

import (

func main() {
        http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request) {
                fmt.Fprintf(w, "A simple web server by")

        http.ListenAndServe(":80", nil)

The above code is a simple web server which will listen to port 80 in go. When visiting the page it should print A simple web server by Inside the same directory as your main.go file you can now type go build. This should make an executable called webserver inside the same directory. We will need sudo permissions to bind to port 80 which is why we made a build. With that in mind you can now type sudo ./webserver and go to the public ip of your server. The text should now show in the browser when going to the ip as shown in the screenshot below.

To stop the web server simply hit ctrl + c inside your terminal.

That’s all there is to it! If you want to run a public project on your server you can clone it with git and build it. It is also possible to build locally for a linux operating system with go. Then you could simply upload your command to the server using scp or an SFTP client like FileZilla. I hope you found the guide useful, feel free to leave any comments and/or feedback in the comments below.

Got Something To Say?

Your email address will not be published. Required fields are marked *