|
| 1 | + |
| 2 | +# Amazon DynamoDB Stream to AWS Step Functions Trigger |
| 3 | + |
| 4 | +This Pattern demonstrates how to automatically trigger AWS Step Functions workflows in response to changes in DynamoDB tables. The CDK construct `DynamoWorkflowTrigger` lets you connect DynamoDB and Step Functions by allowing you to define event handlers that monitor specific changes in your DynamoDB tables and trigger workflows in response. It leverages Lambda functions to evaluate conditions and start Step Functions state machines with inputs derived from the DynamoDB events. |
| 5 | + |
| 6 | +Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/ddbstream-lambda-sfn-cdk-ts](https://serverlessland.com/patterns/ddbstream-lambda-sfn-cdk-ts) |
| 7 | + |
| 8 | +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. |
| 9 | + |
| 10 | +## Requirements |
| 11 | + |
| 12 | +* [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. |
| 13 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 14 | +* [Node and NPM](https://nodejs.org/en/download/) installed |
| 15 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 16 | +* [AWS Cloud Development Kit](https://docs.aws.amazon.com/cdk/latest/guide/cli.html) (AWS CDK) installed |
| 17 | + |
| 18 | +## Deployment Instructions |
| 19 | + |
| 20 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 21 | + ``` |
| 22 | + git clone https://github.com/aws-samples/serverless-patterns |
| 23 | + ``` |
| 24 | +2. Change directory to the pattern directory: |
| 25 | + ``` |
| 26 | + cd ddbstream-lambda-sfn-cdk-ts |
| 27 | + ``` |
| 28 | +3. To deploy from the command line use the following: |
| 29 | + ```bash |
| 30 | + npm install |
| 31 | + npm run lambda |
| 32 | + cdk deploy |
| 33 | + ``` |
| 34 | + **The deployment will take a couple of minutes** |
| 35 | +
|
| 36 | +4. Note the outputs from the CDK deployment process. These contain the `<table_name>`, `<state-machine-arn>` and `<lambda-function-name>` which are used for testing. |
| 37 | +
|
| 38 | +
|
| 39 | +
|
| 40 | +## How It Works |
| 41 | +
|
| 42 | + |
| 43 | +
|
| 44 | +The `DdbstreamLambdaSfnExampleStack` demonstrates how to use the the pattern: |
| 45 | +
|
| 46 | +1. It creates a DynamoDB table (`TestTable`) with streaming enabled |
| 47 | +2. It creates a simple Step Functions state machine (`TestStateMachine`) |
| 48 | +3. It sets up a trigger with the following behavior: |
| 49 | + - It applies a filter to ignore events where a `SkipMe` attribute exists in the new image |
| 50 | + - It only processes `MODIFY` events (updates to existing items) |
| 51 | + - It checks two conditions: |
| 52 | + - The new value of `testKey` must be "test8" |
| 53 | + - The old value of `testKey` must have been "test9" |
| 54 | + - When all conditions are met, it triggers the state machine with input parameters extracted from the DynamoDB event: |
| 55 | + - `Index` taken from the item's partition key |
| 56 | + - `MapAttribute` taken from the first element in a list attribute |
| 57 | +
|
| 58 | +This workflow allows you to respond to specific data changes in DynamoDB by executing custom workflows with Step Functions. |
| 59 | +
|
| 60 | +
|
| 61 | +#### Features |
| 62 | +
|
| 63 | +- Dead letter queue for failed invocations |
| 64 | +- VPC support |
| 65 | +- Custom security groups |
| 66 | +- Fine-grained event filtering |
| 67 | +- Multiple event handlers per construct |
| 68 | +- JSONPath-based condition evaluation |
| 69 | +- Input mapping for state machines |
| 70 | +
|
| 71 | +#### Limitations |
| 72 | +
|
| 73 | +- Tables must have streams enabled with `NEW_AND_OLD_IMAGES` |
| 74 | +- Conditions currently only support exact matches via the `value` property |
| 75 | +- For complex filtering, use Lambda event source filters |
| 76 | +
|
| 77 | +## Testing |
| 78 | +
|
| 79 | +You can test the workflow using the AWS CLI to create and modify items in the DynamoDB table. Here are some example commands to test different scenarios: |
| 80 | +
|
| 81 | +1. First, create an item that shouldn't trigger the workflow (initial state): |
| 82 | +```bash |
| 83 | +aws dynamodb put-item \ |
| 84 | + --table-name <table_name> \ |
| 85 | + --item '{ |
| 86 | + "Index": {"S": "test-item-1"}, |
| 87 | + "testKey": {"S": "test9"}, |
| 88 | + "ListAttribute": {"L": [{"S": "first-element"}]} |
| 89 | + }' |
| 90 | +``` |
| 91 | + |
| 92 | +2. Update the item to trigger the workflow (meets all conditions): |
| 93 | +```bash |
| 94 | +aws dynamodb update-item \ |
| 95 | + --table-name <table_name> \ |
| 96 | + --key '{"Index": {"S": "test-item-1"}}' \ |
| 97 | + --update-expression "SET testKey = :newval" \ |
| 98 | + --expression-attribute-values '{":newval": {"S": "test8"}}' |
| 99 | +``` |
| 100 | + |
| 101 | +3. Test the SkipMe filter by creating an item that should be ignored: |
| 102 | +```bash |
| 103 | +aws dynamodb put-item \ |
| 104 | + --table-name <table_name> \ |
| 105 | + --item '{ |
| 106 | + "Index": {"S": "test-item-2"}, |
| 107 | + "testKey": {"S": "test9"}, |
| 108 | + "SkipMe": {"S": "true"} |
| 109 | + }' |
| 110 | +``` |
| 111 | +This should not trigger DDBEventTrigger lambda at all. |
| 112 | + |
| 113 | +To verify the results: |
| 114 | + |
| 115 | +1. Check if the Step Function was triggered: |
| 116 | +```bash |
| 117 | +aws stepfunctions list-executions \ |
| 118 | + --state-machine-arn <state-machine-arn> |
| 119 | +``` |
| 120 | + |
| 121 | +2. View the execution details: |
| 122 | +```bash |
| 123 | +aws stepfunctions get-execution-history \ |
| 124 | + --execution-arn <execution-arn-from-previous-command> |
| 125 | +``` |
| 126 | + |
| 127 | +3. Monitor Lambda function logs: |
| 128 | +```bash |
| 129 | +aws logs tail /aws/lambda/<lambda-function-name> --follow |
| 130 | +``` |
| 131 | + |
| 132 | + |
| 133 | +#### Troubleshooting |
| 134 | + |
| 135 | +- Check Amazon CloudWatch Logs for the Lambda function |
| 136 | +- Monitor the dead letter queue for failed events |
| 137 | +- Ensure Amazon IAM permissions are correct for DynamoDB stream access and Step Functions execution |
| 138 | + |
| 139 | +## Cleanup |
| 140 | + |
| 141 | +1. From the command line, use the following in the source folder |
| 142 | + ```bash |
| 143 | + cdk destroy |
| 144 | + ``` |
| 145 | +2. Confirm the removal and wait for the resource deletion to complete. |
| 146 | +---- |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 151 | + |
| 152 | +SPDX-License-Identifier: MIT-0 |
0 commit comments