Overview
This guide walks through deploying the fraud detection system to Heroku, a cloud platform that simplifies application deployment and scaling.Prerequisites
- Heroku account (sign up)
- Heroku CLI installed (installation guide)
- Git repository initialized
- Application code ready for deployment
Procfile Configuration
Understanding the Procfile
TheProcfile tells Heroku how to run your application:
web: Process type for HTTP trafficgunicorn: WSGI HTTP servermain:app: Points to theappobject inmain.py
Process Types
Theweb process type:
- Receives HTTP traffic
- Must bind to the PORT environment variable
- Scales horizontally with dynos
Custom Gunicorn Configuration
For advanced configuration, modify the Procfile:--workers 4: Run 4 worker processes--timeout 120: Request timeout in seconds--bind 0.0.0.0:$PORT: Bind to Heroku’s PORT
Runtime Requirements
Python Dependencies
Therequirements.txt file specifies all Python dependencies:
gunicorn==20.0.4: Production WSGI serverFlask==1.1.1: Web frameworkFlask-MonitoringDashboard==3.0.6: Performance monitoring
Python Version (Optional)
Create aruntime.txt to specify Python version:
Deployment Steps
1. Login to Heroku
2. Create Heroku Application
3. Verify Git Remote
Check that Heroku remote was added:4. Deploy to Heroku
Push your code to Heroku:master:
5. Scale the Web Dyno
Ensure at least one web dyno is running:6. Open Your Application
Environment Configuration
Setting Environment Variables
The application reads thePORT environment variable:
Viewing Configuration
Unsetting Variables
Database Configuration
SQLite Limitations on Heroku
Heroku’s ephemeral filesystem means:flask_monitoringdashboard.dbwill reset on dyno restart- Files are not persisted between deploys
Solutions
Option 1: Use Heroku Postgres Add PostgreSQL addon:- Amazon RDS
- Google Cloud SQL
- MongoDB Atlas
Monitoring and Logs
View Application Logs
--tail: Stream logs in real-time--num 200: Show last 200 lines--source app: Only app logs
Access Monitoring Dashboard
Navigate to:Heroku Metrics
View dyno metrics:Testing Endpoints
Test Home Endpoint
Test Prediction Endpoint
Test Training Endpoint
Troubleshooting
Application Crashes
Check logs:- Missing dependencies in
requirements.txt - Port binding issues (ensure using
$PORT) - Memory limits exceeded
Build Failures
Verify Procfile:web: gunicorn main:app
Check requirements.txt:
Timeout Errors
Increase Gunicorn timeout in Procfile:Memory Issues
Check dyno size:Slug Size Too Large
Optimize dependencies:- Remove unused packages from
requirements.txt - Use
.slugignoreto exclude unnecessary files
.slugignore:
Scaling Your Application
Horizontal Scaling
Add more web dynos:Vertical Scaling
Upgrade dyno type:free: 512 MB RAM, sleeps after 30 minhobby: 512 MB RAM, never sleepsstandard-1x: 512 MB RAMstandard-2x: 1 GB RAMperformance-m: 2.5 GB RAMperformance-l: 14 GB RAM
Continuous Deployment
GitHub Integration
- Go to Heroku Dashboard
- Select your app
- Navigate to “Deploy” tab
- Connect to GitHub repository
- Enable automatic deploys from main branch
Manual Deploy from Branch
develop branch to Heroku’s main.
Cost Optimization
Free tier limits:- 550-1000 dyno hours per month
- Apps sleep after 30 minutes of inactivity
- No custom domains with SSL
- Use hobby dyno ($7/month) for production
- Schedule worker dynos to avoid unnecessary costs
- Monitor usage with
heroku psandheroku logs
Security Best Practices
-
Environment variables: Never commit credentials
-
CORS configuration: Restrict origins in production
- HTTPS enforcement: Heroku provides SSL by default
-
Dashboard authentication: Secure monitoring dashboard
Next Steps
- Configure monitoring dashboard
- Production setup guide
- Set up CI/CD pipeline
- Configure custom domain
- Implement automated backups
- Add health check endpoints