|
| 1 | +# Amazon SNS to Amazon SQS by filtering content (Message-content router pattern) |
| 2 | + |
| 3 | +This pattern creates an SNS topic and various SQS destination queues. Messages received by the SNS topic are routed to various SQS queues according to routing logic. The pattern is an implementation of the Integration pattern: "Content-Based Message route" available here: [https://www.enterpriseintegrationpatterns.com/patterns/messaging/ContentBasedRouter.html](https://www.enterpriseintegrationpatterns.com/patterns/messaging/ContentBasedRouter.html) |
| 4 | + |
| 5 | +Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/sns-sqs-message-content-router-cdk](https://serverlessland.com/patterns/sns-sqs-message-content-router-cdk) |
| 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 | +## Requirements |
| 10 | + |
| 11 | +* [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. |
| 12 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 13 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 14 | +* [AWS CDK](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html) (AWS SAMCDK) installed |
| 15 | + |
| 16 | +## Deployment Instructions |
| 17 | + |
| 18 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 19 | + ``` |
| 20 | + git clone https://github.com/aws-samples/serverless-patterns |
| 21 | + ``` |
| 22 | +1. Change directory to the pattern directory: |
| 23 | + ``` |
| 24 | + cd sns-sqs-message-content-router-cdk/src |
| 25 | + ``` |
| 26 | +1. From the command line, use AWS CDK to deploy the Stack: |
| 27 | + ``` |
| 28 | + cdk deploy |
| 29 | + ``` |
| 30 | +1. Note the outputs from the CDK deployment process. These contain the resource names and/or ARNs which are used for testing. |
| 31 | +
|
| 32 | +## How it works |
| 33 | +
|
| 34 | +The CDK Stack pick-up a configuration from `stackconfiguration.json` stored inside the `src` folder. This files contains a JSON structure that describe the routing logic. The stack will also create a DeadQueue letter for each Queue. |
| 35 | +
|
| 36 | +The parameter `IsAttribute` specifies to CDK if the filter is of type attribute or content. |
| 37 | +
|
| 38 | +```json |
| 39 | +{ |
| 40 | + "Sns": { |
| 41 | + "TopicName": "content-router" |
| 42 | + }, |
| 43 | + "Sqs": [ |
| 44 | + { |
| 45 | + "QueueName": "country-usa", |
| 46 | + "IsAttribute": true, |
| 47 | + "Filter": { |
| 48 | + "FilterName": "country", |
| 49 | + "FilterValues": [ |
| 50 | + "united states of america", |
| 51 | + "usa" |
| 52 | + ] |
| 53 | + } |
| 54 | + }, |
| 55 | + { |
| 56 | + "QueueName": "country-germany", |
| 57 | + "IsAttribute": true, |
| 58 | + "Filter": { |
| 59 | + "FilterName": "country", |
| 60 | + "FilterValues": [ |
| 61 | + "germany", |
| 62 | + "de" |
| 63 | + ] |
| 64 | + } |
| 65 | + }, |
| 66 | + { |
| 67 | + "QueueName": "language-english", |
| 68 | + "IsAttribute": false, |
| 69 | + "Filter": { |
| 70 | + "FilterName": "language", |
| 71 | + "FilterValues": [ |
| 72 | + "english", |
| 73 | + "en" |
| 74 | + ] |
| 75 | + } |
| 76 | + } |
| 77 | + ] |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Testing |
| 82 | + |
| 83 | +In order to test the solution, you need to login into your AWS Console and generate a couple of Amazon SNS messages that mimics the filters provided in `stackconfiguration.js`. |
| 84 | + |
| 85 | +**Scenario-1 Attribute based** |
| 86 | + |
| 87 | +Create an SNS message with the following information: |
| 88 | + |
| 89 | +``` |
| 90 | +Message Body: |
| 91 | +{ |
| 92 | + "Message":"Test Message to be received by Queue3 only" |
| 93 | +} |
| 94 | +Message Attribute: |
| 95 | +Type - String; Name - country; Value - usa |
| 96 | +``` |
| 97 | + |
| 98 | +**Scenario-2 Content based** |
| 99 | + |
| 100 | +Create an SNS message with the following information: |
| 101 | + |
| 102 | +``` |
| 103 | +Message body based filter. Copy Json to the message body and add attibutes given below. This message should be received by Queue1 and Queue3 |
| 104 | +Message Body: |
| 105 | +{ |
| 106 | + "Message":"Test Message to be received by Queue2 only", |
| 107 | + "Filter":{ |
| 108 | + "Language": ["en"] |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +Then head to the Amazon SQS queues, pull the messages and verify that the messages have been routed to the correct queue. |
| 114 | + |
| 115 | +## Cleanup |
| 116 | + |
| 117 | +1. Delete the stack |
| 118 | + ```bash |
| 119 | + cdk destroy |
| 120 | + ``` |
| 121 | +---- |
| 122 | +Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 123 | + |
| 124 | +SPDX-License-Identifier: MIT-0 |
0 commit comments