Skip to content

Commit 3797f37

Browse files
committed
addressed PR review comments
1 parent ec03b6a commit 3797f37

5 files changed

Lines changed: 34 additions & 64 deletions

File tree

lambda-durable-eventbridge-cron-nodejs-sam/README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# EventBridge Cron to Durable Lambda Function
1+
# Amazon EventBridge cron to AWS Lambda durable function
22

3-
This pattern demonstrates how to trigger a durable Lambda function using 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.
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.
44

5-
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-durable-eventbrdige-cron-nodejs-sam
5+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/lambda-durable-eventbridge-cron-nodejs-sam
66

77
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.
88

99
## Architecture
1010

11-
This architecture consists of a serverless cron job implementation using EventBridge and durable Lambda functions. An EventBridge rule configured with a cron expression triggers the durable Lambda 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.
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.
1212

1313
## Requirements
1414

@@ -45,29 +45,29 @@ This architecture consists of a serverless cron job implementation using EventBr
4545
4646
This pattern creates:
4747
48-
1. **Durable Orchestrator Lambda Function**: A Nodejs 24.x Lambda function that uses the AWS durable execution SDK to implement a multi-step workflow (invoking 2 Lmabda functions) with automatic checkpointing and replay capabilities.
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.
4949
5050
2. **Data Processor Lambda Function**: An activity function that simulates processing records.
5151
5252
3. **Notification Service Lambda Function**: A final step that simulates sending a summary once processing is complete.
5353
54-
4. **EventBridge Cron Rule**: An EventBridge rule configured with `rate(5 minutes)` that triggers the Lambda function every 5-minutes.
54+
4. **Amazon EventBridge Cron Rule**: An Amazon EventBridge rule configured with `rate(5 minutes)` that triggers the Lambda function every 5-minutes.
5555
5656
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.
5757
58-
6. **Targeted Invocation**: The 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.
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.
5959
6060
### Durable Execution Flow
6161
6262
The Lambda function implements a durable workflow with three steps:
6363
64-
1. **Data Processing Step**: Invokes DataProcessor Lambda function with business logic simulating processing data (checkpointed)
64+
1. **Data Processing Step**: Uses `context.invoke()` to call DataProcessor Lambda function with automatic checkpointing
6565
2. **Wait Period**: Suspends execution for 10 seconds using `context.wait()` - no compute costs during wait
66-
3. **Notification Service Processing**: Invokes NotificationService Lambda function with business logic simulating sending notifications and returns results
66+
3. **Notification Service Processing**: Uses `context.invoke()` to call NotificationService Lambda function and returns results
6767
6868
**Execution Pattern**:
69-
- **Invocation 1**: `invoke-data-processor()` runs → checkpoint created → `context.wait()` suspends execution
70-
- **Invocation 2**: `invoke-data-processor()` replays from checkpoint (no re-execution) → wait completes → `invoke-notification-service()` runs → workflow completes
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
7171
7272
This demonstrates how durable functions can span multiple Lambda invocations while maintaining state and avoiding redundant work through checkpointing.
7373
Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// Main durable function that orchestrates two Lambda functions in sequence
22

3-
import * as durableSDK from "@aws/durable-execution-sdk-js";
4-
const {withDurableExecution, Duration } = durableSDK;
5-
6-
import { LambdaClient, InvokeCommand } from "@aws-sdk/client-lambda";
7-
const lambdaClient = new LambdaClient({});
3+
import { withDurableExecution } from "@aws/durable-execution-sdk-js";
84

