Skip to content

Commit 2bab2ae

Browse files
authored
Merge pull request #2928 from sbbhimji/sbbhimji-feature-lambda-durable-function-chaining-sam
New Serverless pattern - lambda-durable-function-chaining-sam
2 parents 61d53bd + 206afd7 commit 2bab2ae

9 files changed

Lines changed: 460 additions & 0 deletions

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
# Function chaining with AWS Lambda durable functions
2+
3+
This pattern demonstrates the **function chaining** workflow pattern using AWS Lambda durable functions. A durable orchestrator chains three Lambda functions sequentially, with automatic checkpointing after each step for fault tolerance.
4+
5+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-durable-function-chaining-sam
6+
7+
Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.
8+
9+
## Requirements
10+
11+
* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources.
12+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
13+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
14+
* [AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) installed (version 1.141.0+ with DurableConfig support)
15+
* Python 3.13+
16+
17+
## Deployment Instructions
18+
19+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
20+
```bash
21+
git clone https://github.com/aws-samples/serverless-patterns
22+
```
23+
2. Change directory to the pattern directory:
24+
```bash
25+
cd serverless-patterns/lambda-durable-function-chaining-sam
26+
```
27+
3. Build and deploy the application:
28+
```bash
29+
sam build
30+
sam deploy --guided
31+
```
32+
4. During the prompts:
33+
* Enter a stack name
34+
* Enter your preferred AWS Region
35+
* Accept the defaults for remaining options
36+
37+
## How it works
38+
39+
This pattern implements the **function chaining** workflow pattern where a durable orchestrator invokes multiple Lambda functions in sequence, passing the output of each step as input to the next.
40+
41+
### Architecture
42+
43+
```mermaid
44+
flowchart LR
45+
A[Input] --> B[Durable Orchestrator]
46+
B -->|context.invoke| C[Step 1: Add Data]
47+
C -->|checkpoint| B
48+
B -->|context.invoke| D[Step 2: Transform]
49+
D -->|checkpoint| B
50+
B -->|context.invoke| E[Step 3: Finalize]
51+
E -->|checkpoint| B
52+
B --> F[Output]
53+
54+
subgraph Lambda durable function
55+
B
56+
end
57+
58+
subgraph Regular Lambda Functions
59+
C
60+
D
61+
E
62+
end
63+
```
64+
65+
The orchestrator uses `context.invoke()` to chain 3 Lambda functions with automatic checkpointing.
66+
67+
### What are AWS Lambda durable functions?
68+
69+
Lambda durable functions enable you to build resilient, long-running workflows with automatic state management. Key capabilities:
70+
71+
- **Checkpoint/Replay**: Each `context.invoke()` creates a checkpoint. If the function fails, it replays from the beginning but skips completed steps using stored results.
72+
- **Fault Tolerance**: Workflows automatically recover from failures without re-executing completed work.
73+
- **Up to 1 Year Execution**: Durable functions can run for extended periods with waits that don't incur compute charges.
74+
75+
### Data Flow
76+
77+
```
78+
Input: {"id": "test-123", "name": "demo", "value": 5}
79+
80+
Step 1 (Add): value = 5 + 10 = 15
81+
Step 2 (Transform): value = 15 × 2 = 30, name = "DEMO"
82+
Step 3 (Finalize): value = 30 + 5 = 35, status = "COMPLETED"
83+
84+
Output: {"value": 35, "final_value": 40, "transformed_name": "DEMO", "status": "COMPLETED"}
85+
```
86+
87+
### Fault Tolerance Example
88+
89+
If the orchestrator fails after Step 2 completes:
90+
1. Lambda automatically retries the orchestrator
91+
2. During replay, Steps 1 and 2 are **skipped** (results loaded from checkpoints)
92+
3. Step 3 executes normally
93+
4. Workflow completes successfully
94+
95+
## Testing
96+
97+
1. Get the orchestrator alias ARN from stack outputs:
98+
```bash
99+
DURABLE_FUNCTION_ARN=$(aws cloudformation describe-stacks \
100+
--stack-name durable-function-chaining \
101+
--query 'Stacks[0].Outputs[?OutputKey==`OrchestratorAliasArn`].OutputValue' \
102+
--output text)
103+
```
104+
105+
2. Invoke the workflow:
106+
```bash
107+
aws lambda invoke \
108+
--function-name "$DURABLE_FUNCTION_ARN" \
109+
--payload '{"id": "test-123", "name": "demo", "value": 5}' \
110+
--cli-binary-format raw-in-base64-out \
111+
response.json
112+
113+
cat response.json
114+
```
115+
116+
3. Expected output:
117+
```json
118+
{
119+
"workflow": "completed",
120+
"input": {"id": "test-123", "name": "demo", "value": 5},
121+
"output": {
122+
"id": "test-123",
123+
"name": "demo",
124+
"step1_completed": true,
125+
"value": 35,
126+
"step2_completed": true,
127+
"transformed_name": "DEMO",
128+
"step3_completed": true,
129+
"status": "COMPLETED",
130+
"final_value": 40
131+
}
132+
}
133+
```
134+
135+
## Cleanup
136+
137+
```bash
138+
sam delete --stack-name durable-function-chaining
139+
```
140+
141+
## Resources
142+
143+
- [Lambda durable functions Documentation](https://docs.aws.amazon.com/lambda/latest/dg/durable-functions.html)
144+
- [Durable Execution SDK for Python](https://github.com/aws/aws-durable-execution-sdk-python)
145+
- [AWS Blog: Build multi-step applications with Lambda durable functions](https://aws.amazon.com/blogs/aws/build-multi-step-applications-and-ai-workflows-with-aws-lambda-durable-functions/)
146+
147+
----
148+
Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved.
149+
150+
SPDX-License-Identifier: MIT-0
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"title": "Function chaining with AWS Lambda durable functions",
3+
"description": "Demonstrates the function chaining pattern using Lambda durable functions with automatic checkpointing and fault-tolerant sequential execution",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "AWS SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern demonstrates the function chaining pattern using Lambda durable functions.",
11+
"A durable orchestrator function chains three Lambda functions sequentially using context.invoke().",
12+
"Each invocation creates an automatic checkpoint, enabling the workflow to resume from the last successful step after failures.",
13+
"Step 1 (Add) initializes data and adds 10 to the input value.",
14+
"Step 2 (Transform) doubles the value and transforms the name to uppercase.",
15+
"Step 3 (Finalize) adds 5 to the value and sets the completion status.",
16+
"The pattern showcases fault-tolerant workflow orchestration without managing state infrastructure."
17+
]
18+
},
19+
"gitHub": {
20+
"template": {
21+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-durable-function-chaining-sam",
22+
"templateURL": "serverless-patterns/lambda-durable-function-chaining-sam",
23+
"projectFolder": "lambda-durable-function-chaining-sam",
24+
"templateFile": "template.yaml"
25+
}
26+
},
27+
"resources": {
28+
"bullets": [
29+
{
30+
"text": "Lambda durable functions Documentation",
31+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-functions.html"
32+
},
33+
{
34+
"text": "Durable Execution SDK for Python",
35+
"link": "https://github.com/aws/aws-durable-execution-sdk-python"
36+
},
37+
{
38+
"text": "AWS Blog: Build multi-step applications with Lambda durable functions",
39+
"link": "https://aws.amazon.com/blogs/aws/build-multi-step-applications-and-ai-workflows-with-aws-lambda-durable-functions/"
40+
}
41+
]
42+
},
43+
"deploy": {
44+
"text": [
45+
"sam build",
46+
"sam deploy --guided"
47+
]
48+
},
49+
"testing": {
50+
"text": [
51+
"Get the orchestrator alias ARN from stack outputs:",
52+
"<code>DURABLE_FUNCTION_ARN=$(aws cloudformation describe-stacks --stack-name durable-function-chaining --query 'Stacks[0].Outputs[?OutputKey==`OrchestratorAliasArn`].OutputValue' --output text)</code>",
53+
"Invoke the workflow:",
54+
"<code>aws lambda invoke --function-name \"$DURABLE_FUNCTION_ARN\" --payload '{\"id\": \"test-123\", \"name\": \"demo\", \"value\": 5}' --cli-binary-format raw-in-base64-out response.json</code>",
55+
"<code>cat response.json</code>",
56+
"Expected output shows value transformation: 5 → +10 (step1) → ×2 (step2) → +5 (step3) = 35"
57+
]
58+
},
59+
"cleanup": {
60+
"text": [
61+
"Delete the stack: <code>sam delete</code>"
62+
]
63+
},
64+
"authors": [
65+
{
66+
"name": "Sahil Bhimjiani",
67+
"image": "https://drive.google.com/file/d/1E2p7S5UtU36x6Sk1xPS3XnSGJyIUoqK7/view?usp=drivesdk",
68+
"bio": "Sahil Bhimjiani is a Solutions Architect at Amazon Web Services.",
69+
"linkedin": "sahil9701"
70+
},
71+
{
72+
"name": "Anup Rajpara",
73+
"image": "https://drive.google.com/file/d/1MqpPNLCqbU4kvvtTspNXZBqD99aVIJI9/view?usp=sharing",
74+
"bio": "Anup is a Sr. Technical Account Manager at Amazon Web Services. He is passionate about serverless & event-driven architectures.",
75+
"linkedin": "anup-rajpara-developer/"
76+
}
77+
]
78+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"title": "Function chaining with AWS Lambda durable functions",
3+
"description": "Demonstrates the function chaining pattern using Lambda durable functions with automatic checkpointing and fault-tolerant sequential execution",
4+
"language": "Python",
5+
"level": "200",
6+
"framework": "AWS SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern demonstrates the function chaining pattern using Lambda durable functions.",
11+
"A durable orchestrator function chains three Lambda functions sequentially using context.invoke().",
12+
"Each invocation creates an automatic checkpoint, enabling the workflow to resume from the last successful step after failures.",
13+
"Step 1 (Add) initializes data and adds 10 to the input value.",
14+
"Step 2 (Transform) doubles the value and transforms the name to uppercase.",
15+
"Step 3 (Finalize) adds 5 to the value and sets the completion status.",
16+
"The pattern showcases fault-tolerant workflow orchestration without managing state infrastructure."
17+
]
18+
},
19+
"gitHub": {
20+
"template": {
21+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-durable-function-chaining-sam",
22+
"templateURL": "serverless-patterns/lambda-durable-function-chaining-sam",
23+
"projectFolder": "lambda-durable-function-chaining-sam",
24+
"templateFile": "template.yaml"
25+
}
26+
},
27+
"resources": {
28+
"bullets": [
29+
{
30+
"text": "Lambda durable functions Documentation",
31+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-functions.html"
32+
},
33+
{
34+
"text": "Durable Execution SDK for Python",
35+
"link": "https://github.com/aws/aws-durable-execution-sdk-python"
36+
},
37+
{
38+
"text": "AWS Blog: Build multi-step applications with Lambda durable functions",
39+
"link": "https://aws.amazon.com/blogs/aws/build-multi-step-applications-and-ai-workflows-with-aws-lambda-durable-functions/"
40+
}
41+
]
42+
},
43+
"deploy": {
44+
"text": [
45+
"sam build",
46+
"sam deploy --guided"
47+
]
48+
},
49+
"testing": {
50+
"text": [
51+
"Get the orchestrator alias ARN from stack outputs:",
52+
"<code>DURABLE_FUNCTION_ARN=$(aws cloudformation describe-stacks --stack-name durable-function-chaining --query 'Stacks[0].Outputs[?OutputKey==`OrchestratorAliasArn`].OutputValue' --output text)</code>",
53+
"Invoke the workflow:",
54+
"<code>aws lambda invoke --function-name \"$DURABLE_FUNCTION_ARN\" --payload '{\"id\": \"test-123\", \"name\": \"demo\", \"value\": 5}' --cli-binary-format raw-in-base64-out response.json</code>",
55+
"<code>cat response.json</code>",
56+
"Expected output shows value transformation: 5 → +10 (step1) → ×2 (step2) → +5 (step3) = 35"
57+
]
58+
},
59+
"cleanup": {
60+
"text": [
61+
"Delete the stack: <code>sam delete</code>"
62+
]
63+
},
64+
"authors": [
65+
{
66+
"name": "Sahil Bhimjiani",
67+
"image": "https://drive.google.com/file/d/1E2p7S5UtU36x6Sk1xPS3XnSGJyIUoqK7/view?usp=drivesdk",
68+
"bio": "Sahil Bhimjiani is a Solutions Architect at Amazon Web Services.",
69+
"linkedin": "sahil9701"
70+
},
71+
{
72+
"name": "Anup Rajpara",
73+
"image": "https://drive.google.com/file/d/1MqpPNLCqbU4kvvtTspNXZBqD99aVIJI9/view?usp=sharing",
74+
"bio": "Anup is a Sr. Technical Account Manager at Amazon Web Services. He is passionate about serverless & event-driven architectures.",
75+
"linkedin": "anup-rajpara-developer/"
76+
}
77+
],
78+
"patternArch": {
79+
"icon1": {
80+
"x": 25,
81+
"y": 50,
82+
"service": "lambda",
83+
"label": "AWS Lambda durable functions"
84+
},
85+
"icon2": {
86+
"x": 80,
87+
"y": 25,
88+
"service": "lambda",
89+
"label": "AWS Lambda"
90+
},
91+
"icon3": {
92+
"x": 80,
93+
"y": 65,
94+
"service": "lambda",
95+
"label": "AWS Lambda"
96+
},
97+
"line1": {
98+
"from": "icon1",
99+
"to": "icon2",
100+
"label": ""
101+
},
102+
"line2": {
103+
"from": "icon1",
104+
"to": "icon3",
105+
"label": ""
106+
}
107+
}
108+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
from aws_durable_execution_sdk_python import DurableContext, durable_execution
3+
4+
5+
@durable_execution
6+
def lambda_handler(event, context: DurableContext):
7+
"""Durable orchestrator that chains 3 Lambda functions."""
8+
step1_arn = os.environ['STEP1_FUNCTION_ARN']
9+
step2_arn = os.environ['STEP2_FUNCTION_ARN']
10+
step3_arn = os.environ['STEP3_FUNCTION_ARN']
11+
12+
# Step 1: Add initial data
13+
result1 = context.invoke(step1_arn, event, name='step1-add')
14+
15+
# Step 2: Transform data
16+
result2 = context.invoke(step2_arn, result1, name='step2-transform')
17+
18+
# Step 3: Finalize
19+
result3 = context.invoke(step3_arn, result2, name='step3-finalize')
20+
21+
return {
22+
'workflow': 'completed',
23+
'input': event,
24+
'output': result3
25+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
aws-durable-execution-sdk-python
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def lambda_handler(event, context):
2+
"""Step 1: Add initial data fields to the input."""
3+
return {
4+
'id': event.get('id', 'unknown'),
5+
'name': event.get('name', 'default'),
6+
'step1_completed': True,
7+
'value': event.get('value', 0) + 10
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def lambda_handler(event, context):
2+
"""Step 2: Transform and enrich the data."""
3+
return {
4+
**event,
5+
'step2_completed': True,
6+
'value': event.get('value', 0) * 2,
7+
'transformed_name': event.get('name', '').upper()
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def lambda_handler(event, context):
2+
"""Step 3: Finalize the data with status."""
3+
return {
4+
**event,
5+
'step3_completed': True,
6+
'status': 'COMPLETED',
7+
'final_value': event.get('value', 0) + 5
8+
}

0 commit comments

Comments
 (0)