This guide covers deploying the Sensor API to both Vercel and Azure.
Vercel is great for FastAPI applications and offers excellent performance with global CDN.
- Vercel account
- PostgreSQL database (Aiven, Supabase, or Vercel Postgres)
- GitHub repository
-
Prepare your database:
- Your existing Aiven PostgreSQL database will work perfectly
- Make sure your database allows external connections
- Note your connection string
-
Configure environment variables: Create these environment variables in Vercel dashboard:
DATABASE_URL=postgresql://username:password@host:port/database SECRET_KEY=your-secret-key-here ENVIRONMENT=production DEBUG=false -
Deploy to Vercel:
# Install Vercel CLI npm i -g vercel # Login to Vercel vercel login # Deploy vercel --prod
-
Set up domain (optional):
- Configure custom domain in Vercel dashboard
- Update CORS settings in main.py if needed
vercel.json- Vercel configurationrequirements-vercel.txt- Streamlined dependencies for Vercel
Azure offers more control and is excellent for enterprise applications.
- Azure account
- Azure CLI installed
- Docker installed
-
Containerize the application:
# Build Docker image docker build -t sensorapi . # Test locally docker run -p 8000:8000 --env-file .env sensorapi
-
Deploy to Azure Container Apps:
# Login to Azure az login # Create resource group az group create --name sensor-api-rg --location eastus # Create container app environment az containerapp env create \ --name sensor-api-env \ --resource-group sensor-api-rg \ --location eastus # Create container app az containerapp create \ --name sensor-api \ --resource-group sensor-api-rg \ --environment sensor-api-env \ --image sensorapi \ --target-port 8000 \ --ingress external \ --env-vars \ DATABASE_URL="your-database-url" \ SECRET_KEY="your-secret-key"
-
Create App Service:
# Create App Service plan az appservice plan create \ --name sensor-api-plan \ --resource-group sensor-api-rg \ --sku B1 \ --is-linux # Create web app az webapp create \ --resource-group sensor-api-rg \ --plan sensor-api-plan \ --name sensor-api-webapp \ --runtime "PYTHON|3.11" # Configure startup command az webapp config set \ --resource-group sensor-api-rg \ --name sensor-api-webapp \ --startup-file "python -m uvicorn app.main:app --host 0.0.0.0 --port 8000"
-
Deploy code:
# Deploy from local Git az webapp deployment source config-local-git \ --name sensor-api-webapp \ --resource-group sensor-api-rg # Push code git remote add azure <deployment-url> git push azure main
- ✅ Fastest deployment (2 minutes)
- ✅ Automatic HTTPS
- ✅ Global CDN
- ✅ Great developer experience
- ✅ Free tier available
- ✅ More control and customization
- ✅ Better for complex enterprise needs
- ✅ Integration with Azure services
- ✅ More deployment options
- ✅ Better monitoring and logging
-
Run database migrations:
# For Vercel (via Vercel CLI) vercel env pull python -m alembic upgrade head # For Azure (via Azure CLI or portal) az webapp ssh --name sensor-api-webapp --resource-group sensor-api-rg python -m alembic upgrade head
-
Create sample data (optional):
python scripts/create_sample_data.py
-
Test the deployment:
- Visit
/docsfor FastAPI documentation - Visit
/graphqlfor GraphQL playground - Test a simple query
- Visit
Make sure to set these in your deployment platform:
DATABASE_URL=postgresql://user:pass@host:port/db
SECRET_KEY=your-super-secret-key-here
ENVIRONMENT=production
DEBUG=false
CORS_ORIGINS=["https://yourdomain.com"]- Built-in analytics in Vercel dashboard
- Function logs available
- Performance monitoring included
- Application Insights for monitoring
- Log Analytics for detailed logging
- Azure Monitor for alerts
Choose the platform that best fits your needs! Vercel is perfect for getting started quickly, while Azure offers more enterprise-grade features.