Skip to content

Commit 4226d60

Browse files
authored
Merge pull request #2943 from anusha-amz/agganapu-feature-durable-lambda-eventbridge-cron-nodejs-sam
New Serverless pattern - lambda-durable-eventbridge-cron-nodejs-sam
2 parents ca711ce + 0127830 commit 4226d60

10 files changed

Lines changed: 500 additions & 0 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Amazon EventBridge cron to AWS Lambda durable function
2+
3+
This pattern demonstrates how to trigger a Lambda durable function using Amazon EventBridge on a cron schedule. The Lambda function uses the AWS durable execution SDK to implement a multi-step workflow with checkpointing and automatic replay capabilities.
4+
5+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-durable-eventbridge-cron-nodejs-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+
## Architecture
10+
11+
This architecture consists of a serverless cron job implementation using Amazon EventBridge and Lambda durable functions. An Amazon EventBridge rule configured with a cron expression triggers the Lambda durable function every 5-minutes. The Lambda function uses the AWS durable execution SDK to implement a multi-step workflow that can span multiple invocations through checkpointing - when `context.wait()` is called, the function suspends execution and creates a checkpoint, then resumes from that point in a subsequent invocation without re-executing previous steps.
12+
13+
## Requirements
14+
15+
* [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.
16+
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
17+
* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
18+
* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
19+
20+
## Deployment Instructions
21+
22+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
23+
```
24+
git clone https://github.com/aws-samples/serverless-patterns
25+
```
26+
1. Change directory to the pattern directory:
27+
```
28+
cd lambda-durable-eventbridge-cron-nodejs-sam
29+
```
30+
1. From the command line, use AWS SAM to build and deploy the AWS resources for the pattern as specified in the template.yaml file:
31+
```
32+
sam build
33+
sam deploy --guided
34+
```
35+
1. During the prompts:
36+
* Enter a stack name
37+
* Enter the desired AWS Region
38+
* Allow SAM CLI to create IAM roles with the required permissions.
39+
40+
Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults.
41+
42+
1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing.
43+
44+
## How it works
45+
46+
This pattern creates:
47+
48+
1. **Durable Orchestrator Lambda Function**: A Node.js 24.x Lambda function that uses the AWS durable execution SDK to implement a multi-step workflow (invoking 2 Lambda functions) with automatic checkpointing and replay capabilities.
49+
50+
2. **Data Processor Lambda Function**: An activity function that simulates processing records.
51+
52+
3. **Notification Service Lambda Function**: A final step that simulates sending a summary once processing is complete.
53+
54+
4. **Amazon EventBridge Cron Rule**: An Amazon EventBridge rule configured with `rate(5 minutes)` that triggers the Lambda function every 5-minutes.
55+
56+
5. **Function Versioning**: The Lambda function uses `AutoPublishAlias: prod` to automatically publish a new version on each deployment and point the `prod` alias to it.
57+
58+
6. **Targeted Invocation**: The Amazon EventBridge rule specifically targets the published version via the alias as it is a best practice to use numbered versions or aliases for production durable functions rather than $LATEST.
59+
60+
### Durable Execution Flow
61+
62+
The Lambda function implements a durable workflow with three steps:
63+
64+
1. **Data Processing Step**: Uses `context.invoke()` to call DataProcessor Lambda function with automatic checkpointing
65+
2. **Wait Period**: Suspends execution for 10 seconds using `context.wait()` - no compute costs during wait
66+
3. **Notification Service Processing**: Uses `context.invoke()` to call NotificationService Lambda function and returns results
67+
68+
**Execution Pattern**:
69+
- **Invocation 1**: `context.invoke("DataProcessorFunction", ...)` runs → checkpoint created → `context.wait()` suspends execution
70+
- **Invocation 2**: DataProcessor replays from checkpoint (no re-execution) → wait completes → `context.invoke("NotificationServiceFunction", ...)` runs → workflow completes
71+
72+
This demonstrates how durable functions can span multiple Lambda invocations while maintaining state and avoiding redundant work through checkpointing.
73+
74+
## Testing
75+
76+
1. After deployment, the EventBridge rule will automatically trigger the Lambda function every 5-minutes.
77+
78+
2. Monitor the function execution in CloudWatch Logs:
79+
```bash
80+
aws logs tail /aws/lambda/DurableOrchestratorFunction --follow
81+
```
82+
83+
3. You should observe the durable execution pattern:
84+
- First invocation: "DataProcessorStep..." followed by suspension
85+
- Second invocation: "NotificationServiceStep..." (DataProcessorStep skipped due to checkpoint)
86+
87+
4. You can also see the durable execution section in the Lambda function console to get a detailed overview of each execution step in the execution.
88+
89+
## Cleanup
90+
91+
1. Delete the stack
92+
```bash
93+
sam delete
94+
```
95+
----
96+
Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved.
97+
98+
SPDX-License-Identifier: MIT-0
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// First Lambda function in the sequence
2+
export const handler = async (event) => {
3+
console.log('Data Processor invoked with:', JSON.stringify(event, null, 2));
4+
5+
const { executionId, triggerTime, task } = event;
6+
7+
// Simulate data processing
8+
const processedRecords = Math.floor(Math.random() * 1000) + 500;
9+
10+
await new Promise(resolve => setTimeout(resolve, 1000)); // Simulate processing time
11+
12+
const result = {
13+
functionName: 'DataProcessorFunction',
14+
executionId,
15+
triggerTime,
16+
task,
17+
recordsProcessed: processedRecords,
18+
status: 'success',
19+
processedAt: new Date().toISOString()
20+
};
21+
22+
console.log('Data processing completed:', result);
23+
return result;
24+
};
25+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "data-processor-lambda",
3+
"version": "1.0.0",
4+
"description": "Simple data processor service",
5+
"main": "index.js",
6+
"type": "module",
7+
"keywords": [
8+
"aws",
9+
"lambda"
10+
],
11+
"author": "",
12+
"license": "MIT"
13+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Main durable function that orchestrates two Lambda functions in sequence
2+
3+
import { withDurableExecution } from "@aws/durable-execution-sdk-js";
4+
5+
export const handler = withDurableExecution(
6+
async (event, context) => {
7+
8+
const executionId = event.id || 'unknown';
9+
const triggerTime = event.time || new Date().toISOString();
10+
11+
// Step 1: Invoke first Lambda function (data processing)
12+
const step1Result = await context.invoke("DataProcessorFunction", {
13+
executionId,
14+
triggerTime,
15+
task: "process_data_5minutes"
16+
});
17+
console.log(`[${executionId}] DataProcessorStep completed`);
18+
19+
//Lambda will stop executing here and restart in 10 seconds
20+
await context.wait({seconds: 10});
21+
22+
// Step 2: Invoke second Lambda function (notification service)
23+
const step2Result = await context.invoke("NotificationServiceFunction", {
24+
executionId,
25+
triggerTime,
26+
previousStepResult: step1Result,
27+
task: "send_completion_notification",
28+
});
29+
console.log(`[${executionId}] NotificationServiceStep completed`);
30+
31+
console.log(`[${executionId}] DurableFunctionExecutionCompleted`);
32+
33+
// Return final result
34+
return {
35+
status: 'completed',
36+
executionId,
37+
triggerTime,
38+
steps: {
39+
dataProcessor: step1Result,
40+
notificationService: step2Result
41+
},
42+
completedAt: new Date().toISOString()
43+
};
44+
45+
}
46+
);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "nodejs-durable-order-processor",
3+
"version": "1.0.0",
4+
"description": "AWS Lambda durable functions - Node.js orchestrator",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"type": "module",
10+
"keywords": [
11+
"aws",
12+
"lambda",
13+
"durable",
14+
"orchestrator"
15+
],
16+
"author": "",
17+
"license": "MIT",
18+
"dependencies": {
19+
"@aws/durable-execution-sdk-js": "^1.0.2"
20+
}
21+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
{
2+
"title": "Amazon EventBridge Cron to Lambda durable function",
3+
"description": "Create a Lambda durable function triggered by Amazon EventBridge on a cron schedule using AWS SAM.",
4+
"language": "Node.js",
5+
"level": "200",
6+
"framework": "AWS SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This sample project demonstrates how to create a Lambda durable function that is triggered by Amazon EventBridge on a cron schedule. The Lambda function uses the AWS durable execution SDK to implement a multi-step workflow with automatic checkpointing and replay capabilities.",
11+
"The durable execution pattern allows Lambda functions to span multiple invocations while maintaining state. When the function calls context.wait(), it suspends execution and creates a checkpoint. A subsequent invocation resumes from the checkpoint without re-executing previous steps.",
12+
"This pattern deploys a Lambda durable function with Node.js 24 runtime, an Amazon EventBridge rule with cron schedule, and uses function versioning to ensure the cron trigger targets a published version rather than $LATEST."
13+
]
14+
},
15+
"gitHub": {
16+
"template": {
17+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-durable-eventbridge-cron-nodejs-sam",
18+
"templateURL": "serverless-patterns/lambda-durable-eventbridge-cron-nodejs-sam",
19+
"projectFolder": "lambda-durable-eventbridge-cron-nodejs-sam",
20+
"templateFile": "template.yaml"
21+
}
22+
},
23+
"resources": {
24+
"bullets": [
25+
{
26+
"text": "AWS Lambda durable functions",
27+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-functions.html"
28+
},
29+
{
30+
"text": "Invoking AWS Lambda durable functions",
31+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-invoking.html"
32+
},
33+
{
34+
"text": "AWS durable execution SDK for Node.js",
35+
"link": "https://github.com/aws/aws-durable-execution-sdk-js"
36+
}
37+
]
38+
},
39+
"deploy": {
40+
"text": [
41+
"sam build",
42+
"sam deploy --guided"
43+
]
44+
},
45+
"testing": {
46+
"text": [
47+
"See the GitHub repo for detailed testing instructions."
48+
]
49+
},
50+
"cleanup": {
51+
"text": [
52+
"Delete the stack: <code>sam delete</code>."
53+
]
54+
},
55+
"authors": [
56+
{
57+
"name": "Anusha Ganapuram",
58+
"image": "https://avatars.githubusercontent.com/u/58950933",
59+
"bio": "Technical Account Manager at AWS with deep expertise in serverless and event-driven solutions. Passionate about building scalable, secure and distributed applications that help organizations modernize their infrastructure and accelerate innovation.",
60+
"linkedin": "anushaganapuram"
61+
}
62+
]
63+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"title": "Amazon EventBridge Cron to Lambda durable function",
3+
"description": "Create a Lambda durable function triggered by Amazon EventBridge on a cron schedule using AWS SAM.",
4+
"language": "Node.js",
5+
"level": "200",
6+
"framework": "AWS SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This sample project demonstrates how to create a Lambda durable function that is triggered by Amazon EventBridge on a cron schedule. The Lambda function uses the AWS durable execution SDK to implement a multi-step workflow with automatic checkpointing and replay capabilities.",
11+
"The durable execution pattern allows Lambda functions to span multiple invocations while maintaining state. When the function calls context.wait(), it suspends execution and creates a checkpoint. A subsequent invocation resumes from the checkpoint without re-executing previous steps.",
12+
"This pattern deploys a Lambda durable function with Node.js 24 runtime, an Amazon EventBridge rule with cron schedule, and uses function versioning to ensure the cron trigger targets a published version rather than $LATEST."
13+
]
14+
},
15+
"gitHub": {
16+
"template": {
17+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/lambda-durable-eventbridge-cron-nodejs-sam",
18+
"templateURL": "serverless-patterns/lambda-durable-eventbridge-cron-nodejs-sam",
19+
"projectFolder": "lambda-durable-eventbridge-cron-nodejs-sam",
20+
"templateFile": "template.yaml"
21+
}
22+
},
23+
"resources": {
24+
"bullets": [
25+
{
26+
"text": "AWS Lambda durable functions",
27+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-functions.html"
28+
},
29+
{
30+
"text": "Invoking AWS Lambda durable functions",
31+
"link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-invoking.html"
32+
},
33+
{
34+
"text": "AWS durable execution SDK for Node.js",
35+
"link": "https://github.com/aws/aws-durable-execution-sdk-js"
36+
}
37+
]
38+
},
39+
"deploy": {
40+
"text": [
41+
"sam build",
42+
"sam deploy --guided"
43+
]
44+
},
45+
"testing": {
46+
"text": [
47+
"See the GitHub repo for detailed testing instructions."
48+
]
49+
},
50+
"cleanup": {
51+
"text": [
52+
"Delete the stack: <code>sam delete</code>."
53+
]
54+
},
55+
"authors": [
56+
{
57+
"name": "Anusha Ganapuram",
58+
"image": "https://avatars.githubusercontent.com/u/58950933",
59+
"bio": "Technical Account Manager at AWS with deep expertise in serverless and event-driven solutions. Passionate about building scalable, secure and distributed applications that help organizations modernize their infrastructure and accelerate innovation.",
60+
"linkedin": "anushaganapuram"
61+
}
62+
],
63+
"patternArch": {
64+
"icon1": {
65+
"x": 20,
66+
"y": 50,
67+
"service": "eventbridge",
68+
"label": "EventBridge"
69+
},
70+
"icon2": {
71+
"x": 50,
72+
"y": 50,
73+
"service": "lambda",
74+
"label": "Lambda durable function"
75+
},
76+
"icon3": {
77+
"x": 80,
78+
"y": 20,
79+
"service": "lambda",
80+
"label": "DataProcessor Lambda function"
81+
},
82+
"icon4": {
83+
"x": 80,
84+
"y": 70,
85+
"service": "lambda",
86+
"label": "NotificationSvcs Lambda function"
87+
},
88+
"line1": {
89+
"from": "icon1",
90+
"to": "icon2"
91+
},
92+
"line2": {
93+
"from": "icon2",
94+
"to": "icon3"
95+
},
96+
"line3": {
97+
"from": "icon2",
98+
"to": "icon4"
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)