Eight Lambda functions and one FastAPI app need structured logging, request context, CloudWatch metrics, and Lambda warming support.
AWS Lambda Powertools covers all of this. wshtlib is a smaller alternative — stdlib only, no external dependencies, ~400 lines.
Structure
A single directory, copied into each Lambda image at build time:
COPY backend/v1/wshtlib/ ./wshtlib/
Six modules:
context— request-scoped store viacontextvars(trace_id, user_id, correlation_id)logger— structured JSON logger with runtime auto-detection (Lambda vs ECS vs local)middleware— FastAPI middleware that initialises context and logs every requestdecorators—@lambda_handlerfor warming, context init, and error handlingmetrics— CloudWatch Embedded Metrics Format outputenv— strict env var loading (raises on missing, no silent defaults)
Lambda Usage
@lambda_handler
def handler(event, context):
logger.info("processing", shoot_id=event["shoot_id"])
metrics.count("PhotosProcessed")
metrics.flush()
@lambda_handler handles warming events, initialises context, and catches unhandled exceptions. The logger picks up trace_id from context automatically. Metrics emit EMF JSON to stdout — CloudWatch picks it up without a sidecar.
FastAPI Usage
app.add_middleware(WshtlibMiddleware)
Same context initialisation, same structured request logs, same X-Trace-Id response header.
What It Doesn’t Cover
No X-Ray tracing integration, no event parsing utilities, no idempotency helpers.