Deploying SpringBoot Application on AWS EC2: A Comprehensive Guide
Deploying SpringBoot Application on EC2 Instance and GitHub Actions.

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

Architecture Overview
User accesses the app via a web browser using the HTTP.
The request hits Nginx running on the EC2 instance, which listens on port 80.
Nginx acts as a reverse proxy and forwards the request to the Spring Boot application running inside a Docker container on port 8081.
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).
The response is sent back through the same path:
MySQL → Spring Boot → Nginx → User's browser.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
Navigate to Elastic IP section in AWS Console
Allocate new Elastic IP address
Associate it with your EC2 instance
Note down the Elastic IP for future use
Step 3: SSH Into EC2 Instance
ssh -i "your-key.pem" ubuntu@your-elastic-ip
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
Step 5: Set Up SSH for GitHub
- Generate SSH key on EC2:
ssh-keygen -t rsa -b 4096 -C "ec2-rsa-key"
- Add the public key to GitHub:
Copy the content of ~/.ssh/id_rsa.pub
Add it to GitHub under Settings > SSH and GPG Keys
Step 6: Clone and Deploy Application
git clone git@github.com:your-username/your-repo.git
cd your-repo
docker-compose up --build -d
Step 7: Configure Nginx as Reverse Proxy
Install and configure Nginx:
sudo apt install nginx -y
sudo nano /etc/nginx/sites-available/default
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;
}
}
Restart Nginx:
sudo systemctl restart nginx
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
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
Result!!

Common Challenges and Solutions
Permission Issues
Docker permission denied: Run
sudo usermod -aG docker $USERSSH key issues: Verify proper key permissions (
chmod 600)
Networking Issues
502 Bad Gateway: Check if Spring Boot container is runningConnection 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






