Automating Web Application Deployment on AWS EC2 with GitHub Actions
- Sameer Natu

- Mar 17
- 3 min read
Updated: Jun 11
Introduction
Deploying web applications manually can be time-consuming and error-prone. Automating the deployment process ensures consistency, reduces downtime, and improves efficiency. In this blog, we will explore how to automate web application deployment on AWS EC2 using GitHub Actions.
By the end of this guide, you will have a fully automated CI/CD pipeline that pushes code from a GitHub repository to an AWS EC2 instance, ensuring smooth and reliable deployments.

Seamless Deployment Workflow
Prerequisites
Before we begin, ensure you have the following:
An AWS account
An EC2 instance with SSH access
A GitHub repository containing your web application
A domain name (optional)
Basic knowledge of AWS, Linux, and GitHub Actions
Step 1: Set Up Your EC2 Instance
Log in to your AWS account and navigate to the EC2 dashboard.
Launch a new EC2 instance with your preferred operating system (Ubuntu recommended).
Create a new security group and allow inbound SSH (port 22) and HTTP/HTTPS traffic (ports 80, 443).
Connect to your EC2 instance using SSH:
ssh -i /path/to/your-key.pem ubuntu@your-ec2-ip
Update the system and install necessary packages:
sudo apt update && sudo apt upgrade -y
sudo apt install -y git nginx dockerEnsure your application dependencies are installed.
Step 2: Configure SSH Access from GitHub Actions
To allow GitHub Actions to SSH into your EC2 instance and deploy the code:
Generate a new SSH key on your local machine:
ssh-keygen -t rsa -b 4096 -C "github-actions"Copy the public key to your EC2 instance:
cat ~/.ssh/id_rsa.pub | ssh ubuntu@your-ec2-ip 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'Store the private key as a GitHub Actions secret:
Go to your repository on GitHub.
Navigate to Settings > Secrets and variables > Actions.
Add a new secret named EC2_SSH_PRIVATE_KEY and paste the private key.
Also, add a secret named EC2_HOST with your EC2 public IP address.
Add a secret named EC2_USER with the value ubuntu (or your EC2 username).
Step 3: Clone the Repository on EC2
SSH into your EC2 instance:
ssh ubuntu@your-ec2-ipNavigate to the /var/www/html directory and clone your repository:
cd /var/www/html
git clone https://github.com/your-username/your-repo.git myappStep 4: Configure Docker (If Using Docker)
Navigate to the project directory:
cd myappCreate a docker-compose.yml file:
version: '3'
services:
app:
image: myapp:latest
build: .
ports:
- "80:80"Run the application using Docker:
docker-compose up -d --buildStep 5: Create a GitHub Actions Workflow
In your GitHub repository, create a new directory for workflows:
mkdir -p .github/workflowsCreate a new file named deploy.yml inside .github/workflows:
name: Deploy to AWS EC2
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Set up SSH
run: |
echo "${{ secrets.EC2_SSH_PRIVATE_KEY }}" > private_key.pem
chmod 600 private_key.pem
- name: Deploy to EC2
run: |
ssh -o StrictHostKeyChecking=no -i private_key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF'
cd /var/www/html/myapp
git pull origin main
docker-compose down
docker-compose up -d --build
exit
EOFStep 6: Test the CI/CD Pipeline
Push some changes to the main branch of your repository.
Navigate to Actions in your GitHub repository to see the workflow running.
After the deployment completes, visit your EC2 instance's public IP in a browser.
Step 7: Configure Nginx as a Reverse Proxy (Optional)
Install Nginx on your EC2 instance if not already installed:
sudo apt install nginx -yCreate a new Nginx configuration file:
sudo nano /etc/nginx/sites-available/myappAdd the following configuration:
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}Enable the configuration and restart Nginx:
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/ sudo systemctl restart nginxStep 8: Enable HTTPS with Let’s Encrypt (Optional)
Install Certbot:
sudo apt install certbot python3-certbot-nginx -yObtain an SSL certificate:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.comVerify SSL renewal:
sudo certbot renew --dry-runStep 9: Set Up Auto-Restart for Services
Ensure Docker services restart on reboot:
sudo systemctl enable dockerIf using a Node.js or Python application, use PM2 or Supervisor to keep it running.
Step 10: Implement Rollback Strategy
Keep older versions of your application in a backup directory.
In case of failure, manually switch to a previous version by checking out an older commit:
git checkout <commit-hash> docker-compose up -d --buildConclusion
By following this guide, you have successfully automated the deployment of your web application on AWS EC2 using GitHub Actions. This setup ensures that every time you push code to the main branch, your application gets automatically updated on the server.
For further improvements, consider:
Adding rollback strategies for failed deployments.
Implementing automated tests before deployment.
Using AWS CodeDeploy for more complex deployment workflows




Comments