- AWS
- Engineering
- DevOps
Designing a Scalable Serverless API on AWS Lambda
How I structure Lambda + API Gateway + DynamoDB services that stay cheap, fast and maintainable as they grow.
Building a serverless API on AWS Lambda gets you a long way before you ever think about servers - but only if you structure it deliberately. Here is the pattern I reach for on most AWS projects.
Designing a serverless API on AWS Lambda: One handler, one responsibility
Each Lambda should do exactly one thing. For tasks that take longer, offload them like background jobs with BullMQ, and keep core business logic in plain, testable modules while letting the handler act as a thin adapter.
import type { APIGatewayProxyHandlerV2 } from 'aws-lambda'
import { createOrder } from '../services/orders'
export const handler: APIGatewayProxyHandlerV2 = async (event) => {
const body = JSON.parse(event.body ?? '{}')
const order = await createOrder(body)
return {
statusCode: 201,
body: JSON.stringify(order),
}
}Model access patterns, not entities
DynamoDB rewards you for designing around how you read data. List your access patterns first, then design the partition and sort keys to serve them with single-digit-millisecond queries.
- Start from the queries, not the schema.
- Use a single-table design where it genuinely simplifies access.
- Add GSIs only for patterns you actually have.
Infrastructure as code from day one
CloudFormation or the CDK means every environment is reproducible and every change is reviewable. When moving beyond serverless to traditional hosting, similar discipline applies to self-hosting Next.js on a VPS with PM2 and Nginx, keeping infrastructure maintainable over time.
Related posts
Verifying Shopify Session Tokens in FastAPI
Learn how to validate JWT session tokens from Shopify App Bridge inside a Python FastAPI backend.
Read articleModeling DynamoDB Many to Many Relationships with Adjacency Lists
Learn how to structure many-to-many relationships in DynamoDB using single-table design and adjacency lists for fast, dual-direction querying.
Read articleRotating Secrets in AWS Secrets Manager FastAPI Services
Learn how to fetch, cache, and rotate database credentials dynamically in FastAPI using AWS Secrets Manager and SQLAlchemy without application downtime.
Read article