Deploying Golang and React Applications on AWS EC2

vinayvinay
4 min read

Understanding the Deployment Landscape

Deploying a web application is like constructing a complex building. Just as architects carefully choose locations, materials, and design, we'll methodically select and configure our cloud infrastructure. Amazon EC2 (Elastic Compute Cloud) serves as our digital construction site, providing a robust, flexible environment to host our applications.

The Foundation: Selecting Your AWS EC2 Instance

Choosing the Right Operating System

image

Imagine selecting the perfect foundation for a house. In cloud computing, this foundation is your operating system. We'll choose Ubuntu Server 22.04 LTS (Long-Term Support) - a stable, secure, and developer-friendly Linux distribution. This version offers:

  • Regular security updates

  • Extensive community support

  • Compatibility with modern development tools

  • Long-term stability for production environments

Instance Type: Matching Power to Purpose

image

Selecting an EC2 instance is like choosing a vehicle for a specific journey. For development and small-scale applications, the t2.micro instance is ideal:

  • Free-tier eligible

  • 1 vCPU

  • 1 GB memory

  • Perfect for testing and lightweight applications

For production environments requiring more computational power, consider:

  • t2.small: Increased memory and processing capability

  • t2.medium: Enhanced performance for more demanding applications

  • Higher-tier instances for resource-intensive workloads

Security: Your Digital Fortress

Crafting Robust Security Groups

Security groups act as your application's digital gatekeepers. We'll configure precise, minimal access rules:

  • SSH (Port 22): Secure remote server access

  • HTTP (Port 80): Web traffic

  • HTTPS (Port 443): Encrypted web communications

  • Custom ports for specific application needs

Key Pair: Your Secure Access Mechanism

image

The key pair is like a sophisticated, cryptographic key to your server:

  • Generates a public-private key pair

  • Private key (.pem file) stored securely on your local machine

  • Enables secure, password-less SSH authentication

Deployment Workflow: From Local to Cloud

Golang Backend Deployment

Your Golang backend represents the application's core logic - its brain and nervous system. Deployment involves:

  • Installing Golang runtime

  • Configuring environment variables

  • Creating a persistent background service

  • Ensuring automatic startup and recovery

Frontend React Application

The frontend is your application's face - its visual and interactive element. Deployment requires:

  • Node.js and npm installation

  • Building production-ready assets

  • Creating a persistent service for continuous operation

Detailed Technical Implementation

1. Initial Server Connection

# Set secure permissions for key pair
chmod 400 your-key-pair.pem

# Connect to EC2 instance
ssh -i "your-key-pair.pem" ubuntu@your-instance-public-dns

2. Golang Installation

# Update system packages
sudo apt update

# Install dependencies
sudo apt install -y wget curl

# Download and install Go
wget https://golang.org/dl/go1.21.5.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.21.5.linux-amd64.tar.gz

# Configure environment path
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
source ~/.bashrc

# Verify installation
go version

3. Backend Repository Setup

# Configure Git credentials
git config --global credential.helper cache
git clone https://github.com/yourusername/backend-repo.git
cd backend-repo

# Create environment configuration
touch .env
nano .env  # Edit with your specific configurations

4. Backend Service Configuration

# Create systemd service file
sudo nano /etc/systemd/system/backend.service

# Add service configuration
[Unit]
Description=Golang Backend Service
After=network.target

[Service]
Type=simple
Restart=always
WorkingDirectory=/path/to/backend
ExecStart=/usr/local/go/bin/go run main.go
User=ubuntu

[Install]
WantedBy=multi-user.target

# Manage backend service
sudo systemctl daemon-reload
sudo systemctl enable backend.service
sudo systemctl start backend.service

5. Frontend Deployment

# Install Node.js
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs npm

# Clone frontend repository
git clone https://github.com/yourusername/frontend-repo.git
cd frontend-repo

# Install dependencies and build
npm install
npm run build

# Create frontend service
sudo nano /etc/systemd/system/frontend.service
# (Similar configuration to backend service)

# Manage frontend service
sudo systemctl daemon-reload
sudo systemctl enable frontend.service
sudo systemctl start frontend.service

Ongoing Management and Monitoring

Service Lifecycle Commands

# Check service status
sudo systemctl status backend.service

# View service logs
journalctl -u backend.service

# Restart services
sudo systemctl restart frontend.service

Best Practices and Considerations

  1. Regularly update system packages

  2. Implement comprehensive logging

  3. Use environment-specific configurations

  4. Secure your instances with minimal, necessary access

  5. Regularly backup critical data and configurations

Conclusion

Deploying web applications is an art and science of careful configuration, security, and strategic resource allocation. Each step transforms your local development into a robust, scalable cloud application.

0
Subscribe to my newsletter

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

Written by

vinay
vinay