AWS Lambda
LearningTree ยท AWS ยท Compute

AWS Lambda โ€”
Serverless Compute

Run code without provisioning servers. Pay only for execution time. Scale automatically from zero to thousands of concurrent requests โ€” the core building block of serverless architecture on AWS.

โšก Lambda in 30 Seconds

  • Serverless compute โ€” upload your code, AWS runs it in response to events
  • No servers to manage โ€” no patching, no scaling config, no idle costs
  • Pay per invocation + duration โ€” billed in 1ms increments (minimum 1ms)
  • Scales automatically โ€” from 0 to 1000+ concurrent executions instantly
  • Integrates with 200+ AWS services โ€” S3, API Gateway, SQS, DynamoDB, EventBridge
01
Chapter One

What is Lambda

Introduction Introductory

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. You upload your function code, define what triggers it, and AWS handles everything else โ€” server provisioning, patching, scaling, high availability, and monitoring. Your code runs only when triggered and you pay only for the compute time consumed.

๐Ÿ‘‰ Think of Lambda as: A vending machine for code execution โ€” insert an event, get a result. No kitchen to maintain.

Lambda was released in 2014 and fundamentally changed how applications are built on AWS. Instead of running servers 24/7 waiting for requests, you write small, focused functions that execute only when needed โ€” transforming fixed infrastructure costs into variable costs proportional to actual usage.

Why Lambda Exists Introductory
โš ๏ธ

Traditional Server Problems

  • Must provision servers before traffic arrives โ€” pay for idle capacity 24/7
  • Scaling requires configuration โ€” Auto Scaling Groups, health checks, ALBs
  • OS patching and security updates โ€” your responsibility, constant maintenance
  • Over-provisioning to handle peaks โ€” paying for capacity used 5% of the time
  • Deployment complexity โ€” rolling updates, drains, health checks
โœ…

Lambda Solves

  • Zero idle cost โ€” code runs only in response to events, scale to zero
  • Automatic scaling โ€” from 0 to 1000+ concurrent executions, no config needed
  • No OS management โ€” AWS manages the execution environment entirely
  • Pay per millisecond โ€” exactly proportional to actual usage
  • Deploy in seconds โ€” upload code, done. No instance rollouts or drains
The Serverless Concept Introductory

"Serverless" doesn't mean no servers โ€” it means you don't think about servers. AWS runs your code on servers they manage, but the infrastructure is completely abstracted away. You focus solely on business logic.

AspectEC2 (Server-based)Lambda (Serverless)
ProvisioningYou choose instance type, size, countAutomatic โ€” AWS allocates resources per invocation
ScalingConfigure ASG, min/max, policiesInstant โ€” each event gets its own execution
Cost modelPay per hour/second while runningPay per invocation + milliseconds of compute
MaintenancePatch OS, update runtimes, manage AMIsAWS manages everything below your code
Idle costRunning 24/7 even with zero traffic$0 when not executing โ€” true scale-to-zero
Max executionUnlimited (server stays running)15 minutes per invocation (hard limit)
StateLocal disk, memory persists between requestsStateless โ€” no guaranteed persistence between calls
Concept Diagram โ€” Event-Driven Execution Introductory
Lambda Concept: Event โ†’ Function โ†’ Response
EVENT SOURCES API Gateway request S3 file upload SQS message Scheduled (cron) trigger AWS Lambda Your function code Runs in isolated container 128MBโ€“10GB memory Max 15 min timeout result OUTPUTS HTTP response Write to DynamoDB Publish to SNS/SQS Write to S3 Billing: per invocation ($0.20/1M) + duration ($0.0000166667/GB-sec)
AWS Architecture Diagram โ€” Lambda in Context Core
Typical Lambda Integration: API Gateway โ†’ Lambda โ†’ DynamoDB
Client HTTPS API Gateway REST / HTTP API Auth, throttle, CORS Route to Lambda Lambda Business logic Node.js / Python 256MB, 30s timeout DynamoDB NoSQL table Single-digit ms Auto-scaling CloudWatch Logs Metrics Tracing HTTPS invoke read/write auto-logs all invocations
Where Lambda Fits in AWS Introductory
๐ŸŒ

APIs & Web

REST/HTTP APIs via API Gateway. Backend logic without servers โ€” the most common Lambda pattern. Powers millions of production APIs.

๐Ÿ“จ

Event Processing

S3 file uploads, SQS messages, DynamoDB stream changes, IoT events. React to events in real-time without polling.

โฐ

Scheduled Jobs

Cron-like execution via EventBridge rules. Reports, cleanup, data sync โ€” without a running server waiting between runs.

๐Ÿ”„

Data Pipelines

Transform data between services: S3 โ†’ Lambda โ†’ Redshift. ETL, image processing, PDF generation, video transcoding triggers.

๐Ÿ›ก๏ธ

Security & Ops

Auto-remediation: detect config drift โ†’ Lambda fixes it. Respond to CloudTrail events, GuardDuty findings, security alerts.

๐Ÿ”Œ

Glue & Integration

Connect services that don't natively talk to each other. Lambda as the universal adapter between AWS services and external APIs.

Mental Model Core

Think of Lambda like a taxi service vs owning a car:

๐Ÿš—

EC2 = Owning a Car

  • Buy the car (provision the instance)
  • Pay insurance even when parked (idle cost)
  • You maintain it โ€” oil changes, tires (OS patches)
  • Always available โ€” just walk to the garage
  • You choose the size and model (instance type)
  • Great for: daily commute (steady workloads)
๐Ÿš•

Lambda = Taking a Taxi

  • No car to buy โ€” just hail when needed (event triggers)
  • Pay only for the ride โ€” meter running only during execution
  • Driver handles maintenance (AWS manages runtime)
  • Slight wait for pickup (cold start โ€” 100-500ms first time)
  • Limited trip length (15 min max execution)
  • Great for: occasional trips (bursty/event-driven workloads)
Key Takeaway

Lambda is not a replacement for EC2 โ€” it is a different compute model. Use Lambda when workloads are event-driven, short-lived, and bursty. Use EC2 when workloads are long-running, stateful, or need full OS control.

Use Cases โ€” When Lambda Shines Core
Use CasePatternWhy Lambda Wins
REST API backendAPI Gateway โ†’ Lambda โ†’ DynamoDBZero idle cost, auto-scales to any traffic level
File processingS3 upload โ†’ Lambda โ†’ transform โ†’ S3Process only when files arrive, pay per file
Queue consumerSQS โ†’ Lambda batch processingScales consumers with queue depth automatically
Real-time streamsKinesis/DynamoDB Streams โ†’ LambdaProcess each record as it arrives
Cron jobsEventBridge schedule โ†’ LambdaNo server running 24/7 for a 5-second daily job
WebhooksAPI Gateway โ†’ Lambda โ†’ forward/processHandle sporadic external callbacks at any volume
ChatOps / BotsAPI Gateway โ†’ Lambda โ†’ Slack/TeamsIdle 99% of the time โ€” perfect for pay-per-use
When NOT to Use Lambda Core
๐Ÿšซ

