Introduction

Deploying a Go application on AWS EC2 offers a robust, scalable solution for hosting your application with the flexibility of complete control over the server environment. In this guide, we’ll walk you through setting up an EC2 instance, installing the necessary software, deploying your Go application, and configuring Nginx as a reverse proxy to serve your app.

Getting Started with AWS EC2 for Hosting Go Apps

Before deploying your Go application, you must set up an AWS EC2 instance. AWS EC2 (Elastic Compute Cloud) provides resizable compute capacity in the cloud, making it an ideal choice for hosting web applications.

Steps to launch an EC2 instance:

  1. Log in to the AWS Management Console: Navigate to the EC2 Dashboard.
  2. Launch a New Instance: Click “Launch Instance” and choose an Amazon Machine Image (AMI). The Amazon Linux 2 AMI is famous for its compatibility and community support.
  3. Choose an Instance Type: Select a suitable instance type. A t2.micro instance (part of the AWS Free Tier) is sufficient for most Go applications.
  4. Configure Instance Details: Opt for the default settings, but ensure you configure security groups to allow HTTP and SSH traffic.
  5. Add Storage: The default 8 GB should suffice, but you can adjust it based on your application’s needs.
  6. Review and Launch: After reviewing all settings, launch the instance and download the key pair for SSH access.

Setting Up the Environment: Installing Go and Git on EC2

Once your EC2 instance is running, the next step is to install Go and Git, which is essential for running and managing your Go application.

Steps to install Go and Git:

  1. SSH into Your EC2 Instance:
    ssh -i /path/to/your-key.pem ec2-user@your-ec2-public-ip
  2. Update the Package Manager:
    sudo yum update -y
  3. Install Git:
    sudo yum install git -y
  4. Download and Install Go:
    wget https://go.dev/dl/go1.20.3.linux-amd64.tar.gz

sudo tar -C /usr/local -xzf go1.20.3.linux-amd64.tar.gz

  1. Configure Go Path:
    echo “export PATH=$PATH:/usr/local/go/bin” >> ~/.bash_profile

source ~/.bash_profile

  1. Verify Installation:
    go version

Cloning Your Go Project Repository onto EC2

With Go and Git installed, the next step is to clone your Go application from your Git repository onto the EC2 instance.

Steps to clone your project:

  1. Navigate to Your Desired Directory:
    cd ~
  2. Clone the Repository:
    git clone https://github.com/yourusername/your-go-repo.git

cd your-go-repo

Building and Running Your Go Application Locally on EC2

After cloning your repository, you must develop and run your Go application to ensure everything works correctly.

Steps to build and run your application:

  1. Build the Go Application:
    go build -o yourapp
  2. Run the Application:
    ./yourapp
  3. Test the Application: Open your EC2 instance’s public IP in a browser to ensure the application is running. If your app is set to run on port 8080, you can access it via http://your-ec2-public-ip:8080.

Creating a System Service for Persistent Operation

To ensure your Go application runs continuously and restarts automatically after a reboot, you should create a systemd service.

Steps to create a systemd service:

  1. Create a New Service File:
    sudo nano /etc/systemd/system/yourapp.service
  2. Add the Following Configuration:
    [Unit]

Description=Go Application

[Service]

ExecStart=/home/ec2-user/your-go-repo/yourapp

Restart=always

User=ec2-user

[Install]

WantedBy=multi-user.target

  1. Enable and Start the Service:
    sudo systemctl enable yourapp

sudo systemctl start yourapp

  1. Verify the Service Status:
    sudo systemctl status yourapp

Configuring Nginx as a Reverse Proxy for Your Go App

To serve your Go application over standard HTTP and HTTPS ports.

Steps to configure Nginx:

  1. Install Nginx:
    sudo yum install nginx -y
  2. Configure Nginx:
    sudo nano /etc/nginx/nginx.conf

Replace the default server block with the following:
server {

    listen 80;

    server_name your-ec2-public-ip;

    location / {

        proxy_pass http://127.0.0.1:8080;

        proxy_set_header Host $host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header X-Forwarded-Proto $scheme;

    }

}

  1. Start and Enable Nginx:
    sudo systemctl start nginx

sudo systemctl enable nginx

  1. Test Your Application: Open your EC2 instance’s public IP in a browser. Your Go application should now be accessible via http://your-ec2-public-ip.

Conclusion

Congratulations! You’ve successfully deployed your Go application on AWS EC2. Following this guide, you’ve learned how to set up an EC2 instance, install the necessary software, deploy your Go application, and configure Nginx as a reverse proxy.

References

Deploying Go applications with Elastic Beanstalk

QuickStart: Deploy a Go application to Elastic Beanstalk