An open API service indexing awesome lists of open source software.

https://github.com/harcastic/two-tier-flask-app

Fully automated CI/CD pipeline for a two-tier Flask + MySQL app using Jenkins, Docker, and AWS EC2. Code pushes trigger automated builds, tests, and deployments via Docker Compose, delivering a production-style, containerized web application.
https://github.com/harcastic/two-tier-flask-app

ci-cd cloud docker docker-compose ec2-instance flask-application jenkins mysql-database

Last synced: 3 months ago
JSON representation

Fully automated CI/CD pipeline for a two-tier Flask + MySQL app using Jenkins, Docker, and AWS EC2. Code pushes trigger automated builds, tests, and deployments via Docker Compose, delivering a production-style, containerized web application.

Awesome Lists containing this project

README

          

# Two-Tier Flask Application with CI/CD using Jenkins, Docker & AWS EC2

This project demonstrates a production-style CI/CD pipeline for a two-tier web application built using Flask (App Layer) and MySQL (Database Layer). The entire deployment is automated using Jenkins, Docker, Docker Compose, and GitHub Webhooks, running on AWS EC2.

## ๐Ÿ—๏ธ Architecture Overview

```
GitHub โ†’ Jenkins โ†’ Docker Build โ†’ EC2 SSH โ†’ Docker Compose โ†’ Flask App + MySQL
```

### Two-Tier Setup

- **Tier 1 โ€“ Flask App (Python backend)**
- **Tier 2 โ€“ MySQL Database (Persistent storage)**

Both run in separate containers managed by docker-compose.

### CI/CD Flow

1. Developer pushes code to GitHub
2. GitHub Webhook triggers Jenkins
3. Jenkins pulls repository
4. Jenkins builds Docker images
5. Jenkins runs unit tests
6. Jenkins SSHs into EC2
7. Docker Compose deploys updated containers
8. Updated Flask app goes LIVE instantly

## โš™๏ธ Tech Stack

| Component | Technology |
|-----------|-----------|
| App | Python Flask |
| Database | MySQL 8.0 |
| CI/CD | Jenkins |
| SCM | GitHub |
| Containerization | Docker & Docker Compose |
| Cloud | AWS EC2 (Ubuntu) |
| Trigger | GitHub Webhook |

## ๐Ÿ“‚ Project Structure

```
.
โ”œโ”€โ”€ app/
โ”‚ โ”œโ”€โ”€ app.py # Flask application
โ”‚ โ”œโ”€โ”€ requirements.txt # Python dependencies
โ”‚ โ””โ”€โ”€ Dockerfile # Docker image for Flask app
โ”‚
โ”œโ”€โ”€ tests/
โ”‚ โ””โ”€โ”€ test_basic.py # Unit tests
โ”œโ”€โ”€ docker-compose.yml # Multi-container orchestration
โ”œโ”€โ”€ Jenkinsfile # CI/CD pipeline configuration
โ””โ”€โ”€ README.md # This file
```

### Key Files

**Dockerfile**
- Builds the Flask application container
- Manages Python dependencies and application setup

**docker-compose.yml**
- Runs two containers:
- `web`: Flask application (exposed on port 80)
- `db`: MySQL database with persistent volumes & environment variables

**Jenkinsfile**
- Automates the complete CI/CD pipeline:
- Clone code from GitHub
- Build Docker images
- Run unit tests
- Deploy containers to EC2

## ๐Ÿš€ Deployment Instructions

### 1๏ธโƒฃ Set Up EC2 (App Server)

Install Docker and Docker Compose:

```bash
sudo apt update
sudo apt install docker.io docker-compose -y
sudo usermod -aG docker ubuntu
```

Clone the repository (first time only):

```bash
git clone https://github.com/harcastic/two-tier-flask-app.git
cd two-tier-flask-app
```

### 2๏ธโƒฃ Set Up Jenkins EC2 Server

Install Java and Jenkins:

```bash
sudo apt update
sudo apt install fontconfig openjdk-17-jre -y
wget -O jenkins.war https://get.jenkins.io/war-stable/latest/jenkins.war
```

Install Jenkins Plugins:
- Git plugin
- SSH Pipeline Steps
- Docker plugins
- Credentials Binding

Add Credentials in Jenkins:
- GitHub Personal Access Token
- EC2 SSH Private Key (for `app-ec2-ssh`)

