Setting Up a Go Web Server on Amazon EC2

Arish AhmadArish Ahmad
3 min read

Launching EC2 with Amazon Linux

  1. Open the AWS Management Console and navigate to the EC2.

  2. Click “Launch instances”.

  3. Enter an appropriate instance name.

  4. Select “Amazon Linux” under “Application and OS Images”.

  5. Select any “Free tier eligible” Instance type.

  6. Create and select any “ppk key” of the “RSA” type.

  7. Allow “SSH traffic”, “HTTPS traffic” and “HTTP traffic” under “Network settings”.

  8. Click “Launch instance”.

Convert pem Key to ppk Key

  1. Open “PuTTYgen”.

  2. Click “Load”.

  3. Now select the pem key you use instance.

  4. Click on “Conversions” → “Export OpenSSH key” → “Yes”.

  5. Enter an appropriate key name. (Eg. go-pem-key.ppm)

    Note: Make sure the save as type is “All Files” and to you use the “.ppm“ at the end of the key name as given in the example.

  6. Click “Save”.

Connecting to EC2 instance

  1. Copy this

     ssh -i "your-key.pem" ec2-user@<your-ec2-public-ip>
    
  2. Paste in the Command Prompt.

  3. Edit the pem key with the absolute path and the key name you just created. (Eg. If the absolute path is “C:\Users\username” and the key name is “go-pem-key.pem”. Then replace “you-key.pem” to “C:\Users\username\go-pem-key.pem”.

  4. Go back to AWS Management console, Select the newly created instance.

  5. Copy “Public IPv4 address”.

  6. Replace <your-ec2-public-ip> to the copied address.

  7. Press enter.

  8. Type “yes” then again press enter.

Uploading Go webserver to EC2 instance

Note: Before uploading the go server to the EC2 instance the go server must set to run on default IP and 8080 port.

  1. Copy and Paste the following one by one.

     sudo yum update -y
     sudo yum install -y golang
    
  2. You can use scp to transfer your main.go, .env, and any other required files from your local machine to the EC2 instance.

    Copy the following

     scp -i "your-key.pem" -r "/path/to/your/project" ec2-user@<your-ec2-public-ip>:/home/ec2-user/
    
  3. Paste this in another Command Prompt Terminal which is not connected to the EC2 instance.

  4. Replace the "your-key.pem" to the pem key you created with the absolute path.

  5. Replace the “/path/to/your/project” to the absolute path of your project (path to the root folder).

  6. Replace the <your-ec2-public-ip> to the instance’s “Public IPv4 address”.

  7. Hit Enter.

Build the application

  1. Copy and Paste

     cd /home/ec2-user/your-project-folder
    

    Replace you-project-folder to the name of the root folder.

  2. Hit enter.

  3. Build your Go application to ensure it compiles properly:

     go build -o app main.go
    

Run the application

Once the build is successful, run the application:

./app

Run Your Application in the Background

To keep your application running after logging out, use a process manager like screen or tmux, or run the app as a background process.

nohup ./app &

Configure Security Group for HTTP Access

  1. Select the EC2 instance.

  2. Under “Security” click on the “Security groups”.

  3. Scroll and click on “Inbound rules”.

  4. Click on “Edit inbound rules”.

  5. Click “Add Rules”.

  6. Select “Custom TCP” under “Type”, set “8080” in “Port range” and “Anywhere-IPv4” under “Destination”.

  7. Click “Save rules”.

  8. Now Click on “Outbound Rules”.

  9. Click “Edit outbound rules”.

  10. Click “Add Rules”.

  11. Select “Custom TCP” under “Type”, set “8080” in “Port range” and “Anywhere-IPv4” under “Destination”.

  12. Click “Save rules”.

Test the Web Server

Use the curl command with the public IP and port.

For example curl http://<Public-IP>:8080/path_to _service.

Replace <Public-IP> with the instance’s Public IPv4 address and path-to-service with the path where any GET request is sent.

0
Subscribe to my newsletter

Read articles from Arish Ahmad directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arish Ahmad
Arish Ahmad

Final-year BTech student specializing in software development. Completed a Flutter internship and amassed three years of project experience, crafting Threads clone, Stream Arbiter, Ticketify, and Google Docs. Proficient in C++, having successfully solved 250+ LeetCode questions.