Serverless Architecture — Lambda, API Gateway & SQS
Serverless Architecture — Lambda, API Gateway & SQS
The Serverless Pattern
Serverless does not mean "no servers" — it means you do not manage servers. AWS handles capacity, scaling, patching, and availability. You write the function.
The three pillars of serverless on AWS:
- Lambda — compute
- API Gateway — HTTP trigger
- SQS/SNS/EventBridge — async event trigger
API Gateway + Lambda
The most common serverless pattern is an HTTP API backed by Lambda:
User → API Gateway (HTTP API) → Lambda → ResponseAPI Gateway handles:
- TLS termination
- Request routing (GET /users, POST /users/{id})
- Auth (JWT, API key, custom Lambda authorizer)
- Throttling (requests/second limits)
- CORS headers
The Lambda receives an event object that contains everything about the request:
export const handler = async (event) => {
// event shape for API Gateway HTTP API
const { method, path, pathParameters, queryStringParameters, body } = {
method: event.requestContext.http.method,
path: event.requestContext.http.path,
pathParameters: event.pathParameters, // { id: '123' } from /users/{id}
queryStringParameters: event.queryStringParameters,
body: event.body ? JSON.parse(event.body) : null,
}
return {
statusCode: 200,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ path, method }),
}
}SQS — Asynchronous Decoupling
SQS queues let services communicate without being directly connected. Classic use case: a web app receives an order, puts a message on a queue, and a separate Lambda processes it asynchronously.
Web App → SQS Queue → Lambda (processes order)
↓ (failed after retries)
Dead Letter Queue (for inspection)Two queue types:
| Standard Queue | FIFO Queue | |
|---|---|---|
| Throughput | Unlimited | 300 msg/s (3,000 with batching) |
| Ordering | Best-effort | Guaranteed |
| Delivery | At least once | Exactly once |
| Use case | High-throughput async | Order-sensitive processing |
Lambda ↔ SQS integration: Lambda polls the queue for you (you don't write the poll loop):
export const handler = async (event) => {
// event.Records is an array — Lambda batches SQS messages
for (const record of event.Records) {
const message = JSON.parse(record.body)
console.log('Processing:', message)
// If this throws, the message returns to the queue for retry
await processOrder(message)
}
// Returning normally deletes processed messages from the queue
}Visibility timeout: when Lambda reads a message, it becomes invisible for the visibility timeout period (default 30s). If your Lambda takes longer than the timeout to finish, the message reappears and another Lambda picks it up — causing double-processing. Set visibility timeout to 6× your Lambda timeout.
SNS — Fan-Out
SNS topics publish to multiple subscribers simultaneously:
Order Created Event → SNS Topic
├── SQS Queue (fulfillment service)
├── SQS Queue (analytics service)
├── Lambda (send confirmation email)
└── HTTP endpoint (third-party integration)One publish, four consumers process it independently.
EventBridge — Scheduled and Custom Events
// Cron syntax (run at 9am UTC every weekday)
// rate(5 minutes) — every 5 minutes
// cron(0 9 ? * MON-FRI *) — weekdays at 9am UTC
// EventBridge invokes your Lambda with an event like:
{
"source": "aws.events",
"detail-type": "Scheduled Event",
"detail": {}
}Use EventBridge for: daily report generation, queue health checks, cache warming, cleanup jobs.