Skip to content

0005: Free-tier AWS deployment on Lambda, with Neon for Postgres

Status: Accepted

Context

The template needs a public demo deployment that costs nothing on an AWS account older than 12 months. Such an account only gets the Always Free offers: Lambda, CloudFront, SQS, EventBridge Scheduler and CloudWatch Logs are covered, but no managed PostgreSQL (RDS and Aurora free tiers last 12 months). A VPC with a NAT gateway, interface endpoints or ElastiCache would each cost money every month.

Lambda also changes how the app runs: - The execution environment freezes as soon as the response is complete, so work done after the response (the user_activity row, BackgroundTasks) would be lost. - Every concurrent request gets its own instance, so in-memory state (rate-limit counters, connection pools) is per instance. - CloudFront is the only intended caller, but a Function URL is public, and request.client is not the viewer.

Decision

  • Compute: both functions run from one Python zip (scripts/build-lambda-zip.sh) with the AWS Lambda Web Adapter layer, which runs the normal Uvicorn server. We chose it over Mangum because the app stays exactly as tested (same ASGI server and lifespan; Mangum runs the lifespan on every invocation). We chose a zip over a container image because Lambda code storage is free and ECR storage is not.
  • Database: Neon's free plan (it runs on AWS), reached over TLS without a VPC. CI runs scripts/prestart.sh against it before each deploy (ADR 0003).
  • Edge: one CloudFront distribution serves the SPA from S3 and /api/* from the Function URL, so the browser sees one origin. CloudFront adds a secret X-Origin-Verify header, which CloudFrontOriginMiddleware requires. It also takes the client IP from CloudFront-Viewer-Address.
  • Post-response work: CompleteAfterAppMiddleware (DEFER_RESPONSE_UNTIL_COMPLETE=true) holds the last body chunk until the audit row is written.
  • Email: queue_email sends messages to SQS (EMAIL_QUEUE_URL), and a worker function sends them. Holding the response for an SMTP round trip would slow requests, and the timing would reveal on /password-recovery whether an address is registered.
  • Scheduled purge: EventBridge Scheduler invokes the worker daily.

Consequences

  • Rate limits are per Lambda instance (memory://): a determined client can exceed them by hitting several instances. A shared store (Redis) would fix this but is not free.
  • The idle SQS event source mapping uses roughly 650,000 of the 1,000,000 free SQS requests per month.
  • Cold starts add latency to the first request after a period of inactivity, and Neon's free compute also suspends when idle.
  • The Lambda environment holds the app secrets, and Terraform's local state holds a copy. Both must stay private.
  • The Compose / container deployment is unchanged; all of the above is opt-in through settings.