This guide provides step-by-step instructions to deploy the Expense Prediction Flask application on AWS EC2.
Quick Start: Use the provided deployment scripts (deploy.sh for Linux/Mac or deploy.ps1 for Windows) to automatically transfer files while excluding unnecessary folders like venv.
- AWS Account
- EC2 instance running (Ubuntu/Amazon Linux recommended)
- SSH access to your EC2 instance
- Windows: use WinSCP or PowerShell, Linux/Mac: built-in
- Log in to AWS Console
- Navigate to EC2 Dashboard
- Click "Launch Instance"
- Configure instance:
- Name:
expense-prediction-app - AMI: Ubuntu Server 22.04 LTS or Amazon Linux 2023
- Instance type: t2.micro (free tier) or t2.small
- Key pair: Create new or select existing (download .pem file)
- Network settings: Allow SSH and HTTP traffic
- Name:
- Launch instance and note the Public IP address
- Go to EC2 Dashboard → Security Groups
- Select your instance's security group
- Edit Inbound Rules:
- Ensure: SSH, Port: 22, Source: 0.0.0.0/0 (or your IP for testing)
- Ensure: Custom TCP, Port: 5000, Source: 0.0.0.0/0 (or your IP for testing)
- Save rules
ssh -i "path\to\your-key.pem" ubuntu@<EC2-PUBLIC-IP>chmod 400 your-key.pem
ssh -i your-key.pem ubuntu@<EC2-PUBLIC-IP>Replace <EC2-PUBLIC-IP> with your instance's public IP address.
Once connected to EC2, run:
sudo apt-get update
sudo apt-get install -y python3 python3-pip python3-venv
sudo apt install python3.12-venvThe deployment scripts automatically exclude venv, __pycache__, and other unnecessary files.
On Windows (PowerShell):
.\deploy.ps1 -KeyFile "path\to\your-key.pem" ubuntu@<EC2-PUBLIC-IP>On Linux/Mac:
chmod +x deploy.sh
./deploy.sh your-key.pem ubuntu@<EC2-PUBLIC-IP>Note: Replace <EC2-PUBLIC-IP> with your EC2 instance's public IP address.
SSH back into your EC2 instance and run:
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt
cd ~/expense-prediction
source venv/bin/activate
python3 app.pyThe application will start on port 5000. You should see output like:
* Running on http://0.0.0.0:5000
Open your web browser and navigate to:
http://<EC2-PUBLIC-IP>:5000
Replace <EC2-PUBLIC-IP> with your EC2 instance's public IP address.
Once you are done you MUST STOP INSTANCE otherwise you will get billed, go to Instance list from "instances" -> Check your instance -> From "instance state" dropdown above STOP AND TERMINATE your instance