DEV Community

Cover image for Deploying SpringBoot Application on AWS EC2: A Comprehensive Guide
Suraj
Suraj

Posted on

Deploying SpringBoot Application on AWS EC2: A Comprehensive Guide

Introduction

This guide outlines the process of deploying a SpringBoot application on AWS EC2 with GitHub Actions for automated deployment. The setup includes Docker containers and proper security configurations.

Prerequisites

  • AWS Account
  • GitHub Repository
  • Basic knowledge of Docker and AWS services
  • SpringBoot application ready for deployment

NOTE: While working on this project I have made the repository private, so remember

  • If your repo is private, then you must configure SSH or PAT on EC2 to interact with it.
  • If your repo is public, no key setup is needed to clone it.

But for your reference if you want the application I’m making it public

Here is the Repository

https://github.com/Suraj-kumar00/scm-springboot-application-devops

First let’s understand the architecture Diagram

image

Architecture Overview

  1. User accesses the app via a web browser using the HTTP.
  2. The request hits Nginx running on the EC2 instance, which listens on port 80.
  3. Nginx acts as a reverse proxy and forwards the request to the Spring Boot application running inside a Docker container on port 8081.
  4. The Spring Boot app processes the request and, if needed, communicates with the MySQL database (also running in a Docker container on the same EC2 instance).
  5. The response is sent back through the same path:

    MySQL → Spring Boot → Nginx → User's browser.

  6. GitHub Actions is used to automatically deploy updates to the EC2 instance by SSHing in and running the necessary Docker commands (e.g., docker-compose up).

Step 1: Launch EC2 Instance

Begin by setting up your EC2 instance with these specifications:

  • Choose Ubuntu as the operating system
  • Select t2.medium instance type
  • Create and download a new key pair for SSH access
  • Configure security group with the following ports:
  • TCP 22 (SSH)
  • TCP 80 (HTTP for Nginx)
  • TCP 443 (HTTPS)
  • TCP 8081 (Spring Boot via Docker)
  • TCP 3000 (phpMyAdmin)
  • Set EBS volume size (20GB recommended for free tier)

Step 2: Configure Elastic IP

  1. Navigate to Elastic IP section in AWS Console
  2. Allocate new Elastic IP address
  3. Associate it with your EC2 instance
  4. Note down the Elastic IP for future use

Step 3: SSH Into EC2 Instance

ssh -i "your-key.pem" ubuntu@your-elastic-ip
Enter fullscreen mode Exit fullscreen mode

Step 4: Install Docker and Docker Compose

Create and execute this installation script:

#!/bin/bash

# Install Docker
sudo apt update
sudo apt install docker.io -y
sudo systemctl enable docker
sudo usermod -aG docker $USER

# Install Docker Compose
sudo curl -L "<https://github.com/docker/compose/releases/latest/download/docker-compose-$>(uname -s)-$(uname -m)" \\
  -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
Enter fullscreen mode Exit fullscreen mode

Step 5: Set Up SSH for GitHub

  1. Generate SSH key on EC2:
ssh-keygen -t rsa -b 4096 -C "ec2-rsa-key"
Enter fullscreen mode Exit fullscreen mode
  1. Add the public key to GitHub:
  2. Copy the content of ~/.ssh/id_rsa.pub
  3. Add it to GitHub under Settings > SSH and GPG Keys

Step 6: Clone and Deploy Application

git clone [email protected]:your-username/your-repo.git
cd your-repo
docker-compose up --build -d
Enter fullscreen mode Exit fullscreen mode

Step 7: Configure Nginx as Reverse Proxy

Install and configure Nginx:

sudo apt install nginx -y
sudo nano /etc/nginx/sites-available/default
Enter fullscreen mode Exit fullscreen mode

Add this configuration:

server {
    listen 80;
    server_name your-elastic-ip;

    location / {
        proxy_pass <http://localhost:8081>;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
Enter fullscreen mode Exit fullscreen mode

Restart Nginx:

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Step 8: Set Up GitHub Actions

Create .github/workflows/deploy.yml in your repository:

name: Deploy to EC2

on:
  push:
    branches:
      - main
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Setup SSH Key
        run: |
          echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > key.pem
          chmod 600 key.pem

      - name: Deploy via SSH
        run: |
          ssh -o StrictHostKeyChecking=no -i key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
            cd /home/ubuntu/your-repo
            git pull origin main
            docker-compose down
            docker-compose build --no-cache
            docker-compose up -d
          EOF
Enter fullscreen mode Exit fullscreen mode

Step 9: Configure Auto-restart on Reboot

Set up a cron job:

crontab -e
# Add this line:
@reboot cd /home/ubuntu/your-repo && docker-compose up --build -d
Enter fullscreen mode Exit fullscreen mode

Result!!

image

Common Challenges and Solutions

  • Permission Issues
    • Docker permission denied: Run sudo usermod -aG docker $USER
    • SSH key issues: Verify proper key permissions (chmod 600)
  • Networking Issues
    • 502 Bad Gateway: Check if Spring Boot container is running
    • Connection refused: Verify security group settings

Best Practices

  • Always use environment variables for sensitive data
  • Regularly backup your application data
  • Monitor application logs and performance
  • Keep Docker images updated with security patches
  • Use proper version tagging for Docker images

Thanks for reading - see you in the next one!

Top comments (0)