Script Valley
FastAPI: Build Production Python APIs
Production Deployment/Assessment

Practice & Assessment

Test your understanding of Production Deployment

Multiple Choice Questions

6
1

Why is `COPY requirements.txt .` placed before `COPY . .` in a production Dockerfile?

2

What does `pydantic-settings` do if a required field like `database_url` is not set in the environment or `.env` file?

3

What is the difference between a liveness probe and a readiness probe in Kubernetes?

4

Using the formula `(2 × CPU cores) + 1`, how many Gunicorn workers should a 4-core server run?

5

Why should Gunicorn never be exposed directly to the internet without a reverse proxy like Nginx?

6

A Gunicorn master receives a `kill -HUP` signal. What happens to in-flight requests on the old workers?

Coding Challenges

1
1

Containerized FastAPI App with Config Validation

Take an existing FastAPI app (or build a minimal one with GET `/items/` and POST `/items/`) and: (1) Create a `config.py` using `pydantic-settings` with required fields `app_name: str`, `debug: bool = False`, and `max_items: int = 100`. The app should refuse to start if `app_name` is not set. (2) Inject settings via `Depends(get_settings)` in at least one route. (3) Write a `Dockerfile` using the layer-caching pattern. (4) Write a `docker-compose.yml` with the app service reading from a `.env` file. (5) Add GET `/health` (always 200) and GET `/health/ready` (200 with `{status: ok}`, or 503). Inputs: environment variables via `.env`. Outputs: app starts correctly with valid env, refuses to start without `app_name`, health endpoints respond correctly. Estimated time: 25 minutes.

Medium

Mini Project

1

Production-Ready FastAPI Deployment

Take the User and Article API from Module 3 and make it production-ready. Add: (1) `pydantic-settings` config with `database_url`, `secret_key`, `debug` — app must fail at startup if required fields are missing. (2) Request logging middleware that logs method, path, status code, duration, and a short request ID on every request. (3) `/health` liveness and `/health/ready` readiness endpoints (readiness must query the database). (4) A `Dockerfile` with layer-caching, non-root user, and correct CMD. (5) A `docker-compose.yml` with the app service and a PostgreSQL service, env vars from `.env.example` file. (6) A `gunicorn.conf.py` with 4 workers using UvicornWorker. (7) Run the full pytest suite inside Docker using `docker compose run app pytest`. Document the full setup and deployment process in `README.md`.

Hard