Skip to content

Commit

Permalink
Merge pull request #2466 from chaitanyagummadi/chaitanyagummadi-featu…
Browse files Browse the repository at this point in the history
…re-eventbridge-pipes-sqs-to-eventbus-terraform

New serverless pattern - SQS-EventBus using EventBridge Pipes
  • Loading branch information
julianwood authored Dec 17, 2024
2 parents a182db9 + 5b0843b commit c6a8c9b
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 0 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 90 additions & 0 deletions eventbridge-pipes-sqs-to-eventbus-terraform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Amazon SQS queue to Amazon EventBridge custom event bus using Amazon EventBridge Pipe

This pattern sends messages from an SQS queue to an EventBridge event bus using an EventBridge pipe.

Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/patterns/eventbridge-pipes-sqs-to-eventbus-terraform

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.

## Requirements

* [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.
* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured
* [Git](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) installed and configured
* [Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli) installed

## Deployment Instructions

1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
```
git clone https://github.com/aws-samples/serverless-patterns
```
2. Change directory to the pattern directory:
```
cd serverless-patterns/eventbridge-pipes-sqs-to-eventbus-terraform
```
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.
```
terraform init
```
4. Deploy the AWS resources for the pattern as specified in the `main.tf` file.
Use the below command to review the changes before deploying.
```
terraform plan
```
Deploy:
```
terraform apply --auto-approve
```
5. Note the output from the Terraform deployment process. These contain the resource names and/or ARNs which are used for testing.
## How it works
Please refer to the architecture diagram below:
![End to End Architecture](Architecture.jpg)
## Testing
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:
Message1:
```sh
aws sqs send-message --queue-url <SQS_url> --message-body '{"orderId":"125a2e1e-d420-482e-8008-5a606f4b2076", "customerId": "a48516db-66aa-4dbc-bb66-a7f058c5ec24", "type": "NEW"}'
```

Message2:
```sh
aws sqs send-message --queue-url <SQS_url> --message-body '{"orderId":"125a2e1e-d420-482e-8008-5a606f4b2076", "customerId": "a48516db-66aa-4dbc-bb66-a7f058c5ec24", "type": "OLD"}'

```

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.

```
{
"source": ["myapp.demo"]
}
```

## Cleanup

1. Change to the below directory inside the cloned git repo:
```
cd serverless-patterns/eventbridge-pipes-dynamodbstream-to-sqs-terraform
```
2. Delete the resources
```bash
terraform destroy
```
3. Enter 'yes' when prompted.
4. Check if all the resources were deleted successfully.
```bash
terraform show
```
----
Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"title": "Amazon SQS to Amazon EventBridge event bus using Amazon EventBridge Pipes",
"description": "This pattern creates an Amazon EventBridge Pipe to deliver messages from an Amazon SQS queue to Amazon EventBridge Event Bus.",
"language": "",
"level": "200",
"framework": "Terraform",
"introBox": {
"headline": "How it works",
"text": [
"Amazon EventBridge Pipe routes events from an Amazon SQS queue to a custom EventBridge event bus while applying filters.",
"This filtering capability enables you to create precise event processing pipelines, ensuring that only relevant events are propagated to downstream systems.",
"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."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/eventbridge-pipes-sqs-to-eventbus-terraform",
"templateURL": "serverless-patterns/eventbridge-pipes-sqs-to-eventbus-terraform",
"projectFolder": "eventbridge-pipes-sqs-to-eventbus-terraform",
"templateFile": "main.tf"
}
},
"resources": {
"bullets": [
{
"text": "EventBridge Pipes with SQS queue as a source",
"link": "https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-pipes-sqs.html"
},
{
"text": "Amazon EventBridge Pipes architectural patterns blog post",
"link": "https://aws.amazon.com/blogs/compute/implementing-architectural-patterns-with-amazon-eventbridge-pipes/"
}
]
},
"deploy": {
"text": [
"terraform init",
"terraform apply"
]
},
"testing": {
"text": [
"See the README in the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"<code>terraform destroy</code>"
]
},
"authors": [
{
"name": "Chaitanya Gummadi",
"image": "https://gravatar.com/avatar/a9b30c6e727f613f3f44a44016e36998719404ea7451c750f402a21f2dd72937.jpg?size=256",
"bio": "Chaitanya is a Senior Cloud Support Engineer with Amazon Web Services (AWS) based in Texas.",
"linkedin": "cgummadi"
}
],
"patternArch": {
"icon1": {
"x": 20,
"y": 50,
"service": "sqs",
"label": "Amazon SQS queue"
},
"icon2": {
"x": 50,
"y": 50,
"service": "eventbridge-pipes",
"label": "EventBridge pipe"
},
"icon3": {
"x": 80,
"y": 50,
"service": "eventbridge",
"label": "EventBridge bus"
},
"line1": {
"from": "icon1",
"to": "icon2",
"label": ""
},
"line2": {
"from": "icon2",
"to": "icon3",
"label": ""
}
}
}
59 changes: 59 additions & 0 deletions eventbridge-pipes-sqs-to-eventbus-terraform/example-pattern.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"title": "Amazon SQS to Amazon EventBridge event bus using Amazon EventBridge Pipes",
"description": "This pattern creates an Amazon EventBridge Pipe to deliver messages from an Amazon SQS queue to Amazon EventBridge Event Bus.",
"language": "",
"level": "200",
"framework": "Terraform",
"introBox": {
"headline": "How it works",
"text": [
"Amazon EventBridge Pipe routes events from an Amazon SQS queue to a custom EventBridge event bus while applying filters.",
"This filtering capability enables you to create precise event processing pipelines, ensuring that only relevant events are propagated to downstream systems.",
"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."
]
},
"gitHub": {
"template": {
"repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/eventbridge-pipes-sqs-to-eventbus-terraform",
"templateURL": "serverless-patterns/eventbridge-pipes-sqs-to-eventbus-terraform",
"projectFolder": "eventbridge-pipes-sqs-to-eventbus-terraform",
"templateFile": "main.tf"
}
},
"resources": {
"bullets": [
{
"text": "EventBridge Pipes with SQS queue as a source",
"link": "https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-pipes-sqs.html"
},
{
"text": "Amazon EventBridge Pipes architectural patterns blog post",
"link": "https://aws.amazon.com/blogs/compute/implementing-architectural-patterns-with-amazon-eventbridge-pipes/"
}
]
},
"deploy": {
"text": [
"terraform init",
"terraform apply"
]
},
"testing": {
"text": [
"See the README in the GitHub repo for detailed testing instructions."
]
},
"cleanup": {
"text": [
"<code>terraform destroy</code>"
]
},
"authors": [
{
"name": "Chaitanya Gummadi",
"image": "https://gravatar.com/avatar/a9b30c6e727f613f3f44a44016e36998719404ea7451c750f402a21f2dd72937.jpg?size=256",
"bio": "Chaitanya is a Senior Cloud Support Engineer with Amazon Web Services (AWS) based in Texas.",
"linkedin": "cgummadi"
}
]
}
110 changes: 110 additions & 0 deletions eventbridge-pipes-sqs-to-eventbus-terraform/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {}

