|
| 1 | +# DynamoDB Stream to SQS queue using EventBridge Pipes |
| 2 | + |
| 3 | +This serverless pattern demonstrates how to send events from DynamoDB Stream to SQS using EventBridge Pipes. |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## Requirements |
| 8 | + |
| 9 | +* [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. |
| 10 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 11 | +* [Git CLI](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) installed |
| 12 | +* [NodeJS](https://nodejs.org/en/download/) (LTS version) installed |
| 13 | +* [Serverless Framework CLI](https://www.serverless.com/framework/docs/getting-started) installed |
| 14 | + |
| 15 | +## Deployment Instructions |
| 16 | + |
| 17 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 18 | + |
| 19 | + ``` sh |
| 20 | + git clone https://github.com/aws-samples/serverless-patterns |
| 21 | + ``` |
| 22 | +1. Change directory to the pattern directory: |
| 23 | + ``` sh |
| 24 | + cd serverless-patterns/eventbridge-pipes-dynamodbstream-to-sqs-serverless |
| 25 | + ``` |
| 26 | +1. From the command line, use npm/yarn to install the development dependencies: |
| 27 | + ``` sh |
| 28 | + npm install |
| 29 | + ``` |
| 30 | + -or- |
| 31 | + ``` sh |
| 32 | + yarn install |
| 33 | + ``` |
| 34 | +1. From the command line, use Serverless Framework to deploy the AWS resources for the pattern as specified in the serverless.yml file: |
| 35 | + ``` sh |
| 36 | + serverless deploy --verbose |
| 37 | + ``` |
| 38 | + The above command will deploy resources to `ap-south-1` region by default. You can override the target region with `--region <region>` CLI option, e.g. |
| 39 | + ``` sh |
| 40 | + serverless deploy --verbose --region us-west-2 |
| 41 | + ``` |
| 42 | + |
| 43 | +## How it works |
| 44 | + |
| 45 | +This template will create a DynamoDB table, EventBridge Pipe and a SQS queue. DynamoDB Stream is enabled in the DynamoDB table. |
| 46 | + |
| 47 | +Whenever an item is inserted in DynamoDB table, the stream picks up the event & send it to EventBridge Pipes. The EventBridge Pipes Filter function in the Pipes filters the event and passes to the next states accordingly. For this example pattern, it checks for `INSERT` events & passes it to Enrichment Lambda function if found one. The Lambda function makes some logs regarding to event & event name & finally passes it to the Target SQS queue. In Target SQS queue, when new messages are polled, the event information information can be retrieved. |
| 48 | + |
| 49 | +## Testing |
| 50 | + |
| 51 | +Once this stack is deployed in your AWS account, copy the `DynamoDBSourceTableName` value from the output. |
| 52 | + |
| 53 | +Then, insert one record to the DynamoDB table as follows: |
| 54 | +```sh |
| 55 | + aws dynamodb put-item \ |
| 56 | + --table-name DynamoDBSourceTable \ |
| 57 | + --item \ |
| 58 | + '{"Album": {"S": "Item-One"}}' |
| 59 | +``` |
| 60 | +When you check the Target SQS queue, you can see the message from DynamoDB stream is available in the SQS queue. |
| 61 | +Optional - Under the Enrichment Lambda Function of EventBrudge Pipes, you can see the CloudWatch logs for the event information and eventName. |
| 62 | + |
| 63 | + |
| 64 | +## Cleanup |
| 65 | +1. Delete the stack |
| 66 | + ```sh |
| 67 | + serverless remove --verbose |
| 68 | + ``` |
| 69 | +1. Confirm the stack has been deleted |
| 70 | + ```sh |
| 71 | + aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'dynamodbstream-ebpipes-sqs-serverless-prod')].StackStatus" |
| 72 | + ``` |
| 73 | + Expected output |
| 74 | + ```json |
| 75 | + [ |
| 76 | + "DELETE_COMPLETE" |
| 77 | + ] |
| 78 | + ``` |
| 79 | + NOTE: You might need to add `--region <region>` option to AWS CLI command if you AWS CLI default region does not match the one, that you used for the Serverless Framework deployment. |
| 80 | +---- |
| 81 | +Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 82 | +SPDX-License-Identifier: MIT-0 |
0 commit comments