|
| 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 |
0 commit comments