Lambda is Wrong For

  • Long-running processes โ€” anything >15 minutes (use ECS/Fargate)
  • Stateful workloads โ€” in-memory caches, persistent connections (use EC2)
  • High-performance computing โ€” GPU, custom hardware (use EC2 P/G instances)
  • Constant, steady-state load โ€” 100% CPU 24/7 is cheaper on EC2
  • Large applications โ€” 250MB deploy limit, 10GB memory max
๐Ÿ“

Lambda Constraints

  • Timeout: max 15 minutes per invocation
  • Memory: 128MB to 10,240MB (CPU scales proportionally)
  • Package size: 50MB zipped, 250MB unzipped (layers help)
  • Concurrency: 1000 per region default (can increase)
  • Ephemeral storage: 512MB to 10GB /tmp
  • No persistent state between invocations (use DynamoDB/S3)
๐Ÿ“‹ Chapter 1 โ€” Summary
  • Lambda is serverless compute โ€” run code without managing servers.
  • Event-driven: code executes only in response to triggers (API call, file upload, message, schedule).
  • Pay per invocation + duration โ€” true scale-to-zero, $0 when idle.
  • Automatic scaling โ€” from 0 to 1000+ concurrent without configuration.
  • Constraints: 15-min timeout, 10GB memory max, stateless, 250MB package size.
  • Best for: APIs, event processing, scheduled jobs, glue between services.
  • Not for: long-running processes, stateful workloads, steady-state high CPU.
  • Mental model: Lambda = taxi (pay per ride), EC2 = owning a car (pay while parked).
02
Chapter Two

Core Concepts

The Anatomy of a Lambda Function Core

A Lambda function is more than just code โ€” it's a configuration bundle that tells AWS what to run, how to run it, and what permissions it has. Understanding these components is essential before writing your first function.

Lambda Function Anatomy
LAMBDA FUNCTION HANDLER Entry point for execution file.method format e.g. index.handler Receives (event, context) RUNTIME Language environment Node.js 20 ยท Python 3.12 Java 21 ยท .NET 8 ยท Go Or custom runtime (AL2023) EXECUTION ROLE (IAM) What Lambda CAN do Write to DynamoDB? โœ“ Read from S3? โœ“ Publish to SNS? โœ“ MEMORY & TIMEOUT Memory: 128MB โ€“ 10,240MB CPU scales with memory Timeout: 1s โ€“ 15 min 1769MB = 1 full vCPU ENVIRONMENT VARS Key-value config injected DB_HOST, API_KEY, STAGE Encrypted at rest (KMS) Change without redeploy LAYERS Shared libraries / code Up to 5 layers per function Versioned independently Reduce deploy package size
Handler โ€” The Entry Point Core

The handler is the method Lambda calls when your function is invoked. It receives two arguments: the event (input data from the trigger) and context (metadata about the invocation โ€” remaining time, request ID, log group).

RuntimeHandler FormatExample
Node.jsfile.exportedFunctionindex.handler
Pythonfile.function_namelambda_function.lambda_handler
Javapackage.Class::methodcom.example.Handler::handleRequest
GoBinary namemain (compiled binary)
.NETAssembly::Type::MethodMyApp::MyApp.Function::Handler

๐Ÿ‘‰ The event object is your input. Its structure depends entirely on the trigger โ€” an API Gateway event looks nothing like an S3 event. Always validate the event shape in your handler.

Runtime โ€” Language Environment Core

Lambda supports multiple managed runtimes. AWS maintains and patches these โ€” you don't install anything. Choose based on your team's skills and performance requirements.

RuntimeCold StartBest ForNotes
Node.js 20~100-200msAPIs, lightweight processingMost popular Lambda runtime. Fast cold starts.
Python 3.12~150-250msData processing, ML inference, scriptingExcellent library ecosystem (boto3, pandas).
Java 21~800-3000msEnterprise apps, heavy computationSlow cold start. Use SnapStart to reduce ~90%.
Go~50-80msHigh-performance, low-latencyFastest cold start. Compiled binary, no runtime overhead.
.NET 8~300-600msEnterprise .NET ecosystemsAOT compilation available for faster starts.
Custom (AL2023)VariesRust, C++, any languageYou provide the runtime bootstrap binary.
Key Takeaway

For latency-sensitive APIs: use Node.js, Python, or Go. For enterprise workloads: use Java with SnapStart or Provisioned Concurrency to eliminate cold starts.

Memory & CPU โ€” The Only Knob You Turn Core

Lambda doesn't let you choose CPU directly. Instead, CPU scales linearly with memory. More memory = more CPU = higher cost per millisecond. The sweet spot balances execution speed vs cost.

MemoryvCPU EquivalentCost/msGood For
128 MB~0.08 vCPU$0.0000000021Simple transforms, tiny handlers
512 MB~0.3 vCPU$0.0000000083API handlers, light processing
1,769 MB1 full vCPU$0.0000000292Most production workloads
3,008 MB2 vCPU$0.0000000493CPU-intensive tasks, image processing
10,240 MB6 vCPU$0.0000001667ML inference, video processing

๐Ÿ‘‰ Counter-intuitive truth: doubling memory can halve execution time for CPU-bound functions โ€” meaning the same total cost but faster response. Always benchmark with different memory sizes.

IAM Execution Role โ€” What Lambda Can Do Core

Every Lambda function has an execution role โ€” an IAM role that grants permissions for what the function can access. This is the security boundary. A function with no S3 permissions cannot read S3, even if your code tries.

๐Ÿ”‘

Execution Role Includes

  • Trust policy: allows Lambda service to assume this role
  • Permission policies: what AWS resources the function can access
  • CloudWatch Logs: always included (logs:CreateLogGroup, PutLogEvents)
  • Follows least-privilege โ€” only grant what's needed
๐Ÿ›ก๏ธ

Common Managed Policies

  • AWSLambdaBasicExecutionRole โ€” CloudWatch Logs only
  • AWSLambdaVPCAccessExecutionRole โ€” ENI management for VPC
  • AWSLambdaSQSQueueExecutionRole โ€” read from SQS
  • AWSLambdaDynamoDBExecutionRole โ€” read DynamoDB streams
Environment Variables Core

Environment variables inject configuration into your function without changing code. Use them for database endpoints, API keys, feature flags, and stage identifiers (dev/staging/prod). They are encrypted at rest using KMS.

VariableExample ValuePurpose
TABLE_NAMEusers-prodDynamoDB table name (changes per environment)
STAGEproductionControl behavior per deployment stage
LOG_LEVELINFOAdjust logging verbosity without redeploy
API_KEY(encrypted)External service credentials โ€” use SSM/Secrets Manager instead for rotation
Layers โ€” Shared Dependencies In-Depth