# Create EventBus Target
resource "aws_cloudwatch_event_bus" "target" {
name = "demo_bus"
}

# Create SQS Source
resource "aws_sqs_queue" "source" {
name = "demo_queue"
}

# Create EventBridge Pipe
resource "aws_pipes_pipe" "demo_pipe" {
name = "demo_pipe"
role_arn = aws_iam_role.demo_role.arn
source = aws_sqs_queue.source.arn
target = aws_cloudwatch_event_bus.target.arn
source_parameters {
sqs_queue_parameters {
batch_size = 1
maximum_batching_window_in_seconds = 2
}
filter_criteria {
filter {
pattern = jsonencode({
"body":{"type":["NEW"]}
})
}
}
}
target_parameters {
eventbridge_event_bus_parameters {
source = "myapp.demo"
}
}
}

# Create IAM role for EventBridge Pipe
resource "aws_iam_role" "demo_role" {
name = "demo_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "pipes.amazonaws.com"
}
},
]
})
}

# Create an IAM policy for EventBridge Pipe
resource "aws_iam_policy" "demo_policy" {
name = "demo_policy"
policy = jsonencode({
"Version": "2012-10-17",
"Statement": [
{ "Sid": "SourcePermissions",
"Effect": "Allow",
"Action": [
"sqs:ReceiveMessage",
"sqs:DeleteMessage",
"sqs:GetQueueAttributes"
],
"Resource": [
aws_sqs_queue.source.arn
]
},
{ "Sid": "TargetPermissions"
"Effect": "Allow",
"Action": [
"events:PutEvents"
],
"Resource": [
aws_cloudwatch_event_bus.target.arn
]
}
]
})

# Attach the IAM policy to the IAM role
}
resource "aws_iam_role_policy_attachment" "demo-attach" {
role = aws_iam_role.demo_role.name
policy_arn = aws_iam_policy.demo_policy.arn
}

output "SQS_url" {
value = aws_sqs_queue.source.url
description = "The URL of the SQS queue."
}

output "Pipes_arn" {
value = aws_pipes_pipe.demo_pipe.arn
description = "The ARN of the Pipe."
}

0 comments on commit c6a8c9b

Please sign in to comment.