We use cookies to enhance your experience on the site
CodeWorlds

Cloud - Deployment

Time to deploy the application to production! Learn about the main cloud platforms.

Deployment Options

1. PaaS - Platform as a Service

Simplest - the platform manages the infrastructure:

  • Heroku - easiest to start with
  • Railway - modern alternative
  • Render - free tier
  • Fly.io - edge deployment

2. IaaS - Infrastructure as a Service

More control, more work:

  • AWS EC2 - virtual servers
  • Google Compute Engine - GCP
  • DigitalOcean Droplets - simple VPS

3. Serverless

Pay only for execution:

  • AWS Lambda - serverless functions
  • Google Cloud Functions
  • Vercel (for frontend)

Deployment on Railway

Railway is a modern PaaS platform - ideal for getting started!

1. Project Preparation

1# Project structure
2project/
3├── src/
4│   └── main.py
5├── requirements.txt
6├── Procfile
7└── railway.json
1# Procfile
2web: uvicorn src.main:app --host 0.0.0.0 --port $PORT
1// railway.json
2{
3  "build": {
4    "builder": "NIXPACKS"
5  },
6  "deploy": {
7    "startCommand": "uvicorn src.main:app --host 0.0.0.0 --port $PORT",
8    "healthcheckPath": "/health"
9  }
10}

2. Deploy via CLI

1# Install CLI
2npm install -g @railway/cli
3
4# Log in
5railway login
6
7# Create project
8railway init
9
10# Deploy
11railway up
12
13# Add database
14railway add --plugin postgresql

Deployment on AWS

AWS Elastic Beanstalk

1# Install EB CLI
2pip install awsebcli
3
4# Initialize
5eb init safari-api --platform python-3.11
6
7# Create environment
8eb create safari-production
9
10# Deploy
11eb deploy

AWS with Docker (ECS)

1# .github/workflows/aws-deploy.yml
2name: Deploy to AWS
3
4on:
5  push:
6    branches: [main]
7
8jobs:
9  deploy:
10    runs-on: ubuntu-latest
11    steps:
12      - uses: actions/checkout@v4
13
14      - name: Configure AWS credentials
15        uses: aws-actions/configure-aws-credentials@v4
16        with:
17          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
18          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
19          aws-region: eu-central-1
20
21      - name: Login to Amazon ECR
22        id: login-ecr
23        uses: aws-actions/amazon-ecr-login@v2
24
25      - name: Build and push image
26        env:
27          ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
28          IMAGE_TAG: ${{ github.sha }}
29        run: |
30          docker build -t $ECR_REGISTRY/safari-api:$IMAGE_TAG .
31          docker push $ECR_REGISTRY/safari-api:$IMAGE_TAG
32
33      - name: Deploy to ECS
34        run: |
35          aws ecs update-service --cluster safari --service api --force-new-deployment

Environment Variables

NEVER commit secrets! Use environment variables:

1# config.py
2import os
3from pydantic_settings import BaseSettings
4
5class Settings(BaseSettings):
6    database_url: str
7    secret_key: str
8    debug: bool = False
9
10    class Config:
11        env_file = ".env"
12
13settings = Settings()
1# .env.example (commit this)
2DATABASE_URL=postgresql://user:password@localhost:5432/db
3SECRET_KEY=your-secret-key-here
4DEBUG=false
1# .gitignore
2.env
3.env.local
4.env.production

Health Checks and Monitoring

1# main.py
2from fastapi import FastAPI
3from datetime import datetime
4
5app = FastAPI()
6
7@app.get("/health")
8async def health_check():
9    return {
10        "status": "healthy",
11        "timestamp": datetime.utcnow().isoformat(),
12        "version": "1.0.0"
13    }
14
15@app.get("/ready")
16async def readiness_check():
17    # Check database connection, cache, etc.
18    try:
19        await check_database()
20        await check_redis()
21        return {"status": "ready"}
22    except Exception as e:
23        return {"status": "not_ready", "error": str(e)}
Go to CodeWorlds