Layers let you package libraries, custom runtimes, or shared code separately from your function. This keeps deployment packages small and enables reuse across multiple functions.

โœ…

When to Use Layers

  • Large dependencies (numpy, pandas, ffmpeg)
  • Shared utility code across 5+ functions
  • Custom runtimes (Rust bootstrap, PHP)
  • Reducing deploy package below 50MB limit
โš ๏ธ

Layer Gotchas

  • Max 5 layers per function (250MB total unzipped)
  • Layers are immutable โ€” publish new version for changes
  • Version pinning required โ€” no "latest" auto-update
  • Adds complexity โ€” weigh convenience vs dependency management
Versions & Aliases In-Depth

Lambda supports versions (immutable snapshots) and aliases (named pointers to versions). This enables safe deployments and traffic shifting without changing client configurations.

Versions & Aliases: Safe Deployment Pattern
$LATEST mutable (dev) Version 3 immutable snapshot Version 4 immutable snapshot Version 5 new deploy alias: PROD 90% โ†’ v4 ยท 10% โ†’ v5 90% 10% canary

Aliases enable canary deployments: route 10% of traffic to the new version while 90% stays on the stable version. If errors spike, roll back instantly by updating the alias pointer โ€” no redeploy needed.

Function Configuration Reference In-Depth
SettingDefaultRangeNotes
Memory128 MB128 โ€“ 10,240 MBCPU scales proportionally. 1769MB = 1 vCPU.
Timeout3 seconds1s โ€“ 15 minSet just above p99 duration + buffer.
Ephemeral /tmp512 MB512 MB โ€“ 10 GBPersists across warm invocations only.
Concurrency1000/region0 โ€“ account limitPer-function reserved concurrency available.
Package sizeโ€”50 MB zip / 250 MB unzippedUse layers or container images (10GB) for more.
Env varsโ€”4 KB totalEncrypted at rest with KMS. Use SSM for large configs.
Layers0Max 5Total unzipped must still be โ‰ค250MB.
๐Ÿ“‹ Chapter 2 โ€” Summary
  • Handler: entry point (file.method). Receives event + context objects.
  • Runtime: Node.js, Python, Java, Go, .NET, or custom. Choose based on cold start tolerance.
  • Memory: only config knob โ€” 128MB to 10GB. CPU scales linearly. 1769MB = 1 vCPU.
  • Timeout: max 15 min. Set to p99 + buffer โ€” never use max blindly.
  • Execution role: IAM role defining what the function can access. Least privilege always.
  • Environment variables: inject config (table names, stage, keys). Encrypted at rest.
  • Layers: shared dependencies across functions. Max 5, versioned, immutable.
  • Versions & aliases: immutable snapshots + named pointers enable canary deployments and instant rollback.
03
Chapter Three

Execution Model

How Lambda Actually Runs Your Code Core

When Lambda receives an event, it finds (or creates) an execution environment โ€” a lightweight container with your code, runtime, and dependencies. Understanding this lifecycle is the key to optimizing cold starts and managing state.

Invocation Types Core

Lambda supports three invocation models. The model determines who waits for the result and who handles retries.

Three Invocation Types
SYNCHRONOUS Caller waits for response Caller handles retries API Gateway, ALB, SDK RequestResponse mode ASYNCHRONOUS Caller gets 202 immediately Lambda retries 2ร— on failure S3, SNS, EventBridge, IoT Event mode + DLQ/destination EVENT SOURCE MAPPING Lambda polls the source Batch processing (1-10K) SQS, Kinesis, DynamoDB Lambda manages retry/offset Client Lambda wait โ† response S3/SNS Queue Lambda 202 instant โœ“ SQS Lambda polls Lambda batch
TypeCaller Waits?Retry BehaviorTriggersError Handling
SynchronousYes โ€” blocks until responseCaller must retryAPI Gateway, ALB, SDK invokeError returned to caller directly
AsynchronousNo โ€” 202 Accepted immediatelyLambda retries 2ร— (configurable)S3, SNS, EventBridge, CloudFormationDLQ or on-failure destination
Event Source MappingN/A โ€” Lambda pollsRetries until success or expirySQS, Kinesis, DynamoDB Streams, KafkaBisect batch, maxRetries, DLQ
Cold Start vs Warm Start Core

The most important performance concept in Lambda. A cold start happens when Lambda must create a new execution environment from scratch. A warm start reuses an existing environment โ€” dramatically faster.

Cold Start vs Warm Start Lifecycle
COLD START (first invocation) Download code Start runtime Init (outside handler) Execute handler (BILLED) 100-3000ms extra WARM START (subsequent invocations) Execute handler (BILLED) ~1-50ms overhead only AWS-managed (not billed, but adds latency) Your code (billed)
AspectCold StartWarm Start
WhenFirst invocation, after idle (~5-15 min), code update, scaling upReuses existing container (within minutes of last invocation)
Latency added100ms (Go) to 3000ms+ (Java unoptimized)~1-5ms (just handler execution)
Init code runs?Yes โ€” code outside handler runs onceNo โ€” skips directly to handler
ConnectionsMust establish new DB/API connectionsReuses connections from init phase
Frequency~1-5% of invocations under steady traffic95-99% of invocations

๐Ÿ‘‰ Init code (outside the handler) runs only on cold start. Put SDK client creation, DB connections, and config loading outside the handler โ€” they persist across warm invocations. This is the single most impactful optimization.

Execution Lifecycle in Detail In-Depth
๐ŸงŠ

