Serverless Deployment
SQLPage can run on serverless platforms like AWS Lambda, enabling automatic scaling and pay-per-request pricing for your applications.AWS Lambda Deployment
Overview
SQLPage includes a special Lambda runtime that allows it to handle HTTP requests through AWS Lambda and API Gateway. Benefits:- Zero infrastructure management
- Automatic scaling from zero to thousands of requests
- Pay only for actual usage
- Built-in high availability
- Cold start latency (1-3 seconds for first request)
- 15-minute maximum execution time
- Limited to 10GB memory and 512MB /tmp storage
- Best for applications with intermittent traffic
Building for Lambda
SQLPage provides a pre-configured Dockerfile for Lambda:Build the Lambda Function
Deploy to AWS Lambda
Using AWS Console
-
Create Lambda Function:
- Go to AWS Lambda Console
- Click “Create function”
- Choose “Container image”
- Name:
sqlpage-app - Container image URI: Upload your image to ECR first
-
Configure Function:
- Memory: 512 MB (adjust based on needs)
- Timeout: 30 seconds (or higher)
- Environment variables:
DATABASE_URL: Your database connection stringRUST_LOG:sqlpage=info
-
Create API Gateway:
- Add HTTP API trigger
- Create new API
- Security: Open (or configure authentication)
Using AWS CLI
Using SQLite with Lambda
For SQLite databases on Lambda, use the/tmp directory:
/tmpis ephemeral (cleared when function scales down)- Maximum 512 MB storage
- Not suitable for persistent data
Using Amazon RDS
For production Lambda deployments, use a managed database:- Place Lambda in same VPC as RDS for low latency
- Use RDS Proxy to pool database connections
- Set appropriate connection pool size:
Deploying Your Application
Optimizing for Serverless
Reduce Cold Start Time
-
Minimize dependencies:
- Use SQLite for small apps
- Remove unused SQLPage components
-
Provisioned Concurrency:
Keeps 2 warm instances ready (incurs hourly cost).
-
Optimize connection pooling:
Minimize Response Size
Cache Static Assets
Use API Gateway caching:Database Migration on Lambda
Run migrations on Lambda startup:sqlpage/migrations/ on startup.
For complex migrations, use a separate Lambda function or run locally before deployment.
Monitoring Lambda Functions
CloudWatch Logs
View logs in AWS Console or via CLI:Enable Detailed Logging
CloudWatch Metrics
Monitor:- Invocations
- Duration
- Errors
- Throttles
- Cold starts (via custom metrics)
X-Ray Tracing
Enable for detailed performance analysis:Cost Optimization
Lambda Pricing (us-east-1)
- Requests: $0.20 per 1 million requests
- Compute: $0.0000166667 per GB-second
- 1 million requests/month
- 512 MB memory
- 500ms average duration
When Serverless Makes Sense
✅ Good for:- Intermittent traffic (bursty or infrequent)
- Development/staging environments
- Internal tools with sporadic usage
- Auto-scaling from zero
- Consistent high traffic (dedicated server cheaper)
- Real-time applications (cold start latency)
- Large file processing (use ECS/EKS instead)
Alternative Serverless Platforms
Google Cloud Functions
Similar to AWS Lambda, supports custom runtimes:Azure Functions
Supports custom containers:Cloudflare Workers
Not directly compatible (Workers use V8 isolates, not containers). Consider:- Cloudflare Workers + SQLite with D1
- Cloudflare Workers + external database via TCP
Vercel/Netlify
These platforms don’t support custom runtimes like SQLPage. Consider traditional deployment instead.Hybrid Approach: Lambda + ECS
Pattern: Use Lambda for infrequent endpoints, ECS for core application.- Lambda: Admin panel, batch jobs, webhooks
- ECS: Main user-facing application
- Shared database (RDS)
Security Best Practices
Use AWS Secrets Manager
Store sensitive data securely:VPC Configuration
Place Lambda in VPC for secure database access:IAM Least Privilege
Grant only necessary permissions:Testing Lambda Functions Locally
AWS SAM CLI
http://localhost:3000.
Troubleshooting
Cold Start Too Slow
- Enable provisioned concurrency
- Reduce package size
- Use smaller memory (paradoxically faster cold starts)
Database Connection Errors
- Check VPC configuration
- Verify security group rules
- Ensure Lambda has internet access (NAT Gateway) if database is external
- Check connection string format
Out of Memory
- Increase Lambda memory allocation
- Reduce database connection pool size
- Optimize SQL queries to return less data
Timeout Errors
- Increase function timeout (max 15 minutes)
- Optimize slow SQL queries
- Add database indexes