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