Skip to content

Commit 23ba47c

Browse files
author
kurt.tometich
committed
eventbridge pipes dynamic message id pattern created
1 parent 074037f commit 23ba47c

File tree

6 files changed

+262
-0
lines changed

6 files changed

+262
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Assigning a dynamic message group ID from the message body using EventBridge Pipes
2+
3+
EventBridge, a serverless event bus service, and SQS, a managed message queuing service, work together in event-driven architectures to route and process messages between AWS services and applications. While EventBridge routes messages to SQS queues for processing by microservices, FIFO queues can be used for strict message ordering. Although EventBridge cannot directly set message group IDs for organizing related messages, EventBridge Pipes provides a solution by allowing dynamic message group ID assignment based on event properties, enabling ordered processing for specific users, applications, or locations without additional coding.
4+
5+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/eventbridge-pipes-dynamic-message-group-id
6+
7+
> [!Important]
8+
> 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+
* [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 Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed
15+
16+
17+
## Deployment
18+
19+
1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
20+
```
21+
git clone https://github.com/aws-samples/serverless-patterns
22+
```
23+
2. Change directory to the pattern directory:
24+
```
25+
cd eventbridge-pipes-dynamic-message-group-id
26+
```
27+
28+
3. Build and deploy the SAM application
29+
```bash
30+
sam build && sam deploy --guided
31+
```
32+
33+
During the prompts:
34+
* Enter a stack name
35+
* Enter the desired AWS Region
36+
* Allow SAM CLI to create IAM roles with the required permissions.
37+
38+
You can accept all other defaults. Copy down the SQS Output Queue URL. You'll use this later in testing.
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+
## How it works
43+
![AWS Architecture EventBridge rule to SQS FIFO Queue to EventBridge Pipe to SQS FIFO Queue with message group ID set](assets/architecture.png)
44+
45+
This project implements an event processing pipeline using AWS EventBridge and SQS FIFO queues. The pipeline consists of:
46+
47+
- An EventBridge rule that captures events from "my-custom-app" source
48+
- An input SQS FIFO queue that receives events from the EventBridge rule
49+
- An EventBridge pipe that processes messages from the input queue and dynamically sets the message group ID for the target SQS FIFO queue
50+
- An output SQS FIFO queue that receives processed messages
51+
52+
A key component of this pattern is the syntax in the EventBrige Pipe Target to obtain the correct property value to set the message group id. The following shows an example in the AWS Console. You can also view this configuration in the SAM template under the CustomEventPipe resource.
53+
![EventBridge Pipe Target Configuration](assets/pipeTargetConfiguration.png)
54+
55+
## Testing
56+
57+
To test, you'll send an event from a custom source to EventBridge, which will trigger the EventBridge rule to send the event to an SQS FIFO Queue. You'll then wait for the EventBridge pipe to process and finally verify the message within the destination SQS FIFO queue with the message group ID set.
58+
59+
1. Send a test event to EventBridge. Take a look at the event structure and attributes. The account attribute will be used as the message group ID.
60+
```bash
61+
aws events put-events --entries file://events/test-event.json
62+
```
63+
64+
2. Wait for a couple seconds for the EventBridge pipe to process the message, then check the output queue. Make sure you replace the queue-url value with the correct output queue URL from deployment.
65+
```bash
66+
aws sqs receive-message \
67+
--queue-url <OUTPUT_QUEUE_URL> \
68+
--attribute-names All \
69+
--message-attribute-names All \
70+
--max-number-of-messages 10
71+
```
72+
73+
You should see a list of messages. View the event and take note of the MessageGroupID under the Attributes section. This originally came from your event input "accountId" property. The EventBridge Pipe is what allows us to perform this modification. Feel free to modify the event, send additional events to EventBridge and review the output queue messages.
74+
75+
## Cleanup
76+
77+
To remove all resources:
78+
79+
```bash
80+
sam delete
81+
```
82+
83+
----
84+
Copyright 2025 Amazon.com, Inc. or its affiliates. All Rights Reserved.
85+
86+
SPDX-License-Identifier: MIT-0
57.5 KB
Loading
106 KB
Loading
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"Source": "my-custom-app",
4+
"DetailType": "CustomEvent",
5+
"Detail": "{\"accountId\": \"12345678\", \"message\": \"Test message\", \"timestamp\": \"2023-01-01T00:00:00Z\"}"
6+
}
7+
]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"title": "EventBridge Pipes Dynamic Message Group Id",
3+
"description": "Uses EventBridge Pipes to dynamically retrieve and set the message group ID for a SQS FIFO destination queue",
4+
"language": "",
5+
"level": "200",
6+
"framework": "AWS SAM",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"This pattern demonstrates how to use EventBridge rules, EventBridge Pipes and SQS FIFO queues to dynamically retrieve and set a message group ID from the message body for an SQS FIFO queue."
11+
]
12+
},
13+
"gitHub": {
14+
"template": {
15+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/eventbridge-pipes-dynamic-message-group-id",
16+
"templateURL": "serverless-patterns/eventbridge-pipes-dynamic-message-group-id",
17+
"projectFolder": "eventbridge-pipes-dynamic-message-group-id",
18+
"templateFile": "template.yaml"
19+
}
20+
},
21+
"resources": {
22+
"bullets": [
23+
{
24+
"text": "AWS EventBridge Pipes",
25+
"link": "https://aws.amazon.com/eventbridge/pipes/"
26+
},
27+
{
28+
"text": "AWS EventBridge Pipes SQS Source",
29+
"link": "https://docs.aws.amazon.com/eventbridge/latest/userguide/"
30+
},
31+
{
32+
"text": "AWS SQS FIFO Queues",
33+
"link": "https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-fifo-queues.html"
34+
}
35+
]
36+
},
37+
"deploy": {
38+
"text": [
39+
"sam deploy"
40+
]
41+
},
42+
"testing": {
43+
"text": [
44+
"See the GitHub repo for detailed testing instructions."
45+
]
46+
},
47+
"cleanup": {
48+
"text": [
49+
"Delete the stack: sam delete"
50+
]
51+
},
52+
"authors": [
53+
{
54+
"name": "Kurt Tometich",
55+
"image": "https://drive.google.com/uc?export=view&id=16K9snJRzXYTI7O0nYbzrKVYP58xOxciB",
56+
"bio": "Kurt is a Sr. Solutions Architect based in Colorado who enjoys building lean, mean serverless solutions.",
57+
"linkedin": "kurt-tometich",
58+
"twitter": ""
59+
}
60+
]
61+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: SAM template for EventBridge rule, SQS FIFO queues and EventBridge pipe setup
4+
5+
Resources:
6+
7+
# EventBridge Rule IAM Role
8+
EventBridgeRuleRole:
9+
Type: AWS::IAM::Role
10+
Properties:
11+
AssumeRolePolicyDocument:
12+
Version: '2012-10-17'
13+
Statement:
14+
- Effect: Allow
15+
Principal:
16+
Service: events.amazonaws.com
17+
Action: sts:AssumeRole
18+
Policies:
19+
- PolicyName: AllowSQSAccess
20+
PolicyDocument:
21+
Version: '2012-10-17'
22+
Statement:
23+
- Effect: Allow
24+
Action:
25+
- sqs:SendMessage
26+
Resource: !GetAtt InputFifoQueue.Arn
27+
28+
# Input FIFO Queue
29+
InputFifoQueue:
30+
Type: AWS::SQS::Queue
31+
Properties:
32+
QueueName: input-queue.fifo
33+
FifoQueue: true
34+
ContentBasedDeduplication: true
35+
36+
# Output FIFO Queue
37+
OutputFifoQueue:
38+
Type: AWS::SQS::Queue
39+
Properties:
40+
QueueName: output-queue.fifo
41+
FifoQueue: true
42+
ContentBasedDeduplication: true
43+
44+
# EventBridge Rule
45+
CustomEventRule:
46+
Type: AWS::Events::Rule
47+
Properties:
48+
Description: "Rule to match events from my-custom-app"
49+
EventPattern:
50+
source:
51+
- "my-custom-app"
52+
State: "ENABLED"
53+
Targets:
54+
- Arn: !GetAtt InputFifoQueue.Arn
55+
Id: "SendToInputQueue"
56+
SqsParameters:
57+
MessageGroupId: "DEFAULT"
58+
RoleArn: !GetAtt EventBridgeRuleRole.Arn
59+
60+
# EventBridge Pipe
61+
CustomEventPipe:
62+
Type: AWS::Pipes::Pipe
63+
Properties:
64+
Name: "custom-event-pipe"
65+
RoleArn: !GetAtt PipeRole.Arn
66+
Source: !GetAtt InputFifoQueue.Arn
67+
Target: !GetAtt OutputFifoQueue.Arn
68+
TargetParameters:
69+
SqsQueueParameters:
70+
MessageGroupId: "$.body.detail.accountId"
71+
72+
# EventBridge Pipe IAM Role
73+
PipeRole:
74+
Type: AWS::IAM::Role
75+
Properties:
76+
AssumeRolePolicyDocument:
77+
Version: '2012-10-17'
78+
Statement:
79+
- Effect: Allow
80+
Principal:
81+
Service: pipes.amazonaws.com
82+
Action: sts:AssumeRole
83+
Policies:
84+
- PolicyName: PipeSourcePolicy
85+
PolicyDocument:
86+
Version: '2012-10-17'
87+
Statement:
88+
- Effect: Allow
89+
Action:
90+
- sqs:ReceiveMessage
91+
- sqs:DeleteMessage
92+
- sqs:GetQueueAttributes
93+
Resource: !GetAtt InputFifoQueue.Arn
94+
- PolicyName: PipeTargetPolicy
95+
PolicyDocument:
96+
Version: '2012-10-17'
97+
Statement:
98+
- Effect: Allow
99+
Action:
100+
- sqs:SendMessage
101+
Resource: !GetAtt OutputFifoQueue.Arn
102+
103+
Outputs:
104+
105+
OutputQueueURL:
106+
Description: "URL of the output FIFO queue"
107+
Value: !Ref OutputFifoQueue
108+

0 commit comments

Comments
 (0)