Skip to content

Commit c6a8c9b

Browse files
authored
Merge pull request #2466 from chaitanyagummadi/chaitanyagummadi-feature-eventbridge-pipes-sqs-to-eventbus-terraform
New serverless pattern - SQS-EventBus using EventBridge Pipes
2 parents a182db9 + 5b0843b commit c6a8c9b

File tree

5 files changed

+348
-0
lines changed

5 files changed

+348
-0
lines changed
59.9 KB
Loading
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Amazon SQS queue to Amazon EventBridge custom event bus using Amazon EventBridge Pipe
2+
3+
This pattern sends messages from an SQS queue to an EventBridge event bus using an EventBridge pipe.
4+
5+
Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/eventbridge-pipes-sqs-to-eventbus-terraform
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](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) installed and configured
14+
* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli) 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+
2. Change directory to the pattern directory:
23+
```
24+
cd serverless-patterns/eventbridge-pipes-sqs-to-eventbus-terraform
25+
```
26+
3. Run the follow command to initialize, download, and install the defined providers. If you are unfamiliar with the Terraform CLI, refer Terraform [documentation](https://www.terraform.io/cli/commands) to learn more about the available commands.
27+
```
28+
terraform init
29+
```
30+
4. Deploy the AWS resources for the pattern as specified in the `main.tf` file.
31+
32+
Use the below command to review the changes before deploying.
33+
```
34+
terraform plan
35+
```
36+
Deploy:
37+
```
38+
terraform apply --auto-approve
39+
```
40+
5. Note the output from the Terraform deployment process. These contain the resource names and/or ARNs which are used for testing.
41+
42+
## How it works
43+
44+
Please refer to the architecture diagram below:
45+
![End to End Architecture](Architecture.jpg)
46+
47+
## Testing
48+
49+
Once this stack is deployed in your AWS account, copy the SQS_url value from the output. Then, insert two messages to the SQS Queue as follows:
50+
51+
Message1:
52+
```sh
53+
aws sqs send-message --queue-url <SQS_url> --message-body '{"orderId":"125a2e1e-d420-482e-8008-5a606f4b2076", "customerId": "a48516db-66aa-4dbc-bb66-a7f058c5ec24", "type": "NEW"}'
54+
55+
```
56+
57+
Message2:
58+
```sh
59+
aws sqs send-message --queue-url <SQS_url> --message-body '{"orderId":"125a2e1e-d420-482e-8008-5a606f4b2076", "customerId": "a48516db-66aa-4dbc-bb66-a7f058c5ec24", "type": "OLD"}'
60+
61+
```
62+
63+
Optional: You could create an EventBridge rule with the below event pattern on the custom bus and add relevant targets. With this setup, Message2 would be filtered out by the Pipe, and only Message1 would invoke the configured EventBridge target. Ensure to delete the EventBridge rule before the clean up process.
64+
65+
```
66+
{
67+
"source": ["myapp.demo"]
68+
}
69+
```
70+
71+
## Cleanup
72+
73+
1. Change to the below directory inside the cloned git repo:
74+
```
75+
cd serverless-patterns/eventbridge-pipes-dynamodbstream-to-sqs-terraform
76+
```
77+
2. Delete the resources
78+
```bash
79+
terraform destroy
80+
```
81+
3. Enter 'yes' when prompted.
82+
83+
4. Check if all the resources were deleted successfully.
84+
```bash
85+
terraform show
86+
```
87+
----
88+
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
89+
90+
SPDX-License-Identifier: MIT-0
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{
2+
"title": "Amazon SQS to Amazon EventBridge event bus using Amazon EventBridge Pipes",
3+
"description": "This pattern creates an Amazon EventBridge Pipe to deliver messages from an Amazon SQS queue to Amazon EventBridge Event Bus.",
4+
"language": "",
5+
"level": "200",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"Amazon EventBridge Pipe routes events from an Amazon SQS queue to a custom EventBridge event bus while applying filters.",
11+
"This filtering capability enables you to create precise event processing pipelines, ensuring that only relevant events are propagated to downstream systems.",
12+
"Then, other AWS services that are supported as EventBridge targets can consume these events from the EventBus by adding an EventBridge rule with relevant targets."
13+
]
14+
},
15+
"gitHub": {
16+
"template": {
17+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/eventbridge-pipes-sqs-to-eventbus-terraform",
18+
"templateURL": "serverless-patterns/eventbridge-pipes-sqs-to-eventbus-terraform",
19+
"projectFolder": "eventbridge-pipes-sqs-to-eventbus-terraform",
20+
"templateFile": "main.tf"
21+
}
22+
},
23+
"resources": {
24+
"bullets": [
25+
{
26+
"text": "EventBridge Pipes with SQS queue as a source",
27+
"link": "https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-pipes-sqs.html"
28+
},
29+
{
30+
"text": "Amazon EventBridge Pipes architectural patterns blog post",
31+
"link": "https://aws.amazon.com/blogs/compute/implementing-architectural-patterns-with-amazon-eventbridge-pipes/"
32+
}
33+
]
34+
},
35+
"deploy": {
36+
"text": [
37+
"terraform init",
38+
"terraform apply"
39+
]
40+
},
41+
"testing": {
42+
"text": [
43+
"See the README in the GitHub repo for detailed testing instructions."
44+
]
45+
},
46+
"cleanup": {
47+
"text": [
48+
"<code>terraform destroy</code>"
49+
]
50+
},
51+
"authors": [
52+
{
53+
"name": "Chaitanya Gummadi",
54+
"image": "https://gravatar.com/avatar/a9b30c6e727f613f3f44a44016e36998719404ea7451c750f402a21f2dd72937.jpg?size=256",
55+
"bio": "Chaitanya is a Senior Cloud Support Engineer with Amazon Web Services (AWS) based in Texas.",
56+
"linkedin": "cgummadi"
57+
}
58+
],
59+
"patternArch": {
60+
"icon1": {
61+
"x": 20,
62+
"y": 50,
63+
"service": "sqs",
64+
"label": "Amazon SQS queue"
65+
},
66+
"icon2": {
67+
"x": 50,
68+
"y": 50,
69+
"service": "eventbridge-pipes",
70+
"label": "EventBridge pipe"
71+
},
72+
"icon3": {
73+
"x": 80,
74+
"y": 50,
75+
"service": "eventbridge",
76+
"label": "EventBridge bus"
77+
},
78+
"line1": {
79+
"from": "icon1",
80+
"to": "icon2",
81+
"label": ""
82+
},
83+
"line2": {
84+
"from": "icon2",
85+
"to": "icon3",
86+
"label": ""
87+
}
88+
}
89+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"title": "Amazon SQS to Amazon EventBridge event bus using Amazon EventBridge Pipes",
3+
"description": "This pattern creates an Amazon EventBridge Pipe to deliver messages from an Amazon SQS queue to Amazon EventBridge Event Bus.",
4+
"language": "",
5+
"level": "200",
6+
"framework": "Terraform",
7+
"introBox": {
8+
"headline": "How it works",
9+
"text": [
10+
"Amazon EventBridge Pipe routes events from an Amazon SQS queue to a custom EventBridge event bus while applying filters.",
11+
"This filtering capability enables you to create precise event processing pipelines, ensuring that only relevant events are propagated to downstream systems.",
12+
"Then, other AWS services that are supported as EventBridge targets can consume these events from the EventBus by adding an EventBridge rule with relevant targets."
13+
]
14+
},
15+
"gitHub": {
16+
"template": {
17+
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/eventbridge-pipes-sqs-to-eventbus-terraform",
18+
"templateURL": "serverless-patterns/eventbridge-pipes-sqs-to-eventbus-terraform",
19+
"projectFolder": "eventbridge-pipes-sqs-to-eventbus-terraform",
20+
"templateFile": "main.tf"
21+
}
22+
},
23+
"resources": {
24+
"bullets": [
25+
{
26+
"text": "EventBridge Pipes with SQS queue as a source",
27+
"link": "https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-pipes-sqs.html"
28+
},
29+
{
30+
"text": "Amazon EventBridge Pipes architectural patterns blog post",
31+
"link": "https://aws.amazon.com/blogs/compute/implementing-architectural-patterns-with-amazon-eventbridge-pipes/"
32+
}
33+
]
34+
},
35+
"deploy": {
36+
"text": [
37+
"terraform init",
38+
"terraform apply"
39+
]
40+
},
41+
"testing": {
42+
"text": [
43+
"See the README in the GitHub repo for detailed testing instructions."
44+
]
45+
},
46+
"cleanup": {
47+
"text": [
48+
"<code>terraform destroy</code>"
49+
]
50+
},
51+
"authors": [
52+
{
53+
"name": "Chaitanya Gummadi",
54+
"image": "https://gravatar.com/avatar/a9b30c6e727f613f3f44a44016e36998719404ea7451c750f402a21f2dd72937.jpg?size=256",
55+
"bio": "Chaitanya is a Senior Cloud Support Engineer with Amazon Web Services (AWS) based in Texas.",
56+
"linkedin": "cgummadi"
57+
}
58+
]
59+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 5.0"
6+
}
7+
}
8+
}
9+
provider "aws" {}
10+
11+
# Create EventBus Target
12+
resource "aws_cloudwatch_event_bus" "target" {
13+
name = "demo_bus"
14+
}
15+
16+
# Create SQS Source
17+
resource "aws_sqs_queue" "source" {
18+
name = "demo_queue"
19+
}
20+
21+
# Create EventBridge Pipe
22+
resource "aws_pipes_pipe" "demo_pipe" {
23+
name = "demo_pipe"
24+
role_arn = aws_iam_role.demo_role.arn
25+
source = aws_sqs_queue.source.arn
26+
target = aws_cloudwatch_event_bus.target.arn
27+
source_parameters {
28+
sqs_queue_parameters {
29+
batch_size = 1
30+
maximum_batching_window_in_seconds = 2
31+
}
32+
filter_criteria {
33+
filter {
34+
pattern = jsonencode({
35+
"body":{"type":["NEW"]}
36+
})
37+
}
38+
}
39+
}
40+
target_parameters {
41+
eventbridge_event_bus_parameters {
42+
source = "myapp.demo"
43+
}
44+
}
45+
}
46+
47+
# Create IAM role for EventBridge Pipe
48+
resource "aws_iam_role" "demo_role" {
49+
name = "demo_role"
50+
assume_role_policy = jsonencode({
51+
Version = "2012-10-17"
52+
Statement = [
53+
{
54+
Action = "sts:AssumeRole"
55+
Effect = "Allow"
56+
Sid = ""
57+
Principal = {
58+
Service = "pipes.amazonaws.com"
59+
}
60+
},
61+
]
62+
})
63+
}
64+
65+
# Create an IAM policy for EventBridge Pipe
66+
resource "aws_iam_policy" "demo_policy" {
67+
name = "demo_policy"
68+
policy = jsonencode({
69+
"Version": "2012-10-17",
70+
"Statement": [
71+
{ "Sid": "SourcePermissions",
72+
"Effect": "Allow",
73+
"Action": [
74+
"sqs:ReceiveMessage",
75+
"sqs:DeleteMessage",
76+
"sqs:GetQueueAttributes"
77+
],
78+
"Resource": [
79+
aws_sqs_queue.source.arn
80+
]
81+
},
82+
{ "Sid": "TargetPermissions"
83+
"Effect": "Allow",
84+
"Action": [
85+
"events:PutEvents"
86+
],
87+
"Resource": [
88+
aws_cloudwatch_event_bus.target.arn
89+
]
90+
}
91+
]
92+
})
93+
94+
# Attach the IAM policy to the IAM role
95+
}
96+
resource "aws_iam_role_policy_attachment" "demo-attach" {
97+
role = aws_iam_role.demo_role.name
98+
policy_arn = aws_iam_policy.demo_policy.arn
99+
}
100+
101+
output "SQS_url" {
102+
value = aws_sqs_queue.source.url
103+
description = "The URL of the SQS queue."
104+
}
105+
106+
output "Pipes_arn" {
107+
value = aws_pipes_pipe.demo_pipe.arn
108+
description = "The ARN of the Pipe."
109+
}
110+

0 commit comments

Comments
 (0)