INIT Phase (Cold Start Only)

  • Download: code + layers fetched from S3
  • Runtime boot: Node.js/Python/Java process starts
  • Extension init: monitoring agents (Datadog, etc.) initialize
  • Function init: your code outside the handler runs
  • Free 10 seconds for init โ€” not billed (timeout doesn't apply)
  • After init: environment is "warm" and ready
๐Ÿ”ฅ

INVOKE Phase (Every Invocation)

  • Lambda calls your handler with (event, context)
  • Billed from first ms of handler execution
  • Must complete within configured timeout
  • Return value sent back to caller (sync) or logged (async)
  • Environment stays "warm" for ~5-15 min after completion
  • Next invocation reuses same environment (warm start)
Execution Environment Reuse In-Depth

Lambda reuses execution environments across multiple invocations. This is the single most important optimization concept โ€” code outside the handler runs once and persists across warm invocations.

โŒ

Bad โ€” Inside Handler

  • Create DB connection on every invocation
  • Initialize SDK client every time
  • Load config from SSM on every call
  • Result: 50-200ms wasted per invocation
โœ…

Good โ€” Outside Handler (Init)

  • DB connection created once, reused for ~5-15 min
  • SDK client initialized once, available instantly
  • Config loaded once, cached in module scope
  • Result: 1-5ms per warm invocation

๐Ÿ‘‰ What persists across warm invocations: global/module variables, DB connections, SDK clients, /tmp files, imported modules. What does NOT persist: handler local variables, event/context objects, anything after environment timeout (~5-15 min idle).

Concurrency Model In-Depth

Each concurrent invocation gets its own execution environment. One environment handles one request at a time (no multi-threading across requests). If 100 requests arrive simultaneously, Lambda creates 100 environments.

Concurrency TypeWhat It DoesCostWhen to Use
UnreservedShares the account's 1000 pool with all functionsFreeDefault โ€” most functions
ReservedGuarantees N environments for this function (throttles others)FreeCritical functions that must never be throttled
ProvisionedPre-warms N environments โ€” always ready, no cold starts$$$Latency-sensitive APIs where cold starts are unacceptable
Throttling Behavior Core

When concurrency limits are reached, Lambda throttles additional invocations. The throttling behavior depends on the invocation type:

Invocation TypeThrottle BehaviorClient Experience
SynchronousReturns 429 TooManyRequestsExceptionError returned to caller โ€” must implement retry with backoff
AsynchronousEvent queued, retried for up to 6 hoursCaller got 202 already โ€” unaware. Event eventually processes or goes to DLQ.
Event Source Mapping (SQS)Messages stay in queueProcessing delayed โ€” messages visible again after visibility timeout
Event Source Mapping (Kinesis)Shard iterator pausedRecords back up in stream โ€” processing resumes when capacity frees

๐Ÿ‘‰ Exam scenario: "Lambda returns 429 errors" โ†’ answer is always about concurrency limits. Fix: increase account limit, add reserved concurrency, or add SQS queue in front to buffer.

Error Handling & Retries In-Depth

Lambda's retry behavior is completely different per invocation type. Misconfigured retries are the #1 cause of duplicate processing and unexpected costs in serverless systems.

๐Ÿ”„

Sync โ€” Caller Retries

  • Error returned directly to caller
  • Lambda does NOT retry
  • Caller must implement retry logic
  • API Gateway: returns 5xx to client
  • SDK: built-in retry with backoff
๐Ÿ”

Async โ€” Lambda Retries 2ร—

  • Retries automatically (0, 1, or 2 times)
  • Configurable: MaximumRetryAttempts (0-2)
  • After all retries fail โ†’ DLQ or destination
  • MaximumEventAge: discard old events (60s-6hr)
  • Risk: 3 invocations for one event
๐Ÿ“‹

ESM โ€” Until Expiry

  • SQS: retries until visibility timeout, then DLQ
  • Kinesis: retries until record expires (24h-7d)
  • Can block the entire shard (poison pill problem)
  • Fix: BisectBatchOnError, MaximumRetryAttempts
  • Fix: FunctionResponseTypes for partial batch failure
Destinations vs Dead-Letter Queue (DLQ) In-Depth

Both handle failed events, but Destinations are the modern, more flexible approach โ€” they can capture both success and failure, and support more targets.

FeatureDLQ (Legacy)Destinations (Recommended)
CapturesFailures onlySuccess AND failure
TargetsSQS or SNS onlySQS, SNS, Lambda, EventBridge
MetadataOriginal event onlyEvent + response + error + request context
ConfigurationOn the function itselfOn the function (async config)
Invocation typesAsync onlyAsync (+ stream ESM on-failure)

๐Ÿ‘‰ Use Destinations for new designs. DLQ captures only the failed event payload. Destinations capture the full invocation record โ€” event, response, error details, and timestamps โ€” making debugging significantly easier.

Idempotency โ€” Handling Duplicate Events In-Depth

Lambda functions will process the same event multiple times. This is not a bug โ€” it's by design (retries, at-least-once delivery from SQS, duplicate S3 notifications). Your functions must be idempotent: processing the same event twice must produce the same result without side effects.

๐Ÿ”‘

Why Duplicates Happen

  • Async retries: Lambda retries failed async invocations
  • SQS at-least-once: message can be delivered more than once
  • S3 event notifications: can fire twice for one upload
  • Network timeouts: Lambda completed but caller didn't get response
  • Stream replay: Kinesis/DynamoDB on error re-read from checkpoint
โœ…

Idempotency Strategies

  • Idempotency key: use event's unique ID (request_id, message_id)
  • DynamoDB conditional write: PutItem with condition "attribute_not_exists(pk)"
  • Powertools Idempotency: AWS Lambda Powertools library handles it automatically
  • Check-before-write: verify if already processed before acting
  • Design operations to be naturally idempotent: SET (idempotent) vs INCREMENT (not)
Key Takeaway

If your function charges a credit card, sends an email, or increments a counter โ€” it MUST be idempotent. Processing the same payment event twice must not charge twice. This is not optional for production systems.

Common Mistakes Core
MistakeImpactFix
Creating DB connections inside handler50-200ms added per invocationInitialize outside handler โ€” reuse across warm invocations
Ignoring retries โ†’ duplicate processingDouble charges, duplicate emailsImplement idempotency with unique keys
Using VPC unnecessarilyNAT Gateway cost + networking complexityOnly VPC when accessing private resources (RDS, Redis)
Setting memory too lowSlow execution (CPU starved) + higher total costUse Power Tuning tool to find optimal memory
Max timeout on API functionsHung functions billed for 15 minSet timeout to p99 + 20% buffer
No DLQ/destination configuredFailed events silently lost foreverAlways configure on-failure destination
๐Ÿ“‹ Chapter 3 โ€” Summary
  • Synchronous: caller waits (API Gateway, ALB). Caller retries on error.
  • Asynchronous: 202 Accepted immediately (S3, SNS). Lambda retries 2ร—. Use DLQ/Destinations.
  • Event Source Mapping: Lambda polls (SQS, Kinesis, DynamoDB Streams). Batch processing.
  • Cold start: new environment creation. 100ms (Go) to 3000ms (Java). ~1-5% of invocations.
  • Warm start: reuses existing container. ~1-5ms overhead. 95-99% of invocations.
  • Environment reuse: init code outside handler persists. DB connections, SDK clients reused.
  • Throttling: sync โ†’ 429 error, async โ†’ queued for retry, ESM โ†’ messages stay in queue.
  • Idempotency: functions WILL receive duplicates. Use idempotency keys + conditional writes.
  • Destinations > DLQ: capture success + failure, more targets, richer metadata.
  • Concurrency: 1 request per environment. Unreserved (shared), Reserved (guaranteed), Provisioned (pre-warmed).
๐ŸŽ“ Exam Tips โ€” Chapter 3

Lambda = stateless ยท Max execution = 15 minutes ยท Async retries = 2 times ยท Throttled sync = 429 error ยท DLQ only captures failures, Destinations capture both ยท Idempotency is YOUR responsibility ยท VPC adds cold start latency ยท Reserved concurrency = free guarantee

04
Chapter Four

Event Sources & Integrations

Lambda's Superpower: 200+ Integrations Core

Lambda's value isn't just serverless compute โ€” it's the universal glue between AWS services. Every major AWS service can trigger Lambda, and Lambda can call any AWS service via the SDK. This makes it the central nervous system of event-driven architectures.

API Gateway โ€” REST & HTTP APIs Core

The most common Lambda trigger. API Gateway handles HTTPS, authentication, throttling, and CORS โ€” Lambda handles business logic. Together they form a serverless API backend.

API Gateway + Lambda: Serverless REST API
Browser Mobile App API Gateway HTTPS + Auth (Cognito/JWT) Rate limiting + CORS Request/Response transform Lambda GET /users โ†’ handler POST /orders โ†’ handler DynamoDB NoSQL store CloudWatch Logs + Metrics HTTPS invoke R/W HTTP API: $1.00/million requests ยท REST API: $3.50/million (more features)
โšก

HTTP API (Recommended)

  • $1.00/million requests (70% cheaper)
  • Faster โ€” lower latency, simpler proxy
  • JWT authorizer built in (Cognito, Auth0)
  • Best for: most new APIs, microservices
๐Ÿ”ง

REST API (Full-featured)

  • $3.50/million requests
  • Request/response transformation
  • WAF integration, API keys, usage plans
  • Best for: complex APIs needing throttling per client
S3 Events โ€” File Processing Core

S3 triggers Lambda when objects are created, modified, or deleted. This powers image processing, video transcoding, log analysis, and ETL pipelines โ€” all without polling.

S3 Event โ†’ Lambda: Image Processing Pipeline
User uploads photo.jpg S3 (uploads/) PutObject event โ†’ triggers Lambda Lambda Resize to 3 sizes Generate thumbnail S3 (thumbs/) Processed images DynamoDB Store metadata Async invocation ยท Lambda retries 2ร— on failure ยท use DLQ for poison images
SQS โ€” Queue Processing Core

Lambda polls SQS queues and processes messages in batches. This decouples producers from consumers and enables reliable, scalable background processing with automatic retry and dead-letter handling.

๐Ÿ“ฌ

Standard Queue

  • Nearly unlimited throughput
  • At-least-once delivery (possible duplicates)
  • Batch size: 1-10 messages per invocation
  • Lambda scales up to 1000 concurrent batches
  • Best for: high-volume background jobs
๐Ÿ“‹

FIFO Queue

  • Exactly-once processing guaranteed
  • Strict ordering within message group
  • 300 messages/sec (or 3000 with batching)
  • Lambda: 1 concurrent invocation per message group
  • Best for: orders, financial transactions
SNS โ€” Fan-Out Pattern Core

SNS invokes Lambda asynchronously. One SNS topic can fan out to multiple Lambda functions (plus SQS, email, HTTP). This enables event broadcasting โ€” one publish, many consumers.

DynamoDB Streams โ€” Change Data Capture Core

When items change in DynamoDB, streams capture the before/after images. Lambda processes these changes in order โ€” enabling real-time replication, notifications, materialized views, and audit logs.

EventBridge โ€” Scheduled & Event-Driven Core

EventBridge Rules trigger Lambda on schedules (cron/rate) or event patterns from AWS services and custom applications. It's the backbone of event-driven architectures on AWS.

Event Source Summary Core
SourceInvocation TypeBatch?RetryCommon Pattern
API GatewaySynchronousNoCaller retriesREST/HTTP APIs
S3AsynchronousNo2ร— then DLQFile processing, ETL
SNSAsynchronousNo2ร— then DLQFan-out, notifications
SQSEvent Source MappingYes (1-10)Until visibility timeoutQueue consumers, background jobs
KinesisEvent Source MappingYes (1-10K)Until record expires (24h-7d)Real-time stream processing
DynamoDB StreamsEvent Source MappingYes (1-10K)Until record expires (24h)CDC, replication, triggers
EventBridgeAsynchronousNoConfigurableCron jobs, event routing
ALBSynchronousNoCaller retriesMulti-target groups, weighted routing
CloudFront (Lambda@Edge)SynchronousNoโ€”Request/response manipulation at edge
Architecture Diagram โ€” Multi-Source Event Processing In-Depth
Lambda as Central Event Processor (Multi-Source)
API Gateway S3 Events SQS Queue SNS Topic EventBridge (cron) Lambda Functions API handler File processor Queue consumer Notification handler Scheduled job DynamoDB S3 (output) SNS (notify) SQS (next step) CloudWatch Logs
Key Takeaway

Lambda is not a standalone service โ€” its power comes from integrations. Design around events flowing between services, with Lambda as the processing logic between them.

๐Ÿ“‹ Chapter 4 โ€” Summary
  • API Gateway: most common trigger. HTTP API ($1/M) for most; REST API ($3.5/M) for advanced features.
  • S3: async trigger on PutObject. Powers file processing pipelines. Use DLQ for failures.
  • SQS: event source mapping. Batch processing (1-10 msgs). Auto-scales with queue depth.
  • SNS: async fan-out. One publish โ†’ multiple Lambda functions in parallel.
  • DynamoDB Streams: ordered change data capture. Real-time replication and triggers.
  • EventBridge: cron schedules + event pattern matching from 90+ AWS services.
  • Design principle: Lambda is the glue โ€” connect services via events, not direct calls.
05
Chapter Five

Lambda + VPC Networking

Why Put Lambda in a VPC? Core

By default, Lambda runs in an AWS-managed VPC with internet access but no access to your private resources (RDS, ElastiCache, private EC2 instances). If your function needs to connect to a database in a private subnet, you must attach Lambda to your VPC.

โœ…

Put Lambda in VPC When

  • Connecting to RDS/Aurora in private subnets
  • Accessing ElastiCache (Redis/Memcached)
  • Reaching private EC2 instances or ECS services
  • Connecting to resources via VPC peering or Transit Gateway
  • Security requirements mandate private-only network access
โš ๏ธ

Do NOT Put Lambda in VPC When

  • Only calling public AWS APIs (S3, DynamoDB, SQS)
  • Only calling external HTTP APIs
  • No private resource access needed
  • Unnecessary VPC adds cold start latency + NAT cost
  • Most Lambda functions do NOT need VPC

๐Ÿ‘‰ Golden rule: don't put Lambda in a VPC unless you must access private resources. VPC adds ENI creation overhead, NAT Gateway costs ($0.045/hr + data), and networking complexity. DynamoDB, S3, SQS, and SNS are all reachable without VPC.

How Lambda VPC Networking Works Core

When you attach Lambda to a VPC, AWS creates Elastic Network Interfaces (ENIs) in your specified subnets. Lambda execution environments use these ENIs to communicate with resources in the VPC. Since 2019, AWS uses Hyperplane ENIs โ€” shared across functions in the same security group + subnet, dramatically reducing cold start impact.

Lambda in VPC: ENI Architecture
AWS LAMBDA SERVICE Execution env 1 Execution env 2 (AWS-managed infra) YOUR VPC 10.0.0.0/16 PRIVATE SUBNET A 10.0.2.0/24 Hyperplane ENI sg-lambda 10.0.2.50 RDS Primary 10.0.2.10 PRIVATE SUBNET B 10.0.3.0/24 Hyperplane ENI sg-lambda 10.0.3.50 ElastiCache Redis PUBLIC SUBNET 10.0.1.0/24 NAT Gateway EIP attached IGW Internet VPC Endpoints (free for S3) S3 Gateway ยท DynamoDB Gateway No NAT needed for these services cross-account ENI attach Internet via NAT S3/DynamoDB via endpoint (free)
VPC Configuration Checklist Core
SettingRecommendationWhy
SubnetsPrivate subnets in 2+ AZsHA โ€” Lambda uses ENIs across specified subnets. Never use public subnets.
Security GroupDedicated sg-lambdaOutbound: allow 5432 (Postgres), 6379 (Redis), 443 (HTTPS). Inbound: none.
Internet accessNAT Gateway in public subnetLambda in private subnet has no internet without NAT. Required for external APIs.
AWS servicesVPC Endpoints for S3, DynamoDBGateway endpoints are free โ€” avoid NAT data charges for S3/DynamoDB.
IAM roleAdd AWSLambdaVPCAccessExecutionRoleGrants ec2:CreateNetworkInterface, DescribeNetworkInterfaces, DeleteNetworkInterface.
IP addressesEnsure subnet has enough IPsHyperplane ENIs share IPs, but 1 ENI per unique (security group + subnet) pair.
The NAT Gateway Problem In-Depth

Lambda in a VPC with internet access requires a NAT Gateway โ€” and NAT Gateways are expensive. This is the biggest hidden cost of VPC-attached Lambda.

๐Ÿ’ฐ

NAT Gateway Cost

  • $0.045/hour = ~$32/month per NAT (always-on)
  • $0.045/GB processed data
  • 2 AZs = 2 NATs = ~$65/month before any data
  • Lambda calling external APIs? Every byte goes through NAT
  • This can exceed Lambda compute costs for low-traffic functions
๐Ÿ’ก

Cost Reduction Strategies

  • VPC Endpoints: S3 + DynamoDB Gateway endpoints = $0
  • Interface Endpoints: for SQS, SNS, Secrets Manager ($7.20/mo each)
  • Separate functions: only VPC-attach functions that need private access
  • Non-VPC function โ†’ calls public APIs directly (no NAT)
  • VPC function โ†’ calls RDS only (no external internet needed)
Security Groups for Lambda Core

Lambda's security group controls outbound traffic from the function. The resources it connects to must allow inbound from the Lambda security group.

RuleLambda SG (sg-lambda)RDS SG (sg-rds)ElastiCache SG (sg-redis)
InboundNone needed5432 from sg-lambda6379 from sg-lambda
Outbound5432 to sg-rds
6379 to sg-redis
443 to 0.0.0.0/0 (HTTPS)
Default (allow all)Default (allow all)

๐Ÿ‘‰ Reference security groups, not IP addresses. Lambda's ENI IPs change โ€” never hardcode them. Allow sg-lambda in your RDS/Redis security group inbound rules. This is the VPC-native, maintainable approach.

RDS Proxy โ€” Connection Pooling In-Depth

Lambda creates a new database connection per execution environment. At scale (100+ concurrent), this can overwhelm RDS (PostgreSQL default: 100 connections). RDS Proxy solves this by pooling connections.

๐Ÿ’ฅ

Without RDS Proxy

  • 100 concurrent Lambda = 100 DB connections
  • Spike to 500 = "too many connections" errors
  • Connection setup: 30-50ms per cold start (TCP + TLS + auth)
  • Connections linger in warm containers โ†’ wasted DB slots
โœ…

With RDS Proxy

  • Proxy pools connections โ€” 500 Lambda share 50 DB connections
  • Connection reuse via multiplexing
  • IAM auth (no password in code/env vars)
  • Automatic failover โ€” follows RDS Multi-AZ switchover
  • Cost: ~$21/month per proxy instance (based on ACU)
๐Ÿ“‹ Chapter 5 โ€” Summary
  • VPC only when needed: RDS, ElastiCache, private EC2. Most functions don't need VPC.
  • Hyperplane ENIs: shared ENIs per (security group + subnet). Minimal cold start impact since 2019.
  • Private subnets only: never attach Lambda to public subnets (no public IP assigned to ENIs).
  • NAT Gateway required for internet: $32/month/NAT + data charges. Biggest hidden VPC cost.
  • VPC Endpoints: S3 and DynamoDB Gateway endpoints are free. Use them to skip NAT.
  • Security groups: reference sg-lambda in RDS/Redis inbound rules. Never hardcode IPs.
  • RDS Proxy: essential at scale โ€” pools connections, prevents "too many connections" errors.
๐ŸŽ“ Exam Tips โ€” Chapter 5

Lambda in VPC = needs ENI permissions (ec2:CreateNetworkInterface) ยท Private subnets only โ€” public gives NO internet ยท NAT Gateway needed for internet access from VPC ยท VPC Endpoints for S3/DynamoDB avoid NAT ยท Lambda cannot have a public IP ยท Pre-2019 VPC = slow cold starts (fixed with Hyperplane ENIs)

06
Chapter Six

Performance & Optimization

The Performance Levers Core

Lambda performance optimization comes down to three things: reducing cold starts, right-sizing memory, and minimizing external call latency. Most performance issues are not Lambda's fault โ€” they're network calls to databases and APIs.

Memory Tuning โ€” The #1 Optimization Core

Memory is the only resource knob. CPU scales with memory โ€” but the relationship is non-linear for your workload. A CPU-bound function at 128MB may take 2000ms; at 1024MB it takes 250ms โ€” 8ร— faster for 8ร— memory but same total cost.

Memory vs Duration vs Cost: Find the Sweet Spot
Memory (MB) Duration / Cost 128 512 1024 1769 3008 10240 Duration โ†“ Cost (U-shape) SWEET SPOT

๐Ÿ‘‰ Use AWS Lambda Power Tuning (open-source tool by Alex Casalboni). It runs your function at every memory size and gives you the optimal cost/performance point. Don't guess โ€” measure.

Cold Start Reduction Core
TechniqueImpactCostComplexity
Init code outside handlerReuse SDK clients, DB connections across warm invocationsFreeLow โ€” restructure code
Smaller packageReduce download time. Tree-shake, exclude dev depsFreeLow โ€” build optimization
Choose fast runtimeGo ~50ms, Node ~150ms, Java ~1-3s cold startFreeMedium โ€” language choice
Provisioned ConcurrencyEliminates cold starts entirely โ€” pre-warmed$$$ โ€” pay for idle warm environmentsLow โ€” config only
SnapStart (Java only)Snapshots init state, restores in ~200ms vs 3sFreeLow โ€” enable in config
ARM64 (Graviton2)~10-15% faster, 20% cheaper per msSavingsLow โ€” change architecture flag
Provisioned Concurrency In-Depth

Provisioned Concurrency keeps N execution environments pre-initialized and ready. No cold starts, guaranteed sub-100ms startup. Use it for latency-sensitive APIs where even occasional 1-second cold starts are unacceptable.

โšก

When to Use Provisioned

  • APIs with strict SLA (<200ms p99)
  • Java/C# functions with heavy cold starts
  • Predictable traffic patterns (business hours)
  • Use Application Auto Scaling to schedule PC
  • Combine with alias for traffic shifting
๐Ÿ’ฐ

Cost Calculation

  • Provisioned: $0.015/GB-hour (keep-warm cost)
  • 10 instances ร— 512MB ร— 24h = ~$1.80/day
  • Compare vs: EC2 t3.micro = ~$0.25/day
  • Only worth it if traffic justifies serverless benefits
  • Schedule: provision 50 at 8am, scale to 5 at 10pm
ARM64 / Graviton2 Core

Lambda supports ARM64 (Graviton2) processors โ€” 20% cheaper per GB-second and often 10-15% faster than x86. For most workloads, switching is a one-line config change with immediate savings.

Aspectx86_64arm64 (Graviton2)
Price per GB-sec$0.0000166667$0.0000133334 (20% less)
PerformanceBaseline~10-34% faster (workload dependent)
CompatibilityAll runtimes, all native binariesAll managed runtimes. Native binaries must be ARM-compiled.
Migration effortโ€”Pure Python/Node/Java: zero effort. C extensions: recompile.
Timeout Tuning Core

Set timeout to p99 duration + 20% buffer, not the 15-minute max. Too-long timeouts waste money on hung invocations and delay error detection. Too-short timeouts cause false failures on legitimate slow paths.

๐ŸŽฏ

API Handlers

10-30 seconds. API Gateway times out at 29s anyway. Clients expect fast responses.

๐Ÿ“ฆ

Queue Processors

30-120 seconds. Match SQS visibility timeout (6ร— Lambda timeout recommended).

๐Ÿ”„

Data Processing

5-15 minutes. Large file transforms. Use Step Functions for anything longer.

Concurrency & Throttling In-Depth
TypeDefaultPurposeBehavior When Exceeded
Account limit1000/regionProtect account from runaway scalingThrottled (429) or queued depending on invocation type
Reserved concurrencyNot setGuarantee capacity for critical functionsOther functions share remaining pool
Provisioned concurrencyNot setEliminate cold starts for N instancesOverflow uses on-demand (with cold starts)
Burst concurrency500-3000/regionAllow rapid scale-upAfter burst, scales +500/minute
Cost Optimization Core
๐Ÿ’ก

Reduce Cost

  • ARM64: instant 20% savings, one config change
  • Power tuning: find optimal memory โ€” avoid over-provisioning
  • Timeout: reduce from 15min default to actual need
  • VPC only when needed: avoid NAT Gateway costs
  • VPC Endpoints: S3/DynamoDB gateway = free vs NAT data charges
๐Ÿ“Š

Cost Breakeven: Lambda vs EC2

  • <1M invocations/month: Lambda almost always cheaper
  • 1-10M/month: depends on duration and memory
  • >10M/month at steady rate: EC2/Fargate likely cheaper
  • Bursty traffic: Lambda wins even at high volume
  • Rule of thumb: if Lambda >14 hours/day utilized โ†’ consider EC2
๐Ÿ“‹ Chapter 6 โ€” Summary
  • Memory is the only knob: CPU scales linearly. Use Lambda Power Tuning to find the cost/speed sweet spot.
  • Cold start reduction: init outside handler, smaller packages, fast runtimes, SnapStart (Java), Provisioned Concurrency.
  • ARM64/Graviton2: 20% cheaper, 10-15% faster. Switch for pure-language runtimes with zero effort.
  • Provisioned Concurrency: eliminates cold starts. Schedule with Auto Scaling for business-hours traffic.
  • Timeout: set to p99 + 20% buffer. Never use 15-min default on API handlers.
  • Concurrency: 1000/region default. Reserve for critical functions. Burst allows 500-3000 rapid scale.
  • Cost breakeven: Lambda wins for bursty/low-volume. EC2/Fargate wins for steady >14h/day utilization.
๐ŸŽ“ Exam Tips โ€” Chapter 6

Memory โ†‘ = CPU โ†‘ (proportional) ยท 1769MB = 1 vCPU ยท Provisioned Concurrency = no cold starts (costs $$) ยท SnapStart = Java only, free ยท ARM64 = 20% cheaper, one config change ยท Burst limit = 500-3000 depending on region ยท Reserved concurrency of 0 = function disabled (throttles 100%)

07
Chapter Seven

Architecture Patterns

Production Patterns with Lambda In-Depth

Lambda shines in specific architectural patterns. These are battle-tested approaches used at scale by companies from startups to enterprises. Each pattern solves a different problem โ€” choose based on your workload characteristics.

Pattern 1 โ€” Serverless REST API Core

The most common Lambda pattern. API Gateway handles HTTP, auth, and throttling. Lambda handles business logic. DynamoDB handles state. Zero servers, zero idle cost, infinite scale.

Pattern 1: Serverless REST API (Production Stack)
Route 53 CloudFront API GW Cognito auth Rate limiting Custom domain Lambda Business logic Validation Transform DynamoDB NoSQL Single-digit ms On-demand mode S3 (static) Cognito User pools + JWT CloudWatch Cost at 1M req/month: ~$3 (API GW) + $0.20 (Lambda) + $1.25 (DynamoDB) โ‰ˆ $5/month total
Pattern 2 โ€” Event-Driven Pipeline Core

Files arrive โ†’ Lambda processes โ†’ results go elsewhere. Each step is decoupled, independently scalable, and fault-isolated. Failed items go to dead-letter queues without blocking the pipeline.

Pattern 2: Event-Driven File Processing Pipeline
S3 (input) PutObject event Validate Check format Virus scan SQS Decouple steps Buffer bursts Transform Resize/convert Extract metadata SNS Fan-out Notify subscribers S3 (output) DynamoDB DLQ (failures) Each step independently scalable ยท Failures isolated to DLQ ยท No data loss
Pattern 3 โ€” Queue-Based Load Leveling Core

SQS absorbs traffic spikes. Lambda processes at a controlled rate. This protects downstream services (databases, APIs) from being overwhelmed during bursts while ensuring every message is eventually processed.

๐Ÿ“ฌ

How It Works

  • Producers write to SQS at any rate (uncapped)
  • SQS buffers messages (up to 14 days retention)
  • Lambda polls and processes in batches (1-10)
  • Controlled by: batch size, concurrency limit, batch window
  • Failed messages โ†’ retry โ†’ DLQ after max attempts
๐Ÿ›ก๏ธ

Protects Downstream

  • Set reserved concurrency = 10 โ†’ max 10 DB connections
  • Burst of 10,000 messages? SQS queues them safely
  • Lambda processes at steady rate (no DB overload)
  • Visibility timeout = 6ร— Lambda timeout (prevent duplicates)
  • Use FIFO queue when message order matters
Pattern 4 โ€” Serverless Microservices In-Depth

Each microservice is a Lambda function (or group) with its own API, data store, and deployment lifecycle. Services communicate asynchronously via SNS/SQS/EventBridge โ€” not direct invocation.

Pattern 4: Serverless Microservices via EventBridge
EventBridge Bus event routing ยท pattern matching ยท decoupled Order Service API GW + Lambda + DynamoDB Payment Service Lambda + Stripe API Notification Service Lambda + SES/SNS Inventory Service Lambda + DynamoDB order.created โ†’ charge card โ†’ send email โ†’ reserve stock Services know nothing about each other ยท Add/remove subscribers without code changes
Pattern 5 โ€” Scheduled Jobs (Cron) Core

Replace cron servers and scheduled EC2 instances with EventBridge rules triggering Lambda. Zero idle cost between executions โ€” pay only for the seconds your job actually runs.

๐Ÿ“Š

Daily Reports

  • EventBridge: rate(1 day)
  • Query DynamoDB/RDS
  • Generate PDF โ†’ S3
  • Send via SES
๐Ÿงน

Cleanup Jobs

  • EventBridge: rate(1 hour)
  • Delete expired sessions
  • Purge old temp files
  • Rotate logs
๐Ÿ”„

Data Sync

  • EventBridge: rate(5 minutes)
  • Poll external API
  • Update DynamoDB cache
  • Detect changes โ†’ notify
Pattern 6 โ€” Step Functions Orchestration In-Depth

For complex workflows with branching logic, retries, parallel steps, and human approval โ€” use Step Functions to orchestrate multiple Lambda functions. Each function stays simple; the workflow defines the complexity.

๐Ÿ”€

When to Use Step Functions

  • Multi-step workflows with branching/parallel
  • Long-running processes (>15 minutes total)
  • Human approval steps (wait for callback)
  • Complex error handling with retry/catch per step
  • Audit trail: visual execution history
๐Ÿ“‹

Real-World Examples

  • Order fulfillment (validate โ†’ pay โ†’ ship โ†’ notify)
  • User onboarding (create โ†’ verify โ†’ configure โ†’ welcome)
  • ML pipeline (fetch โ†’ preprocess โ†’ train โ†’ evaluate โ†’ deploy)
  • Document processing (extract โ†’ validate โ†’ sign โ†’ store)
Anti-Patterns to Avoid In-Depth
๐Ÿšซ

Lambda Anti-Patterns

  • Monolith Lambda: 250MB function doing everything โ†’ split into focused handlers
  • Lambda calling Lambda (sync): doubled latency + cost. Use SQS/SNS or Step Functions.
  • Long-running loops: processing 10,000 items sequentially โ†’ use SQS batch + fan-out
  • Using Lambda as a cron for heavy ETL: 15-min limit. Use Fargate/Glue instead.
  • VPC for DynamoDB/S3 access: no VPC needed for these. Adding VPC = cost + complexity for nothing.
โœ…

Best Practices

  • Single responsibility: one function, one purpose, one trigger
  • Async by default: use events/queues, not synchronous chains
  • Idempotent handlers: same event processed twice = same result (retries are real)
  • Structured logging: JSON logs with request_id, correlation_id for tracing
  • Infrastructure as Code: SAM, CDK, or Terraform. Never ClickOps Lambda.
Pattern Selection Guide Core
WorkloadPatternKey Services
CRUD APIServerless REST APIAPI Gateway + Lambda + DynamoDB
File processingEvent-driven pipelineS3 โ†’ Lambda โ†’ SQS โ†’ Lambda โ†’ S3
Background jobsQueue-based load levelingSQS โ†’ Lambda (reserved concurrency)
MicroservicesEvent meshEventBridge + Lambda per domain
Periodic tasksScheduled executionEventBridge rule โ†’ Lambda
Multi-step workflowOrchestrationStep Functions + Lambda functions
Real-time streamsStream processingKinesis/DynamoDB Streams โ†’ Lambda
Fan-out notificationsPub/SubSNS โ†’ multiple Lambda subscribers
๐Ÿ“‹ Chapter 7 โ€” Summary
  • REST API: API Gateway + Lambda + DynamoDB. Zero idle cost, ~$5/month for 1M requests.
  • Event pipeline: S3 โ†’ Lambda โ†’ SQS โ†’ Lambda โ†’ output. Steps decoupled, failures isolated.
  • Queue leveling: SQS buffers bursts, Lambda processes at controlled rate. Protects downstream.
  • Microservices: EventBridge routes events between independent Lambda-based services.
  • Cron jobs: EventBridge schedule โ†’ Lambda. Replace EC2 cron servers โ€” $0 idle cost.
  • Step Functions: orchestrate multi-step workflows with branching, parallel, and approval.
  • Anti-patterns: avoid monolith Lambda, sync chains, VPC for public services, 15-min ETL.
  • Best practices: single-responsibility, async-first, idempotent, structured logs, IaC always.
๐ŸŽ“ Exam Tips โ€” Chapter 7

Lambda calling Lambda synchronously = anti-pattern (use SQS/Step Functions) ยท EventBridge for microservice decoupling ยท Step Functions for workflows >15 min ยท SQS visibility timeout must be โ‰ฅ 6ร— Lambda timeout ยท Idempotent functions = mandatory for serverless

Lambda Quick Reference Core
FeatureValue
Compute ModelServerless (event-driven)
ScalingAutomatic (0 โ†’ 1000+ concurrent)
Max Timeout15 minutes
Max Memory10,240 MB (6 vCPU equivalent)
StateStateless (use DynamoDB/S3 for state)
Package Size50 MB zipped / 250 MB unzipped (10 GB container image)
Concurrency Limit1000/region (soft limit, can increase)
BillingPer invocation ($0.20/1M) + per GB-second ($0.0000166667)
Billing Granularity1ms increments (minimum 1ms)
Supported RuntimesNode.js, Python, Java, Go, .NET, Ruby, Custom (AL2023)
VPC SupportOptional โ€” via Hyperplane ENIs in private subnets
Ephemeral Storage512 MB โ€“ 10 GB (/tmp)
Environment Variables4 KB total, encrypted at rest (KMS)
LayersMax 5 per function, 250 MB total unzipped
Async Retries0, 1, or 2 (configurable)
ARM64 Savings20% cheaper per GB-second