Skip to content

Commit 9235b05

Browse files
authored
Merge pull request #2962 from archiev4/archiev4-feature-lambda-durable-function-chaining-terraform
New serverless pattern - lambda-durable-function-chaining-terraform
2 parents e24ce48 + c0d75fc commit 9235b05

8 files changed

Lines changed: 638 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Function chaining with AWS Lambda durable functions in Terraform
2+
3+
This Terraform pattern demonstrates function chaining using AWS Lambda durable functions. A durable orchestrator invokes three Lambda functions in sequence (add, transform and finalize). The output of each step is passed as the input to the next. The durable functions framework automatically checkpoints after every step, so if the orchestrator fails mid-workflow, it replays from the beginning and skips already-completed work. This ensures exactly-once execution without re-processing.
4+
5+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-durable-function-chaining-terraform
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+
* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started) installed
15+
16+
## Deployment Instructions
17+
18+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
19+
```
20+
git clone https://github.com/aws-samples/serverless-patterns
21+
```
22+
1. Change directory to the pattern directory:
23+
```
24+
cd lambda-durable-function-chaining-terraform
25+
```
26+
1. From the command line, initialize terraform to downloads and installs the providers defined in the configuration:
27+
```
28+
terraform init
29+
```
30+
1. From the command line, apply the configuration in the main.tf file:
31+
```
32+
terraform apply -auto-approve
33+
```
34+
1. During the prompts:
35+
#var.aws_region
36+
- Enter a value: {enter the region for deployment}
37+
38+
#var.prefix
39+
- Enter a value: {enter any prefix to associate with resources}
40+
41+
1. Note the outputs from the Terraform deployment process. These contain the resource names and/or ARNs which are used for testing.
42+
43+
## How it works
44+
45+
![architecture](architecture/architecture.png)
46+
47+
This pattern uses a durable orchestrator to chain three Lambda functions in sequence. Each function receives the output of the previous step as its input, forming a pipeline: **Add → Transform → Finalize**.
48+
49+
At each `context.invoke()` call, the durable function creates a checkpoint. If the orchestrator fails at any point, it automatically replays from the beginning but skips already-completed steps using stored results, ensuring no work is repeated. Workflows resume from the last successful checkpoint, recovering automatically without manual intervention.
50+
51+
Consider the following input,
52+
53+
```
54+
Input: {"id": "test-1", "name": "demo", "value": 10}
55+
56+
Step 1 (Add): value = 10 + 10 = 20
57+
Step 2 (Transform): value = 20 × 2 = 40, name = "DEMO"
58+
Step 3 (Finalize): value = 40 + 5 = 45, status = "COMPLETED"
59+
60+
Output: {"id": "test-123", "name": "demo", "step1_completed": true, "step1_value": 20, "step2_completed": true, "step2_value": 40, "transformed_name": "DEMO", "step3_completed": true, "status": "COMPLETED", "final_value": 45}
61+
```
62+
63+
## Testing
64+
65+
1. You can invoke the Lambda function by using the following CLI command:
66+
```
67+
aws lambda invoke --function-name "ORCHESTRATOR_ALIAS_ARN" --payload '{"id": "test-123", "name": "demo", "value": 10}' --cli-binary-format raw-in-base64-out response.json
68+
```
69+
Note: Replace the `ORCHESTRATOR_ALIAS_ARN` with the generated `orchestrator_alias-arn` from Terraform (refer to the Terraform Outputs section)
70+
71+
1. Display the contents of response.json
72+
```
73+
cat response.json
74+
```
75+
76+
Expected output
77+
```
78+
{"id": "test-123", "name": "demo", "step1_completed": true, "step1_value": 20, "step2_completed": true, "step2_value": 40, "transformed_name": "DEMO", "step3_completed": true, "status": "COMPLETED", "final_value": 45}
79+
```
80+
81+
## Cleanup
82+
83+
1. Change directory to the pattern directory:
84+
```
85+
cd serverless-patterns/lambda-durable-function-chaining-terraform
86+
```
87+
88+
1. Delete all created resources
89+
```
90+
terraform destroy -auto-approve
91+
```
92+
93+
1. During the prompts:
94+
```
95+
Enter all details as entered during creation.
96+
```
97+
98+
1. Confirm all created resources has been deleted
99+
```
100+
terraform show
101+
```
102+
----
103+
Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved.
104+
105+
SPDX-License-Identifier: MIT-0
57.7 KB
Loading
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"title": "Function chaining with AWS Lambda durable functions in Terraform",
3+
"description": "A durable orchestrator that chains three Lambda functions in sequence to demonstrate AWS Lambda's durable function",
4+
"language": "Python",
5+
"level": "300",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"A durable orchestrator invokes three Lambda functions in sequence, passing each step's output as input to the next. The durable framework checkpoints after every `context.invoke()` call and if the orchestrator fails, it replays from the beginning but skips completed steps using stored results, ensuring no work is repeated."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-durable-function-chaining-terraform",
16+
"templateURL": "serverless-patterns/lambda-durable-function-chaining-terraform",
17+
"projectFolder": "lambda-durable-function-chaining-terraform",
18+
"templateFile": "main.tf"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "Lambda durable functions",
25+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-functions.html"
26+
},
27+
{
28+
"text": "Build multi-step applications and AI workflows with AWS Lambda durable functions",
29+
"link": "https://aws.amazon.com/blogs/aws/build-multi-step-applications-and-ai-workflows-with-aws-lambda-durable-functions/"
30+
}
31+
]
32+
},
33+
"deploy": {
34+
"text": [
35+
"terraform init",
36+
"terraform apply"
37+
]
38+
},
39+
"testing": {
40+
"text": [
41+
"See the GitHub repo for detailed testing instructions."
42+
]
43+
},
44+
"cleanup": {
45+
"text": [
46+
"terraform destroy",
47+
"terraform show"
48+
]
49+
},
50+
"authors": [
51+
{
52+
"name": "Archana V",
53+
"image": "ttps://media.licdn.com/dms/image/v2/D5603AQGhkVtEhllFEw/profile-displayphoto-shrink_200_200/B56ZZH3LL6H0AY-/0/1744962369852?e=1772668800&v=beta&t=y0t7bsQKJwy5McO395hDmW7QPu_K-a1WKXeTA0-ecno",
54+
"bio": "Solutions Architect at AWS",
55+
"linkedin": "archanavenkat"
56+
}
57+
]
58+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"title": "Function chaining with AWS Lambda durable functions in Terraform",
3+
"description": "A durable orchestrator that chains three Lambda functions in sequence to demonstrate AWS Lambda durable functions",
4+
"language": "Python",
5+
"level": "300",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"A durable orchestrator invokes three Lambda functions in sequence, passing each step's output as input to the next. The durable framework checkpoints after every `context.invoke()` call and if the orchestrator fails, it replays from the beginning but skips completed steps using stored results, ensuring no work is repeated."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-durable-function-chaining-terraform",
16+
"templateURL": "serverless-patterns/lambda-durable-function-chaining-terraform",
17+
"projectFolder": "lambda-durable-function-chaining-terraform",
18+
"templateFile": "main.tf"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "Lambda durable functions",
25+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-functions.html"
26+
},
27+
{
28+
"text": "Build multi-step applications and AI workflows with AWS Lambda durable functions",
29+
"link": "https://aws.amazon.com/blogs/aws/build-multi-step-applications-and-ai-workflows-with-aws-lambda-durable-functions/"
30+
}
31+
]
32+
},
33+
"deploy": {
34+
"text": [
35+
"terraform init",
36+
"terraform apply"
37+
]
38+
},
39+
"testing": {
40+
"text": [
41+
"See the GitHub repo for detailed testing instructions."
42+
]
43+
},
44+
"cleanup": {
45+
"text": [
46+
"terraform destroy",
47+
"terraform show"
48+
]
49+
},
50+
"authors": [
51+
{
52+
"name": "Archana V",
53+
"image": "ttps://media.licdn.com/dms/image/v2/D5603AQGhkVtEhllFEw/profile-displayphoto-shrink_200_200/B56ZZH3LL6H0AY-/0/1744962369852?e=1772668800&v=beta&t=y0t7bsQKJwy5McO395hDmW7QPu_K-a1WKXeTA0-ecno",
54+
"bio": "Solutions Architect at AWS",
55+
"linkedin": "archanavenkat"
56+
}
57+
],
58+
"patternArch": {
59+
"icon1": {
60+
"x": 20,
61+
"y": 50,
62+
"service": "lambda",
63+
"label": "AWS Lambda durable function"
64+
},
65+
"icon2": {
66+
"x": 50,
67+
"y": 20,
68+
"service": "lambda",
69+
"label": "AWS Lambda"
70+
},
71+
"icon3": {
72+
"x": 75,
73+
"y": 45,
74+
"service": "lambda",
75+
"label": "AWS Lambda"
76+
},
77+
"icon4": {
78+
"x": 90,
79+
"y": 75,
80+
"service": "lambda",
81+
"label": "AWS Lambda"
82+
},
83+
"line1": {
84+
"from": "icon1",
85+
"to": "icon2"
86+
},
87+
"line2": {
88+
"from": "icon1",
89+
"to": "icon3"
90+
},
91+
"line3": {
92+
"from": "icon1",
93+
"to": "icon4"
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)