Fly.io Deployment¶
Deploy Django Keel projects globally with Fly.io's edge network.
Overview¶
Fly.io runs your application close to users worldwide with:
- Global edge deployment - Deploy to 30+ regions
- Automatic HTTPS - Free SSL certificates
- PostgreSQL included - Managed Postgres clusters
- Redis included - Managed Redis
- Free tier - Generous free allowance
- Near-instant deploys - Deploy in seconds
Prerequisites¶
- Fly.io account (free tier available)
- Fly CLI installed
deployment_targets: [flyio]selected during generation
Installation¶
Install Fly CLI¶
# macOS
brew install flyctl
# Linux
curl -L https://fly.io/install.sh | sh
# Windows
powershell -Command "iwr https://fly.io/install.ps1 -useb | iex"
Authenticate¶
Quick Start¶
1. Launch Application¶
From your project directory:
Fly.io will:
1. Detect your Django app
2. Generate fly.toml configuration
3. Prompt for app name and region
4. Create PostgreSQL database (optional)
5. Create Redis instance (optional)
2. Configure fly.toml¶
Generated fly.toml:
app = "your-app-name"
primary_region = "sjc" # San Jose
[build]
builder = "paketobuildpacks/builder:base"
[env]
PORT = "8000"
DJANGO_SETTINGS_MODULE = "config.settings.prod"
[[services]]
internal_port = 8000
protocol = "tcp"
[[services.ports]]
port = 80
handlers = ["http"]
[[services.ports]]
port = 443
handlers = ["tls", "http"]
[[services.http_checks]]
interval = "10s"
timeout = "2s"
grace_period = "5s"
method = "GET"
path = "/health/"
3. Set Secrets¶
# Required
fly secrets set DJANGO_SECRET_KEY=$(python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())')
fly secrets set DEBUG=False
fly secrets set ALLOWED_HOSTS=your-app.fly.dev
# If using AWS S3
fly secrets set AWS_ACCESS_KEY_ID=your-key
fly secrets set AWS_SECRET_ACCESS_KEY=your-secret
fly secrets set AWS_STORAGE_BUCKET_NAME=your-bucket
# If using Sentry
fly secrets set SENTRY_DSN=your-sentry-dsn
# If using Stripe
fly secrets set STRIPE_PUBLIC_KEY=your-public-key
fly secrets set STRIPE_SECRET_KEY=your-secret-key
4. Deploy¶
Your app will be live at https://your-app.fly.dev
Database Setup¶
Create PostgreSQL¶
Options:
- Name: your-app-db
- Region: Same as your app
- Configuration: Development (1GB RAM, 10GB disk)
Attach Database¶
This sets the DATABASE_URL secret automatically.
Create Redis¶
Options: - Plan: Free (256MB) - Region: Same as your app
Attach Redis¶
This sets the REDIS_URL secret automatically.
Multi-Region Deployment¶
Deploy to multiple regions for low latency worldwide:
# Add regions
fly regions add ams # Amsterdam
fly regions add gru # São Paulo
fly regions add syd # Sydney
# Scale to 2 machines per region
fly scale count 2
Fly.io automatically routes users to the nearest region.
Adding Background Workers¶
Celery Worker¶
Create fly.worker.toml:
app = "your-app-worker"
[build]
builder = "paketobuildpacks/builder:base"
[deploy]
release_command = "python manage.py migrate"
[[services]]
internal_port = 8080
protocol = "tcp"
Add Procfile for worker:
Deploy worker:
Celery Beat¶
Similar setup for scheduled tasks:
Volumes for Persistent Storage¶
If not using S3:
# Create volume
fly volumes create data --size 10 # 10GB
# Update fly.toml
[[mounts]]
source = "data"
destination = "/data"
Update Django settings:
Custom Domains¶
Add Domain¶
Configure DNS¶
Add records to your DNS provider:
A @ 66.241.124.123 # Fly.io IP (check dashboard)
AAAA @ 2a09:8280:1::1 # IPv6
CNAME www your-app.fly.dev
Fly.io automatically provisions SSL certificates.
Monitoring¶
View Logs¶
Metrics¶
Monitoring Dashboard¶
Access at: https://fly.io/apps/your-app/monitoring
- Request rate
- Response time
- Error rate
- CPU/Memory usage
Scaling¶
Vertical Scaling¶
# List available VM sizes
fly platform vm-sizes
# Scale to shared-cpu-2x (2 CPU, 4GB RAM)
fly scale vm shared-cpu-2x
VM Sizes: - shared-cpu-1x: 1 CPU, 256MB RAM (free tier) - shared-cpu-2x: 2 CPU, 4GB RAM - dedicated-cpu-1x: 1 vCPU, 2GB RAM - dedicated-cpu-2x: 2 vCPU, 4GB RAM
Horizontal Scaling¶
# Scale to 3 instances
fly scale count 3
# Scale per region
fly scale count 2 --region sjc
fly scale count 2 --region ams
Auto-scaling¶
# fly.toml
[[services]]
internal_port = 8000
[[services.autoscaling]]
min_instances = 2
max_instances = 10
Health Checks¶
Fly.io uses your /health/ endpoint:
[[services.http_checks]]
interval = "10s"
timeout = "2s"
grace_period = "5s"
method = "GET"
path = "/health/"
Django Keel provides this endpoint by default.
Zero-Downtime Deployments¶
Fly.io handles this automatically:
- Deploys new version
- Waits for health checks to pass
- Routes traffic to new version
- Terminates old version
Troubleshooting¶
Build Fails¶
Error: "No Dockerfile found"
Error: "requirements.txt not found"
Database Connection¶
Error: "could not connect to database"
# Check DATABASE_URL is set
fly secrets list | grep DATABASE
# Test connection
fly ssh console
python manage.py dbshell
Memory Issues¶
Error: "OOMKilled"
SSL Certificate Issues¶
Error: "certificate verification failed"
# Check certificate status
fly certs show yourdomain.com
# Recreate certificate
fly certs remove yourdomain.com
fly certs create yourdomain.com
Cost Optimization¶
Free Tier Limits¶
- 3 shared-cpu-1x VMs (256MB RAM each)
- 3GB PostgreSQL storage
- 160GB outbound transfer
- Free HTTPS certificates
Tips¶
- Use shared-cpu for dev/staging
- Auto-scale down during low traffic
- Optimize images - Reduce Docker image size
- Use volumes instead of S3 for small files
- Monitor usage - Set billing alerts
Production Checklist¶
- [ ] Set
DEBUG=False - [ ] Configure
ALLOWED_HOSTSwith your domain - [ ] Set strong
DJANGO_SECRET_KEY - [ ] Enable Sentry for error tracking
- [ ] Set up database backups
- [ ] Configure custom domain with SSL
- [ ] Test Celery workers (if using)
- [ ] Configure auto-scaling
- [ ] Set up monitoring alerts
- [ ] Enable database replication for critical apps
Comparison with Other Platforms¶
| Feature | Fly.io | Render | Heroku |
|---|---|---|---|
| Regions | 30+ | 7 | 2 (US/EU) |
| Free Tier | 3 VMs, 3GB DB | 750 hrs/month | No free tier |
| Auto-scaling | ✅ Yes | ✅ Yes | ✅ Yes |
| SSL | ✅ Free | ✅ Free | ✅ Free |
| Pricing | $1.94+/month | $7+/month | $5-$25+/month |
Best Practices¶
- Deploy to multiple regions - Low latency worldwide
- Use volumes for persistent data (if not using S3)
- Monitor health checks - Ensure endpoints respond
- Enable auto-scaling - Handle traffic spikes
- Regular backups - Snapshot PostgreSQL regularly
- Use Fly CLI - Faster than web dashboard
- Keep fly.toml in version control
Further Reading¶
Support¶
- Community Forum: community.fly.io
- Status: status.fly.io