95
export const handler = withDurableExecution(
106
async (event, context) => {
@@ -13,51 +9,26 @@ export const handler = withDurableExecution(
139
const triggerTime = event.time || new Date().toISOString();
1410

1511
// Step 1: Invoke first Lambda function (data processing)
16-
const step1Result = await context.step("invoke-data-processor", async () => {
17-
console.log('DataProcessorStep...');
18-
19-
const command = new InvokeCommand({
20-
FunctionName: "DataProcessorFunction",
21-
InvocationType: "RequestResponse",
22-
Payload: JSON.stringify({
23-
executionId,
24-
triggerTime,
25-
task: "process_data_5minutes"
26-
})
27-
});
28-
29-
const response = await lambdaClient.send(command);
30-
const payload = JSON.parse(new TextDecoder().decode(response.Payload));
31-
32-
return payload;
12+
const step1Result = await context.invoke("DataProcessorFunction", {
13+
executionId,
14+
triggerTime,
15+
task: "process_data_5minutes"
3316
});
17+
console.log(`[${executionId}] DataProcessorStep completed`);
3418

3519
//Lambda will stop executing here and restart in 10 seconds
3620
await context.wait({seconds: 10});
3721

3822
// Step 2: Invoke second Lambda function (notification service)
39-
const step2Result = await context.step("invoke-notification-service", async () => {
40-
41-
console.log('NotificationServiceStep...');
42-
43-
const command = new InvokeCommand({
44-
FunctionName: "NotificationServiceFunction",
45-
InvocationType: "RequestResponse",
46-
Payload: JSON.stringify({
47-
executionId,
48-
triggerTime,
49-
previousStepResult: step1Result,
50-
task: "send_completion_notification",
51-
})
52-
});
53-
54-
const response = await lambdaClient.send(command);
55-
const payload = JSON.parse(new TextDecoder().decode(response.Payload));
56-
57-
return payload;
23+
const step2Result = await context.invoke("NotificationServiceFunction", {
24+
executionId,
25+
triggerTime,
26+
previousStepResult: step1Result,
27+
task: "send_completion_notification",
5828
});
29+
console.log(`[${executionId}] NotificationServiceStep completed`);
5930

60-
console.log('DurableFunctionExecutionCompleted...');
31+
console.log(`[${executionId}] DurableFunctionExecutionCompleted`);
6132

6233
// Return final result
6334
return {
@@ -72,4 +43,4 @@ export const handler = withDurableExecution(
7243
};
7344

7445
}
75-
);
46+
);

lambda-durable-eventbridge-cron-nodejs-sam/durable-orchestrator/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"author": "",
1717
"license": "MIT",
1818
"dependencies": {
19-
"@aws-sdk/client-lambda": "^3.700.0",
2019
"@aws/durable-execution-sdk-js": "^1.0.2"
2120
}
2221
}

lambda-durable-eventbridge-cron-nodejs-sam/example-pattern.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"title": "EventBridge Cron to durable Lambda function",
3-
"description": "Create a durable Lambda function triggered by EventBridge on a cron schedule using AWS SAM.",
4-
"language": "Nodejs",
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",
55
"level": "200",
66
"framework": "AWS SAM",
77
"introBox": {
88
"headline": "How it works",
99
"text": [
10-
"This sample project demonstrates how to create a durable Lambda function that is triggered by 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.",
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.",
1111
"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 durable Lambda function with Nodejs 24 runtime, an EventBridge rule with cron schedule, and uses function versioning to ensure the cron trigger targets a published version rather than $LATEST."
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."
1313
]
1414
},
1515
"gitHub": {
@@ -31,7 +31,7 @@
3131
"link": "https://docs.aws.amazon.com/lambda/latest/dg/durable-invoking.html"
3232
},
3333
{
34-
"text": "AWS durable execution SDK for Nodejs",
34+
"text": "AWS durable execution SDK for Node.js",
3535
"link": "https://github.com/aws/aws-durable-execution-sdk-js"
3636
}
3737
]

lambda-durable-eventbridge-cron-nodejs-sam/template.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#AWS SAM template to deploy the durable function with EventBridge schedule
1+
#AWS SAM template to deploy the Lambda durable function with Amazon EventBridge schedule
22
AWSTemplateFormatVersion: "2010-09-09"
33
Transform: AWS::Serverless-2016-10-31
4-
Description: Durable Function with EventBridge Hourly Schedule
4+
Description: Lambda Durable Function with Amazon EventBridge 5-Minute Schedule
55

66
Resources:
77
# Main Durable Orchestrator Function
@@ -32,7 +32,7 @@ Resources:
3232
Type: Schedule
3333
Properties:
3434
Schedule: "rate(5 minutes)"
35-
Description: Trigger durable function every 5 minutes
35+
Description: Trigger Lambda durable function every 5 minutes
3636
Enabled: true
3737

3838
# Data Processor Function

0 commit comments

Comments
 (0)