🚀 Deployment Patterns

Understanding how to get your application to production


What Is Deployment?

Deployment = Making your application available to end users on the internet.

Your Computer (Local)
    ↓
Testing & Development
    ↓
Version Control (Git)
    ↓
Deployment Platform (DigitalOcean, AWS, Heroku, etc.)
    ↓
The Internet
    ↓
End Users

Deployment Checklist

Before deploying, make sure:

  • [ ] Application works locally without errors
  • [ ] All required dependencies are in requirements.txt
  • [ ] Environment variables are configured
  • [ ] Database can initialize on new system
  • [ ] Code is committed to git
  • [ ] No hardcoded secrets or passwords
  • [ ] Static files are properly configured
  • [ ] Error logging is set up

Common Deployment Steps

1. Prepare Code for Production

# Make sure all changes are committed
git status
git add .
git commit -m "Prepare for deployment"

What this does: Ensures git has your latest code.


2. Test Locally with Production Settings

# Set production environment
export FLASK_ENV=production
export FLASK_DEBUG=0

# Run locally to test
python run_v2.py

What this does: Tests that your app works in production mode before actually deploying.


3. Configure Deployment Platform

Different platforms require different steps:

Example: DigitalOcean

name: my-app
services:
  - name: web
    run_command: python run_v2.py
    environment_slug: python
    instance_count: 1

Example: Heroku

Create Procfile:
web: python run_v2.py

Example: AWS

Use Elastic Beanstalk or EC2 with configuration files

4. Set Environment Variables on Deployment Platform

On your platform's dashboard/console, set:

FLASK_ENV=production
SECRET_KEY=[generate-a-strong-key]
DATABASE_URL=[database-connection-string]

What these do: - FLASK_ENV=production → Run in production mode - SECRET_KEY → Secure random key for sessions - DATABASE_URL → Where data is stored in production


5. Push Code to Deployment Platform

git push [deployment-remote] [branch]

What this does: Sends your code to the platform, which builds and runs it.


6. Verify Deployment

# Check health endpoint
curl https://your-app.example.com/health

# Or visit in browser
https://your-app.example.com

What you're checking: Is the app running? Can you access it?


Key Concepts

Development vs. Production

Setting Development Production
Error Visibility Show full error details Hide errors from users
Debug Mode On (shows variables, etc.) Off (for security)
Auto-reload On (restart on code change) Off (manual restart only)
Logging Console output File or service logs
Database Local SQLite file Production database
Performance Less optimized Heavily optimized

Environment Variables

Settings that change between environments:

Local Development:
  FLASK_ENV=development
  DATABASE_PATH=./app.db
  SECRET_KEY=any-key-for-testing

Production:
  FLASK_ENV=production
  DATABASE_URL=postgresql://user:pass@host:5432/db
  SECRET_KEY=[strong-random-key]

Why different? - Local: convenience, debugging - Production: security, performance, reliability


Secrets & Security

Never put secrets in code:

❌ WRONG (in code):
SECRET_KEY = "my-secret-key-12345"

✅ RIGHT (in environment variable):
export SECRET_KEY="my-secret-key-12345"

Deployment Platforms

DigitalOcean App Platform (Recommended for simplicity) - Click button to deploy from git - Auto-builds and starts your app - Good for small projects - Cost: ~$5-12/month

Heroku (Easy for beginners) - Connect git repo - Auto-deploys on push - Free tier available (limited) - Cost: Free or $7+/month

AWS (Most flexible but complex) - Most control - More setup required - Scalable to any size - Cost: $0-100+/month depending on usage

DigitalOcean Droplet (VPS, more control) - Full server access - You manage everything - More complexity - Cost: $4-24+/month


Deployment Process

Typical Flow

1. Code changes locally
        ↓
2. Test locally (FLASK_ENV=development)
        ↓
3. Commit to git
        ↓
4. Test with production settings locally
        ↓
5. Push to git (or directly to platform)
        ↓
6. Platform builds your app
        ↓
7. Platform starts your app
        ↓
8. Visit your live URL
        ↓
9. Test production version
        ↓
10. If problems, fix locally and push again

What Happens During Deployment

Build Phase

Platform receives code
    ↓
Install Python
    ↓
Install dependencies (requirements.txt)
    ↓
Run any setup scripts
    ↓
Ready to start

Run Phase

Start application
    ↓
Application listens for requests
    ↓
Users visit your URL
    ↓
Requests handled
    ↓
Responses sent back

Failure Phase (If something goes wrong)

Error occurs during build
    ↓
Logs show error message
    ↓
You read logs
    ↓
Fix the issue locally
    ↓
Push again

Monitoring & Maintenance

Check Application Status

# Visit health check endpoint
curl https://your-app.example.com/health

# Or check platform dashboard
# Look for: Running, No errors, Response time < 1s

Read Logs

# Check what's happening in production
doctl apps logs [app-id] --tail
# or
heroku logs --tail

What to look for: - Errors (ERROR, Exception) - Warnings (WARN) - Startup messages - Request logs

Common Issues

Issue Cause Fix
App won't start Missing dependency Add to requirements.txt
500 errors Code bug Check logs, fix, push
Slow response Too much traffic Upgrade instance
Database errors Connection failed Check DATABASE_URL
Static files missing Wrong path Check file locations

Deployment Commands (By Platform)

DigitalOcean App Platform

# Create and deploy (through UI usually)
# Or push to connected git repo
git push origin main

Heroku

# Deploy
git push heroku main

# View logs
heroku logs --tail

# Restart
heroku restart

AWS Elastic Beanstalk

# Deploy
eb deploy

# View status
eb status

# View logs
eb logs

Security Checklist for Deployment

  • [ ] Secret keys are in environment variables (not in code)
  • [ ] Database passwords are secure
  • [ ] No debug mode in production
  • [ ] HTTPS enabled (SSL certificate)
  • [ ] Logging captures important events
  • [ ] Error messages don't expose system details
  • [ ] Database backups are automated
  • [ ] Rate limiting is configured (prevents abuse)

Next Steps

  • Learn about your specific platform: [Check Platform Docs]
  • Set up monitoring: Set up logs on your platform
  • Plan backups: Automated daily backups recommended

Key Takeaway: Deployment is moving your application from your computer to a public server so anyone can use it. Test locally first, then deploy when ready.