Skip to content

Commit d5fe7c3

Browse files
docs: adding new Lambda features
1 parent 139107a commit d5fe7c3

27 files changed

+737
-0
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
---
2+
title: Durable Functions
3+
description: Using Powertools for AWS Lambda (Python) with Lambda Durable Functions
4+
---
5+
6+
<!-- markdownlint-disable MD043 -->
7+
8+
[Lambda Durable Functions](https://docs.aws.amazon.com/lambda/latest/dg/durable-functions.html){target="_blank" rel="nofollow"} enable you to build resilient multi-step workflows that can execute for up to one year. They use checkpoints to track progress and automatically recover from failures through replay.
9+
10+
## Key concepts
11+
12+
| Concept | Description |
13+
| --------------------- | ------------------------------------------------------------------ |
14+
| **Durable execution** | Complete lifecycle of a durable function, from start to completion |
15+
| **Checkpoint** | Saved state that tracks progress through the workflow |
16+
| **Replay** | Re-execution from the beginning, skipping completed checkpoints |
17+
| **Steps** | Business logic with built-in retries and progress tracking |
18+
| **Waits** | Suspend execution without incurring compute charges |
19+
20+
## How it works
21+
22+
Durable functions use a **checkpoint/replay mechanism**:
23+
24+
1. Your code runs from the beginning
25+
2. Completed operations are skipped using stored results
26+
3. Execution continues from where it left off
27+
4. State is automatically managed by the SDK
28+
29+
## Powertools integration
30+
31+
Powertools for AWS Lambda (Python) works seamlessly with Durable Functions. The [Durable Execution SDK](https://github.com/aws/aws-durable-execution-sdk-python){target="_blank" rel="nofollow"} has native integration with Powertools Logger via `context.set_logger()`.
32+
33+
???+ note "Found an issue?"
34+
If you encounter any issues using Powertools for AWS with Durable Functions, please [open an issue](https://github.com/aws-powertools/powertools-lambda-python/issues/new?template=bug_report.yml){target="_blank"}.
35+
36+
### Logger
37+
38+
The Durable Execution SDK provides a `context.logger` that automatically handles **log deduplication during replays**. You can integrate Powertools Logger to get structured JSON logging while keeping the deduplication benefits.
39+
40+
#### Using Powertools Logger with context.set_logger
41+
42+
For the best experience, set the Powertools Logger on the durable context:
43+
44+
```python hl_lines="5 10" title="Integrating Powertools Logger with Durable Functions"
45+
--8<-- "examples/lambda_features/durable_functions/src/using_logger.py"
46+
```
47+
48+
This gives you:
49+
50+
- **JSON structured logging** from Powertools for AWS
51+
- **Log deduplication** during replays (logs from completed operations don't repeat)
52+
- **Automatic SDK enrichment** (execution_arn, parent_id, name, attempt)
53+
- **Lambda context injection** (request_id, function_name, etc.)
54+
55+
#### Log deduplication during replay
56+
57+
When you use `context.logger`, the SDK prevents duplicate logs during replays:
58+
59+
```python title="Log deduplication behavior"
60+
--8<-- "examples/lambda_features/durable_functions/src/log_deduplication.py"
61+
```
62+
63+
???+ warning "Direct logger usage"
64+
If you use the Powertools Logger directly (not through `context.logger`), logs will be emitted on every replay:
65+
66+
```python
67+
# Logs will duplicate during replays
68+
logger.info("This appears on every replay")
69+
70+
# Use context.logger instead for deduplication
71+
context.logger.info("This appears only once")
72+
```
73+
74+
### Tracer
75+
76+
Tracer works with Durable Functions. Each execution creates trace segments.
77+
78+
???+ note "Trace continuity"
79+
Due to the replay mechanism, traces may not show a continuous flow. Each execution (including replays) creates separate trace segments. Use the `execution_arn` to correlate traces.
80+
81+
```python hl_lines="5 9" title="Using Tracer with Durable Functions"
82+
--8<-- "examples/lambda_features/durable_functions/src/using_tracer.py"
83+
```
84+
85+
### Metrics
86+
87+
Metrics work with Durable Functions, but be aware that **metrics may be emitted multiple times** during replay if not handled carefully.
88+
89+
```python hl_lines="6 10 21" title="Using Metrics with Durable Functions"
90+
--8<-- "examples/lambda_features/durable_functions/src/using_metrics.py"
91+
```
92+
93+
???+ tip "Accurate metrics"
94+
Emit metrics at workflow completion rather than during intermediate steps to avoid counting replays as new executions.
95+
96+
### Idempotency
97+
98+
The `@idempotent` decorator integrates with Durable Functions and is **replay-aware**. It's useful for protecting the Lambda handler entry point, especially for Event Source Mapping (ESM) invocations like SQS, Kinesis, or DynamoDB Streams.
99+
100+
```python hl_lines="9 15" title="Using Idempotency with Durable Functions"
101+
--8<-- "examples/lambda_features/durable_functions/src/using_idempotency.py"
102+
```
103+
104+
**When to use Powertools Idempotency:**
105+
106+
- Protecting the Lambda handler entry point from duplicate invocations
107+
- Methods you don't want to convert into steps but need idempotency guarantees
108+
- Event Source Mapping triggers (SQS, Kinesis, DynamoDB Streams)
109+
110+
**When you don't need it:**
111+
112+
- Steps within a durable function are already idempotent via the checkpoint mechanism
113+
114+
### Parser
115+
116+
Parser works with Durable Functions for validating and parsing event payloads.
117+
118+
```python hl_lines="9 14" title="Using Parser with Durable Functions"
119+
--8<-- "examples/lambda_features/durable_functions/src/using_parser.py"
120+
```
121+
122+
### Parameters
123+
124+
Parameters work normally with Durable Functions.
125+
126+
```python hl_lines="13" title="Using Parameters with Durable Functions"
127+
--8<-- "examples/lambda_features/durable_functions/src/using_parameters.py"
128+
```
129+
130+
???+ note "Parameter freshness"
131+
For long-running workflows (hours/days), parameters fetched at the start may become stale. Consider fetching parameters within steps that need the latest values.
132+
133+
## Best practices
134+
135+
### Use context.logger for log deduplication
136+
137+
Always use `context.set_logger()` and `context.logger` instead of using the Powertools Logger directly. This ensures logs are deduplicated during replays.
138+
139+
```python title="Recommended logging pattern"
140+
--8<-- "examples/lambda_features/durable_functions/src/best_practice_logging.py"
141+
```
142+
143+
### Emit metrics at workflow completion
144+
145+
To avoid counting replays as new executions, emit metrics only when the workflow completes successfully.
146+
147+
```python title="Metrics at completion"
148+
--8<-- "examples/lambda_features/durable_functions/src/best_practice_metrics.py"
149+
```
150+
151+
### Use Idempotency for ESM triggers
152+
153+
When your durable function is triggered by Event Source Mappings (SQS, Kinesis, DynamoDB Streams), use the `@idempotent` decorator to protect against duplicate invocations.
154+
155+
```python title="Idempotency for ESM"
156+
--8<-- "examples/lambda_features/durable_functions/src/best_practice_idempotency.py"
157+
```
158+
159+
## FAQ
160+
161+
### Do I need Idempotency utility with Durable Functions?
162+
163+
It depends on your use case. Steps within a durable function are already idempotent via checkpoints. However, the `@idempotent` decorator is useful for protecting the Lambda handler entry point, especially for Event Source Mapping invocations (SQS, Kinesis, DynamoDB Streams) where the same event might trigger multiple invocations.
164+
165+
### Why do I see duplicate logs?
166+
167+
If you're using the logger directly instead of `context.logger`, logs will be emitted on every replay. Use `context.set_logger(logger)` and then `context.logger.info()` to get automatic log deduplication.
168+
169+
### How do I correlate logs across replays?
170+
171+
Use the `execution_arn` field that's automatically added to every log entry when using `context.logger`:
172+
173+
```sql
174+
fields @timestamp, @message, execution_arn
175+
| filter execution_arn = "arn:aws:lambda:us-east-1:123456789012:function:my-function:execution-id"
176+
| sort @timestamp asc
177+
```
178+
179+
### Can I use Tracer with Durable Functions?
180+
181+
Yes, but be aware that each execution (including replays) creates separate trace segments. Use the `execution_arn` as a correlation identifier for end-to-end visibility.
182+
183+
### How should I emit metrics without duplicates?
184+
185+
Emit metrics at workflow completion rather than during intermediate steps. This ensures you count completed workflows, not replay attempts.

docs/lambda-features/index.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Lambda Features
3+
description: Using Powertools with advanced Lambda features
4+
---
5+
6+
<!-- markdownlint-disable MD043 -->
7+
8+
This section covers how to use Powertools for AWS Lambda (Python) with advanced Lambda features like Lambda Managed Instances and Durable Functions.
9+
10+
<div class="grid cards" markdown>
11+
12+
- :material-server:{ .lg .middle } __Lambda Managed Instances__
13+
14+
---
15+
16+
Run Lambda functions on EC2 instances with multi-concurrent invocations
17+
18+
[:octicons-arrow-right-24: Getting started](./managed-instances.md)
19+
20+
- :material-state-machine:{ .lg .middle } __Durable Functions__
21+
22+
---
23+
24+
Build resilient multi-step workflows that can execute for up to one year
25+
26+
[:octicons-arrow-right-24: Getting started](./durable-functions.md)
27+
28+
</div>
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
---
2+
title: Lambda Managed Instances
3+
description: Using Powertools for AWS Lambda (Python) with Lambda Managed Instances
4+
---
5+
6+
<!-- markdownlint-disable MD043 -->
7+
8+
[Lambda Managed Instances](https://docs.aws.amazon.com/lambda/latest/dg/lambda-managed-instances.html){target="_blank" rel="nofollow"} enables you to run Lambda functions on Amazon EC2 instances without managing infrastructure. It supports multi-concurrent invocations, EC2 pricing models, and specialized compute options like Graviton4.
9+
10+
## Key differences from Lambda (default)
11+
12+
| Aspect | Lambda (default) | Lambda Managed Instances |
13+
| ---------------- | ------------------------------------------- | ----------------------------------------------- |
14+
| **Concurrency** | Single invocation per execution environment | Multiple concurrent invocations per environment |
15+
| **Python model** | One process, one request | Multiple processes, one request each |
16+
| **Pricing** | Per-request duration | EC2-based with Savings Plans support |
17+
| **Scaling** | Scale on demand with cold starts | Async scaling based on CPU, no cold starts |
18+
| **Isolation** | Firecracker microVMs | Containers on EC2 Nitro |
19+
20+
## How Lambda Python runtime handles concurrency
21+
22+
Unlike Java or Node.js which use threads, the **Lambda Python runtime uses multiple processes** for concurrent requests. Each request runs in a separate process, which provides natural isolation between requests.
23+
24+
This means:
25+
26+
- **Memory is not shared** between concurrent requests
27+
- **Global variables** are isolated per process
28+
- **`/tmp` directory is shared** across all processes - use caution with file operations
29+
30+
## Isolation model
31+
32+
Lambda Managed Instances use a different isolation model than Lambda (default):
33+
34+
| Layer | Lambda (default) | Lambda Managed Instances |
35+
| ---------------------- | ---------------------------------------- | ------------------------------------------ |
36+
| **Instance level** | Firecracker microVMs on shared AWS fleet | Containers on EC2 Nitro in your account |
37+
| **Security boundary** | Execution environment | Capacity provider |
38+
| **Function isolation** | Strong isolation via microVMs | Container-based isolation within instances |
39+
40+
**Capacity providers** serve as the security boundary. Functions within the same capacity provider share the underlying EC2 instances. For workloads requiring strong isolation between functions, use separate capacity providers.
41+
42+
For Python specifically, the multi-process model adds another layer of isolation - each concurrent request runs in its own process with separate memory space.
43+
44+
## Powertools integration
45+
46+
Powertools for AWS Lambda (Python) works seamlessly with Lambda Managed Instances. All utilities are compatible with the multi-process concurrency model used by Python.
47+
48+
### Logger
49+
50+
Logger works without any changes. Each process has its own logger instance.
51+
52+
```python hl_lines="4 7" title="Using Logger with Managed Instances"
53+
--8<-- "examples/lambda_features/managed_instances/src/using_logger.py"
54+
```
55+
56+
### Tracer
57+
58+
Tracer works without any changes. X-Ray traces are captured per request.
59+
60+
???+ note "VPC connectivity required"
61+
Lambda Managed Instances run in your VPC. Ensure you have [network connectivity](https://docs.aws.amazon.com/lambda/latest/dg/lambda-managed-instances-networking.html){target="_blank" rel="nofollow"} to send traces to X-Ray.
62+
63+
```python hl_lines="4 8 12" title="Using Tracer with Managed Instances"
64+
--8<-- "examples/lambda_features/managed_instances/src/using_tracer.py"
65+
```
66+
67+
### Metrics
68+
69+
Metrics work without any changes. Each process flushes metrics independently.
70+
71+
???+ note "VPC connectivity required"
72+
Ensure you have [network connectivity](https://docs.aws.amazon.com/lambda/latest/dg/lambda-managed-instances-networking.html){target="_blank" rel="nofollow"} to send metrics to CloudWatch.
73+
74+
```python hl_lines="5 9 12" title="Using Metrics with Managed Instances"
75+
--8<-- "examples/lambda_features/managed_instances/src/using_metrics.py"
76+
```
77+
78+
### Parameters
79+
80+
Parameters utility works correctly, but be aware that **cache is per-process**.
81+
82+
```python hl_lines="9" title="Using Parameters with Managed Instances"
83+
--8<-- "examples/lambda_features/managed_instances/src/using_parameters.py"
84+
```
85+
86+
???+ tip "Cache behavior"
87+
Since each process has its own cache, you might see more calls to SSM/Secrets Manager during initial warm-up. Once each process has cached the value, subsequent requests within that process use the cache.
88+
89+
### Idempotency
90+
91+
Idempotency works without any changes. It uses DynamoDB for state management, which is external to the process.
92+
93+
```python hl_lines="7 10" title="Using Idempotency with Managed Instances"
94+
--8<-- "examples/lambda_features/managed_instances/src/using_idempotency.py"
95+
```
96+
97+
### Batch Processing
98+
99+
Batch Processing works without any changes. Each batch is processed within a single process.
100+
101+
```python hl_lines="5 8 14" title="Using Batch Processing with Managed Instances"
102+
--8<-- "examples/lambda_features/managed_instances/src/using_batch.py"
103+
```
104+
105+
???+ note "Other utilities"
106+
All other Powertools for AWS utilities (Feature Flags, Validation, Parser, Data Masking, etc.) work without any changes. If you encounter any issues, please [open an issue](https://github.com/aws-powertools/powertools-lambda-python/issues/new?template=bug_report.yml){target="_blank"}.
107+
108+
## Working with shared resources
109+
110+
### The `/tmp` directory
111+
112+
The `/tmp` directory is **shared across all processes** in the execution environment. Use caution when writing files.
113+
114+
```python title="Safe file handling with unique names"
115+
--8<-- "examples/lambda_features/managed_instances/src/tmp_file_handling.py"
116+
```
117+
118+
### Database connections
119+
120+
Since each process is independent, connection pooling behaves differently than in threaded runtimes.
121+
122+
```python title="Database connections per process"
123+
--8<-- "examples/lambda_features/managed_instances/src/database_connections.py"
124+
```
125+
126+
## VPC connectivity
127+
128+
Lambda Managed Instances require VPC configuration for:
129+
130+
- Sending logs to CloudWatch Logs
131+
- Sending traces to X-Ray
132+
- Accessing AWS services (SSM, Secrets Manager, DynamoDB, etc.)
133+
134+
Configure connectivity using one of these options:
135+
136+
1. **VPC Endpoints** - Private connectivity without internet access
137+
2. **NAT Gateway** - Internet access from private subnets
138+
3. **Public subnet with Internet Gateway** - Direct internet access
139+
140+
See [Networking for Lambda Managed Instances](https://docs.aws.amazon.com/lambda/latest/dg/lambda-managed-instances-networking.html){target="_blank" rel="nofollow"} for detailed setup instructions.
141+
142+
## FAQ
143+
144+
### Does Powertools for AWS Lambda (Python) work with Lambda Managed Instances?
145+
146+
Yes, all Powertools for AWS utilities work seamlessly with Lambda Managed Instances. The multi-process model in Python provides natural isolation between concurrent requests.
147+
148+
### Is my code thread-safe?
149+
150+
For Python, you don't need to worry about thread safety because Lambda Managed Instances uses **multiple processes**, not threads. Each request runs in its own process with isolated memory.
151+
152+
### Why is my cache not shared between requests?
153+
154+
Each process maintains its own cache (for Parameters, Feature Flags, etc.). This is expected behavior. The cache will warm up independently per process, which may result in slightly more calls to backend services during initial warm-up.
155+
156+
### Can I use global variables?
157+
158+
Yes, but remember they are **per-process**, not shared across concurrent requests. This is actually safer than shared state.
159+
160+
### How should I handle files in `/tmp`?
161+
162+
Use unique file names (include request ID or UUID) to avoid conflicts between concurrent requests. Always clean up files after use to avoid filling the shared `/tmp` directory.
163+
164+
### Do I need to change my existing Powertools for AWS code?
165+
166+
No changes are required. Your existing code will work as-is with Lambda Managed Instances.

examples/lambda_features/__init__.py

Whitespace-only changes.

examples/lambda_features/durable_functions/__init__.py

Whitespace-only changes.

examples/lambda_features/durable_functions/src/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)