AWS Lambda Serverless
When to Use
- Building event-driven functions with AWS Lambda
- Designing serverless architectures on AWS
- Configuring event source mappings (SQS, DynamoDB Streams, Kinesis)
- Managing Lambda concurrency, cold starts, and provisioned concurrency
- Deploying Lambda@Edge or CloudFront Functions for edge computing
- Preparing for AWS DVA-C02 or SAP-C02 exams
Core Jobs
1. Lambda Triggers (Event Sources)
| Source |
Invocation |
Concurrency |
Ordering |
| API Gateway / ALB |
Synchronous (push) |
Per-request |
N/A |
| S3 |
Asynchronous (push) |
Per-event |
Not guaranteed |
| SNS |
Asynchronous (push) |
Per-message |
Not guaranteed |
| EventBridge |
Asynchronous (push) |
Per-event |
Not guaranteed |
| SQS |
Polling (event source mapping) |
Batch-based |
FIFO within queue |
| DynamoDB Streams |
Polling (event source mapping) |
Per-shard |
Ordered per shard |
| Kinesis Data Streams |
Polling (event source mapping) |
Per-shard |
Ordered per shard |
| Cognito |
Synchronous (trigger type specific) |
Per-event |
N/A |
| Step Functions |
Synchronous or async |
Per-execution |
N/A |
Push model (API GW, S3, SNS): AWS service invokes Lambda directly.
Pull model / Event source mapping (SQS, Kinesis, DynamoDB Streams): Lambda polls the source.
2. Concurrency Management
| Type |
Description |
Use Case |
| Unreserved concurrency |
Shared pool across all functions |
Default; functions share account limit |
| Reserved concurrency |
Guaranteed + maximum cap |
Ensure function always has capacity; prevent throttling other functions |
| Provisioned concurrency |
Pre-initialized execution environments |
Eliminate cold starts for latency-sensitive functions |
Account limit: 1,000 concurrent executions per region by default (can request increase).
Reserved concurrency = 0: effectively disables the function (throttles all invocations).
Concurrency calculation: concurrency = (requests/second) × (average function duration in seconds)
Example: 500 req/s × 0.2s average = 100 concurrent executions needed.
3. Cold Starts
Cold start occurs when:
- No warm execution environment available
- Function not invoked for several minutes (environment recycled)
- Burst traffic exceeds warm environments
Cold start duration by runtime (approximate):
- Java, .NET, Kotlin: 500ms–2s (JVM initialization)
- Python, Node.js, Go, Ruby: 50–200ms (lighter runtimes)
- Custom container: varies (container pull adds overhead)
Cold start mitigations:
- Provisioned concurrency — pre-initialize environments; eliminates cold starts (costs money continuously)
- Smaller deployment package — less code to load; faster init
- Minimize imports/initialization code — move work outside handler to module-level (runs once)
- SnapStart (Java) — snapshot initialized environment; restore on cold start (sub-100ms for Java)
- Keep warm (not recommended) — scheduled pings every 5 minutes; not reliable and wasteful
4. Event Source Mapping Configuration
SQS + Lambda:
- Batch size: 1–10,000 messages
- Batch window: 0–300 seconds (wait to fill batch before invocation)
- Visibility timeout: must be > Lambda function timeout × 6 (to prevent duplicate processing during retries)
- On failure: DLQ on SQS queue or Lambda destination (failure destination)
ReportBatchItemFailures: Lambda reports specific failed messages → only failed items retried (not whole batch)
Kinesis / DynamoDB Streams + Lambda:
- Shard iterator = one Lambda invocation per active shard
BisectBatchOnFunctionError: split batch and retry halves to isolate bad records
DestinationConfig.OnFailure: send failed records to SQS or SNS
MaximumRetryAttempts and MaximumRecordAgeInSeconds: limit retries to prevent infinite loops
ParallelizationFactor: 1–10 concurrent invocations per shard (increase throughput)
5. Lambda Layers
- Package shared libraries and dependencies separately from function code
- Up to 5 layers per function
- Max unzipped size: 250MB (function + all layers combined)
- Layers can be shared across accounts (layer-based sharing for internal libraries)
- Common uses: AWS SDK extensions, pandas/numpy (large ML libraries), custom runtimes
- Not for secrets: use Secrets Manager or Parameter Store; not environment variables or layers
6. Lambda@Edge and CloudFront Functions
| Feature |
Lambda@Edge |
CloudFront Functions |
| Runtime |
Node.js, Python |
JavaScript (ES5) |
| Max execution time |
5s (viewer), 30s (origin) |
1ms |
| Max memory |
128MB–10GB |
2MB |
| Location |
400+ edge locations |
400+ edge locations |
| Trigger points |
Viewer request/response, Origin request/response |
Viewer request/response only |
| Cost |
Per request + duration |
Per request only (cheaper) |
| State |
Stateless |
Stateless |
| Deployment region |
Must deploy in us-east-1 |
Global via CloudFront |
CloudFront Functions = simple transformations (URL rewrite, header manipulation, redirect).
Lambda@Edge = complex logic (auth, A/B testing, geo-routing, dynamic origin selection).
7. Lambda Destinations
- Alternative to try/catch inside Lambda for async invocations
- Configure on-success and on-failure destinations:
- SQS queue, SNS topic, EventBridge bus, another Lambda function
- Provides full invocation record (input + output/error) to destination
- More flexible than DLQ (which only sends failed event payload)
Key Concepts
- Execution environment — isolated container with function code, runtime, layers, and env vars; reused for warm invocations
- Handler — entry point function Lambda invokes (
module.handler for Node.js, module.handler for Python)
- Execution role — IAM role Lambda assumes; defines what AWS services the function can call
- Resource-based policy — policy on the Lambda function itself; controls who can invoke it (other accounts, services)
- Lambda function URL — built-in HTTPS endpoint for Lambda without API Gateway; supports IAM or no auth
- Lambda extensions — lightweight processes running alongside Lambda function (observability agents, secrets caching)
- SnapStart — Java 11+ feature: snapshot initialized state of execution environment for sub-100ms cold starts
- Ephemeral storage /tmp — 512MB to 10GB per execution environment; NOT shared between invocations
Checklist
Output Format
- 🔴 Critical — SQS visibility timeout < Lambda timeout (guarantees duplicate processing); Lambda function consuming full account concurrency (starving other functions); no error handling on async invocations (silent failures)
- 🟡 Warning — Cold starts affecting user-facing API (provisioned concurrency not configured); no DLQ/destination for async function failures; large deployment package (slow cold starts)
- 🟢 Suggestion — Lambda SnapStart for Java functions; Lambda function URL instead of API Gateway for simple use cases; layers for shared dependencies
Exam Tips
- SQS + Lambda: batch size up to 10,000; visibility timeout must be > Lambda timeout (to avoid duplicate processing during retries)
- Provisioned concurrency = eliminates cold starts; costs money even when idle (pre-initialized environments running 24/7)
- Lambda execution role = what Lambda CAN do (outbound AWS API calls); resource policy = who CAN invoke Lambda (inbound)
- Max execution timeout = 15 minutes; for longer tasks use Step Functions + Fargate or ECS tasks
- Lambda@Edge = deployed in us-east-1 ONLY but runs globally at CloudFront edge; CloudFront Functions = cheaper, sub-millisecond, simpler
- Layers = shared dependencies (SDK, pandas); NOT for secrets — use Secrets Manager or Parameter Store instead
- BisectBatchOnFunctionError (Kinesis/DynamoDB Streams): splits failing batch in half to isolate bad records, reducing reprocessing
- ReportBatchItemFailures (SQS): return failed message IDs — only those specific messages are retried, not the whole batch
1---2name: lambda-serverless3description: Use when building Lambda functions, designing serverless architectures, configuring event sources, managing concurrency and cold starts, or setting up Lambda@Edge. Covers AWS DVA-C02 and SAP-C02 serverless domains.4---56# AWS Lambda Serverless78## When to Use9- Building event-driven functions with AWS Lambda10- Designing serverless architectures on AWS11- Configuring event source mappings (SQS, DynamoDB Streams, Kinesis)12- Managing Lambda concurrency, cold starts, and provisioned concurrency13- Deploying Lambda@Edge or CloudFront Functions for edge computing14- Preparing for AWS DVA-C02 or SAP-C02 exams1516## Core Jobs1718### 1. Lambda Triggers (Event Sources)1920| Source | Invocation | Concurrency | Ordering |21|--------|-----------|-------------|---------|22| **API Gateway / ALB** | Synchronous (push) | Per-request | N/A |23| **S3** | Asynchronous (push) | Per-event | Not guaranteed |24| **SNS** | Asynchronous (push) | Per-message | Not guaranteed |25| **EventBridge** | Asynchronous (push) | Per-event | Not guaranteed |26| **SQS** | Polling (event source mapping) | Batch-based | FIFO within queue |27| **DynamoDB Streams** | Polling (event source mapping) | Per-shard | Ordered per shard |28| **Kinesis Data Streams** | Polling (event source mapping) | Per-shard | Ordered per shard |29| **Cognito** | Synchronous (trigger type specific) | Per-event | N/A |30| **Step Functions** | Synchronous or async | Per-execution | N/A |3132**Push model** (API GW, S3, SNS): AWS service invokes Lambda directly.33**Pull model / Event source mapping** (SQS, Kinesis, DynamoDB Streams): Lambda polls the source.3435### 2. Concurrency Management3637| Type | Description | Use Case |38|------|-------------|---------|39| **Unreserved concurrency** | Shared pool across all functions | Default; functions share account limit |40| **Reserved concurrency** | Guaranteed + maximum cap | Ensure function always has capacity; prevent throttling other functions |41| **Provisioned concurrency** | Pre-initialized execution environments | Eliminate cold starts for latency-sensitive functions |4243**Account limit**: 1,000 concurrent executions per region by default (can request increase).44**Reserved concurrency = 0**: effectively disables the function (throttles all invocations).4546**Concurrency calculation**: `concurrency = (requests/second) × (average function duration in seconds)`47Example: 500 req/s × 0.2s average = 100 concurrent executions needed.4849### 3. Cold Starts5051**Cold start occurs when**:52- No warm execution environment available53- Function not invoked for several minutes (environment recycled)54- Burst traffic exceeds warm environments5556**Cold start duration by runtime** (approximate):57- Java, .NET, Kotlin: 500ms–2s (JVM initialization)58- Python, Node.js, Go, Ruby: 50–200ms (lighter runtimes)59- Custom container: varies (container pull adds overhead)6061**Cold start mitigations**:621. **Provisioned concurrency** — pre-initialize environments; eliminates cold starts (costs money continuously)632. **Smaller deployment package** — less code to load; faster init643. **Minimize imports/initialization code** — move work outside handler to module-level (runs once)654. **SnapStart (Java)** — snapshot initialized environment; restore on cold start (sub-100ms for Java)665. **Keep warm (not recommended)** — scheduled pings every 5 minutes; not reliable and wasteful6768### 4. Event Source Mapping Configuration6970**SQS + Lambda**:71- Batch size: 1–10,000 messages72- Batch window: 0–300 seconds (wait to fill batch before invocation)73- Visibility timeout: must be > Lambda function timeout × 6 (to prevent duplicate processing during retries)74- On failure: DLQ on SQS queue or Lambda destination (failure destination)75- `ReportBatchItemFailures`: Lambda reports specific failed messages → only failed items retried (not whole batch)7677**Kinesis / DynamoDB Streams + Lambda**:78- Shard iterator = one Lambda invocation per active shard79- `BisectBatchOnFunctionError`: split batch and retry halves to isolate bad records80- `DestinationConfig.OnFailure`: send failed records to SQS or SNS81- `MaximumRetryAttempts` and `MaximumRecordAgeInSeconds`: limit retries to prevent infinite loops82- `ParallelizationFactor`: 1–10 concurrent invocations per shard (increase throughput)8384### 5. Lambda Layers8586- Package shared libraries and dependencies separately from function code87- Up to 5 layers per function88- Max unzipped size: 250MB (function + all layers combined)89- Layers can be shared across accounts (layer-based sharing for internal libraries)90- Common uses: AWS SDK extensions, pandas/numpy (large ML libraries), custom runtimes91- **Not for secrets**: use Secrets Manager or Parameter Store; not environment variables or layers9293### 6. Lambda@Edge and CloudFront Functions9495| Feature | Lambda@Edge | CloudFront Functions |96|---------|------------|---------------------|97| Runtime | Node.js, Python | JavaScript (ES5) |98| Max execution time | 5s (viewer), 30s (origin) | 1ms |99| Max memory | 128MB–10GB | 2MB |100| Location | 400+ edge locations | 400+ edge locations |101| Trigger points | Viewer request/response, Origin request/response | Viewer request/response only |102| Cost | Per request + duration | Per request only (cheaper) |103| State | Stateless | Stateless |104| Deployment region | Must deploy in us-east-1 | Global via CloudFront |105106**CloudFront Functions** = simple transformations (URL rewrite, header manipulation, redirect).107**Lambda@Edge** = complex logic (auth, A/B testing, geo-routing, dynamic origin selection).108109### 7. Lambda Destinations110111- Alternative to try/catch inside Lambda for async invocations112- Configure on-success and on-failure destinations:113 - SQS queue, SNS topic, EventBridge bus, another Lambda function114- Provides full invocation record (input + output/error) to destination115- More flexible than DLQ (which only sends failed event payload)116117## Key Concepts118119- **Execution environment** — isolated container with function code, runtime, layers, and env vars; reused for warm invocations120- **Handler** — entry point function Lambda invokes (`module.handler` for Node.js, `module.handler` for Python)121- **Execution role** — IAM role Lambda assumes; defines what AWS services the function can call122- **Resource-based policy** — policy on the Lambda function itself; controls who can invoke it (other accounts, services)123- **Lambda function URL** — built-in HTTPS endpoint for Lambda without API Gateway; supports IAM or no auth124- **Lambda extensions** — lightweight processes running alongside Lambda function (observability agents, secrets caching)125- **SnapStart** — Java 11+ feature: snapshot initialized state of execution environment for sub-100ms cold starts126- **Ephemeral storage /tmp** — 512MB to 10GB per execution environment; NOT shared between invocations127128## Checklist129130- [ ] SQS visibility timeout set to > Lambda timeout × 6 (prevent duplicate processing)?131- [ ] Reserved concurrency set to prevent one function from consuming all account concurrency?132- [ ] Provisioned concurrency configured for latency-sensitive APIs?133- [ ] Initialization code (clients, connections) outside handler function (module-level)?134- [ ] ReportBatchItemFailures enabled for SQS event source (partial batch success)?135- [ ] Lambda destinations configured for async invocations (on-success and on-failure)?136- [ ] Layers used for shared dependencies (not duplicated across functions)?137- [ ] Lambda execution role follows least-privilege principle?138139## Output Format140141- 🔴 **Critical** — SQS visibility timeout < Lambda timeout (guarantees duplicate processing); Lambda function consuming full account concurrency (starving other functions); no error handling on async invocations (silent failures)142- 🟡 **Warning** — Cold starts affecting user-facing API (provisioned concurrency not configured); no DLQ/destination for async function failures; large deployment package (slow cold starts)143- 🟢 **Suggestion** — Lambda SnapStart for Java functions; Lambda function URL instead of API Gateway for simple use cases; layers for shared dependencies144145## Exam Tips146147- **SQS + Lambda**: batch size up to 10,000; visibility timeout must be > Lambda timeout (to avoid duplicate processing during retries)148- **Provisioned concurrency** = eliminates cold starts; costs money even when idle (pre-initialized environments running 24/7)149- **Lambda execution role** = what Lambda CAN do (outbound AWS API calls); **resource policy** = who CAN invoke Lambda (inbound)150- **Max execution timeout = 15 minutes**; for longer tasks use Step Functions + Fargate or ECS tasks151- **Lambda@Edge** = deployed in us-east-1 ONLY but runs globally at CloudFront edge; **CloudFront Functions** = cheaper, sub-millisecond, simpler152- **Layers = shared dependencies** (SDK, pandas); NOT for secrets — use Secrets Manager or Parameter Store instead153- **BisectBatchOnFunctionError** (Kinesis/DynamoDB Streams): splits failing batch in half to isolate bad records, reducing reprocessing154- **ReportBatchItemFailures** (SQS): return failed message IDs — only those specific messages are retried, not the whole batch