Backend

This lesson explains how to create a Python backend, organize the code for API delivery, and deploy the application to cloud infrastructure with repeatable operational steps.

Backend Stack

For modern Python backend work, teams commonly combine an API framework, a dependency manager, test tooling, and an ASGI or WSGI server.

  • Frameworks: FastAPI, Flask, Django.
  • Servers: Uvicorn, Gunicorn.
  • Quality tools: pytest, Ruff, mypy.
  • Config: environment variables, secrets manager, container config.

Minimal API

A minimal backend should expose health status, isolate business logic, and keep HTTP handlers thin.

from fastapi import FastAPI

app = FastAPI()

@app.get("/health")
def health() -> dict[str, str]:
    return {"status": "ok"}

Project Layout

my_api/
  pyproject.toml
  src/
    my_api/
      __init__.py
      main.py
      routes/
      services/
      models/
  tests/
    test_health.py

Local Run

pip install fastapi uvicorn pytest ruff
uvicorn my_api.main:app --reload

Cloud Deployment

Deployments should be artifact-based and environment-driven. Common targets include containers, platform services, and serverless endpoints.

  • Container platforms: Docker plus Kubernetes or managed container services.
  • Platform-as-a-service: Render, Railway, Fly.io, Azure App Service, Google Cloud Run.
  • Serverless API execution where cold start and dependency size are acceptable.
FROM python:3.12-slim
WORKDIR /app
COPY pyproject.toml ./
COPY src ./src
RUN pip install --no-cache-dir fastapi uvicorn
CMD ["uvicorn", "src.my_api.main:app", "--host", "0.0.0.0", "--port", "8080"]

Release Checklist

  1. Run tests and lint checks in CI.
  2. Build the deployable artifact or image once.
  3. Inject secrets through cloud configuration, never source control.
  4. Expose health checks and logs.
  5. Verify staging before production rollout.

Backend Lab

  1. Create a FastAPI or Flask service with one health route.
  2. Add tests and linting.
  3. Containerize the app.
  4. Deploy it to a cloud platform and verify the public health endpoint.