Teams often trigger automation, health checks, and reporting through APIs. This module turns your Python logic into a real HTTP service with FastAPI.
Start with a one-file "hello" API, then study a full Internal DevOps Utilities API that exposes system metrics, log analysis, and AWS info — with routers, services, auto-generated docs, and a Dockerfile.
- Build an API with FastAPI and run it with
uvicorn - The request/response flow and JSON responses
- Organize a real app into
routers/(HTTP) andservices/(logic) - Reuse the log-analyzer and metrics logic from earlier modules
- Explore the auto-generated Swagger docs at
/docs
hello_api.py— the smallest possible FastAPI appdevops-utilities-api/— the full app (routers, services, Dockerfile)
pip install fastapi uvicorn psutil boto3
uvicorn hello_api:app --reload
# open http://127.0.0.1:8000/docscd devops-utilities-api
pip install -r requirements.txt
python main.py # uvicorn on http://0.0.0.0:8000curl http://127.0.0.1:8000/ # hello
curl http://127.0.0.1:8000/health # {"status":"ok"}
curl http://127.0.0.1:8000/metrics # CPU/mem/disk
curl http://127.0.0.1:8000/logs # analyze bundled app.log
curl http://127.0.0.1:8000/aws/s3 # needs AWS credsInteractive docs: http://127.0.0.1:8000/docs
Extend the utilities API (work in devops-utilities-api/):
- Add a
/versionendpoint returning{"version": "1.2.0"}. - Add
/logs/errorsreturning only the ERROR count (reuseservices/logs_service.py). - Add a
cpu_thresholdquery param to/metrics(e.g./metrics?cpu_threshold=50). - Bonus: build the Docker image and hit
/health.