AWS Lambda is the industry benchmark for Function-as-a-Service (FaaS). Launched in 2014, Lambda lets you run code for virtually any type of application or backend service with zero administration. In this master class, we explore the internal anatomy of a production Lambda function, container reuse optimizations, event-source mappings, and concurrency management.
A Lambda invocation consists of three distinct phases: (1) Init Phase: Downloads code and executes global initialization statements outside the handler; (2) Invoke Phase: Runs the exported handler function with the event payload; (3) Shutdown Phase: Cleans up runtimes upon termination.
Every Lambda function requires an exported Handler Method that accepts an event payload and a context runtime object:
// index.mjs - Production AWS Lambda Handler
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb";
// INITIALIZATION PHASE: Initialize clients OUTSIDE handler for container reuse!
const client = new DynamoDBClient({});
const docClient = DynamoDBDocumentClient.from(client);
export const handler = async (event, context) => {
console.log("Processing Request ID:", context.awsRequestId);
try {
// Parse incoming HTTP request body from API Gateway
const body = JSON.parse(event.body || "{}");
// Save record into DynamoDB
await docClient.send(new PutCommand({
TableName: process.env.ORDERS_TABLE,
Item: {
orderId: context.awsRequestId,
user: body.user,
amount: body.amount,
timestamp: new Date().toISOString()
}
}));
return {
statusCode: 201,
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: "Order processed successfully!", orderId: context.awsRequestId })
};
} catch (error) {
console.error("Lambda Error:", error);
return {
statusCode: 500,
body: JSON.stringify({ error: "Internal Server Processing Error" })
};
}
};
Understanding concurrency is critical to preventing downstream database saturation and ensuring zero cold starts:
| Invocation Type | Event Sources | Error Handling & Retries |
|---|---|---|
| Synchronous | Amazon API Gateway, Application Load Balancer, AWS SDK. | Caller waits for response. Caller handles retries if function errors (HTTP 500). |
| Asynchronous | Amazon S3, Amazon SNS, EventBridge. | Lambda returns 202 Accepted immediately. Automatically retries twice before sending to a Dead Letter Queue (DLQ). |
| Event-Source Mapping | Amazon SQS, DynamoDB Streams, Apache Kafka (MSK). | Lambda actively polls the stream/queue in batches and invokes the function. Automatically splits batches on error. |
Broaden your enterprise cloud architecture and DevOps expertise across our curriculum:
Review fundamental FaaS design principles, stateless execution models, and ephemeral storage.
Secure Lambda execution endpoints with API Gateway JWT authorizers and CORS policies.
Encrypt Lambda environment variables and database secrets using AWS KMS and Secrets Manager.
Hands-on project: Build a serverless image-resizing pipeline triggered by S3 file uploads.