### 3๏ธโƒฃ Configure Jenkins Pipeline

Create a new Pipeline job and use the following configuration:

```groovy
pipeline {
agent any

stages {
stage('Clone Code') {
steps {
git branch: 'main',
url: 'https://github.com/harcastic/two-tier-flask-app.git'
}
}

stage('Build Docker Images') {
steps {
sh 'docker-compose build'
}
}

stage('Run Unit Tests') {
steps {
sh 'docker-compose run web pytest || true'
}
}

stage('Deploy to EC2') {
steps {
sshagent(['app-ec2-ssh']) {
sh 'ssh -o StrictHostKeyChecking=no ubuntu@ "cd two-tier-flask-app && git pull && docker-compose down && docker-compose up -d"'
}
}
}
}
}
```

**Replace `` with your actual EC2 instance public IP**

### 4๏ธโƒฃ Set Up GitHub Webhook

Configure GitHub to trigger Jenkins automatically:

1. Go to your repository โ†’ **Settings** โ†’ **Webhooks** โ†’ **Add Webhook**
2. Set **Payload URL**: `http://:8080/github-webhook/`
3. Set **Content Type**: `application/json`
4. Select events: โœ” **Push events**
5. Click **Add webhook**

**Replace `` with your Jenkins EC2 instance public IP**

## ๐Ÿงช Verification

### Check Running Containers

```bash
docker ps
```

Expected output should show:
- `web` - Flask application container
- `db` - MySQL database container

### Access Flask Application

Open your browser and navigate to:

```
http://
```

You should see the Flask application running with the message "New Updates are released!!"

### Check Container Logs

```bash
# View all logs
docker-compose logs

# View specific service logs
docker-compose logs web
docker-compose logs db
```

### Test Health Endpoint

```bash
curl http:///health
```

Should return: `OK`

## โœ… Features

- โœ” End-to-end CI/CD automation
- โœ” Two-tier containerized application
- โœ” Production-style deployment workflow
- โœ” GitHub โ†’ Jenkins Webhook integration
- โœ” Docker Compose-based multi-container architecture
- โœ” Real cloud deployment on AWS EC2
- โœ” Automated unit testing in pipeline
- โœ” Zero-downtime deployments

## ๐Ÿ“˜ Future Improvements

- [ ] Add comprehensive unit testing coverage
- [ ] Implement Blue-Green deployment strategy
- [ ] Add monitoring with Prometheus/Grafana
- [ ] Migrate to Kubernetes for orchestration
- [ ] Implement automated rollback on test failures
- [ ] Add database migration scripts
- [ ] Implement secrets management using AWS Secrets Manager
- [ ] Add container image scanning for security vulnerabilities
- [ ] Setup CloudWatch for centralized logging
- [ ] Implement auto-scaling policies

## ๐Ÿ”ง Environment Variables

Configure the following in your `docker-compose.yml` or environment:

```yaml
MYSQL_ROOT_PASSWORD: root123
MYSQL_DATABASE: flaskdb
```

## ๐Ÿ“ Notes

- The Flask app runs on port 5000 internally and is exposed on port 80
- MySQL data is persisted using Docker volumes (`db_data`)
- Jenkins must have SSH access to the App EC2 instance
- GitHub Webhook requires Jenkins to be accessible from the internet
- All deployments are containerized for consistency across environments

## ๐Ÿ™Œ Acknowledgements

This project demonstrates real-world DevOps practices and helped in learning:

- Jenkins Pipeline automation
- Docker build and containerization
- GitHub Webhooks for event-driven CI/CD
- Multi-tier application deployment strategies
- Infrastructure as Code with Docker Compose
- Cloud deployment on AWS EC2
- End-to-end automation workflows

## ๐Ÿ“š Additional Resources

- [Flask Documentation](https://flask.palletsprojects.com/)
- [Docker Documentation](https://docs.docker.com/)
- [Jenkins Pipeline Documentation](https://www.jenkins.io/doc/book/pipeline/)
- [GitHub Webhooks Documentation](https://docs.github.com/en/developers/webhooks-and-events/webhooks)
- [AWS EC2 Documentation](https://docs.aws.amazon.com/ec2/)

## ๐Ÿ“„ License

This project is open source and available under the MIT License.

---

**Last Updated**: November 2025