AWS serverless compute service that runs code in response to events without provisioning or managing servers, automatically scaling from zero to thousands of concurrent executions.
AWS Lambda is Amazon's Functions as a Service (FaaS) offering that lets you run code without provisioning or managing servers. You upload your function, define what events trigger it, and AWS handles provisioning, automatic scaling, high availability, and monitoring.
Lambda operates on an event-driven execution model: when a trigger occurs (HTTP request, queue message, file uploaded to S3), AWS provisions an execution environment, runs your code, and charges only for the compute time used. This model eliminates the need to maintain idle servers and enables instant scaling from zero to thousands of concurrent executions.
The service supports multiple runtimes (Python, Node.js, Java, Go, .NET, Ruby) and allows deployments with both ZIP files and container images, offering flexibility for different architectures and use cases.
Lambda operates with a specific lifecycle that directly impacts performance and costs:
During the initialization phase, Lambda executes any code outside the handler — ideal for initializing database connections, HTTP clients, or configurations that can be reused across invocations.
import json
import logging
import os
import boto3
from botocore.exceptions import ClientError
from aws_lambda_powertools import Logger, Tracer, Metrics
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.metrics import MetricUnit
# Initialization outside handler (reused across invocations)
logger = Logger()
tracer = Tracer()
metrics = Metrics()
# Reusable DynamoDB client
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(os.environ['TABLE_NAME'])
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
@tracer.capture_lambda_handler
@metrics.log_metrics(capture_cold_start_metric=True)
def lambda_handler(event, context):
"""
Processes API Gateway events with structured error handling
and complete observability using Lambda Powertools.
"""
try:
# Extract data from event
body = json.loads(event.get('body', '{}'))
user_id = body.get('user_id')
if not user_id:
logger.error("Missing user_id in request body")
return {
'statusCode': 400,
'body': json.dumps({'error': 'user_id is required'})
}
# Business operation with tracing
with tracer.subsegment("dynamodb_operation"):
response = table.get_item(Key={'user_id': user_id})
if 'Item' not in response:
logger.info(f"User {user_id} not found")
metrics.add_metric(name="UserNotFound", unit=MetricUnit.Count, value=1)
return {
'statusCode': 404,
'body': json.dumps({'error': 'User not found'})
}
# Success
user_data = response['Item']
logger.info(f"Successfully retrieved user {user_id}")
metrics.add_metric(name="UserRetrieved", unit=MetricUnit.Count, value=1)
return {
'statusCode': 200,
'body': json.dumps(user_data, default=str)
}
except ClientError as e:
logger.error(f"DynamoDB error: {e.response['Error']['Message']}")
metrics.add_metric(name="DynamoDBError", unit=MetricUnit.Count, value=1)
return {
'statusCode': 500,
'body': json.dumps({'error': 'Internal server error'})
}
except Exception as e:
logger.exception("Unexpected error occurred")
metrics.add_metric(name="UnexpectedError", unit=MetricUnit.Count, value=1)
return {
'statusCode': 500,
'body': json.dumps({'error': 'Internal server error'})
}Lambda Layers allow sharing code, libraries, and configurations across multiple functions:
A Layer can contain up to 250 MB (uncompressed) and a function can use up to 5 Layers. Layers are mounted at /opt during execution, allowing the runtime to access libraries automatically.
Since 2020, Lambda supports container images as an alternative to ZIP files:
| Aspect | ZIP Package | Container Image |
|---|---|---|
| Maximum size | 250 MB (compressed) | 10 GB |
| Deployment time | Fast (seconds) | Moderate (minutes) |
| Runtime flexibility | Predefined runtimes | Fully customizable runtime |
| Tooling | SAM, Serverless Framework | Docker, ECR |
| Cold start | Optimized | Slightly higher |
Container images are ideal for functions with heavy dependencies (ML models, scientific libraries) or when you need full control over the runtime environment.
Cold starts impact initial function latency. Mitigation strategies:
Provisioned Concurrency: Maintains pre-initialized environments
SnapStart (Java only):
Architectural optimizations:
AWS Lambda Powertools provides utilities for structured logging, metrics, and tracing:
Powertools is available for Python, TypeScript, Java, and .NET, following the same APIs and patterns across all runtimes.
Lambda charges for three main components:
Requests: $0.20 per million requests Duration: $0.0000166667 per GB-second of execution Provisioned Concurrency: $0.0000041667 per GB-second of reserved capacity
Memory tuning: More memory = more CPU = faster execution
Architecture patterns:
Free tier: 1M free requests + 400,000 GB-seconds monthly
For logic executed at CloudFront's edge:
| Feature | Lambda@Edge | CloudFront Functions |
|---|---|---|
| Runtime | Node.js, Python | JavaScript (ES5) |
| Maximum duration | 30s | 1ms |
| Maximum memory | 3008 MB | 2 MB |
| Triggers | 4 CloudFront events | 2 CloudFront events |
| Cost | More expensive | Cheaper |
| Use cases | A/B testing, auth | URL rewrites, headers |
Each Lambda function requires an execution role that defines which AWS services it can access. Security principles:
Lambda represents a fundamental shift in the compute model: from "servers running applications" to "functions responding to events." For event-driven architectures, this model offers significant advantages: automatic scaling, pay-per-actual-use, and elimination of infrastructure management.
However, Lambda is not a universal solution. Duration limitations (15 minutes), stateless model, and cold starts make it unsuitable for long-running applications or those with extremely low latency requirements. The decision between Lambda and traditional containers should consider traffic patterns, performance requirements, and operational complexity.
Cloud computing model where the provider manages infrastructure automatically, allowing code execution without provisioning or managing servers, paying only for actual usage.
AWS identity and access management service controlling who can do what in your account, with granular policies based on the principle of least privilege.
Architectural pattern where components communicate through asynchronous events, enabling decoupled, scalable, and reactive systems.
Architecture design for scaling a personal second brain to a production system with AWS serverless — from the current prototype to specialized use cases in legal, research, and community building.
Collection of 13 Terraform modules published on the Terraform Registry for deploying serverless architectures on AWS, with 12 examples covering basic ECS to full-stack CRUD with DynamoDB and AgentCore with MCP.
Production-ready serverless backend for a personal knowledge graph — DynamoDB, Lambda, Bedrock, MCP, Step Functions. The implementation of the architecture described in the 'From Prototype to Production' essay.
Serverless GitHub App that auto-approves pull requests after CI passes, with optional AI code review via Amazon Bedrock. Five repositories: TypeScript/Probot app, AWS Terraform module (Lambda + API Gateway + Secrets Manager + SQS DLQ), GitHub Terraform module (webhooks), deployment infra, and test repo.
AWS serverless orchestration service that coordinates multiple services into visual workflows using Amazon States Language (ASL), with built-in error handling, retries, and parallel execution.
AWS fully managed message queue service that decouples distributed application components, guaranteeing message delivery with unlimited scalability.
AWS pub/sub messaging service that distributes messages to multiple subscribers simultaneously, enabling fan-out patterns and notifications at scale.
AWS open-source framework for building serverless applications with simplified CloudFormation syntax, CLI for local development, and integrated deployment.
AWS serverless event bus connecting applications using events, enabling decoupled event-driven architectures with rule-based routing.
AWS serverless NoSQL database with single-digit millisecond latency at any scale, ideal for applications requiring high performance and automatic scalability.
AWS managed service for creating, publishing, and managing REST, HTTP, and WebSocket APIs that act as entry points to Lambda functions and other backend services.