Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions eventbridge-fan-in-terraform/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Amazon Eventbridge Eventbus fan-in to Central Eventbus to different Region
# Amazon EventBridge Eventbus fan-in to Central Eventbus to different Region

This pattern demonstrates how to aggregate all your events from multiple Eventbus (in the same Region) to a central Eventbus in a different Region. This allows you to centrally accumulate events coming in from different event sources for downstream consumption.

This pattern is deployed using Terraform to create a central EventBridge bus, Eventbridge rules on fan-in buses and all IAM resources required. The Eventbuses to aggregate can be defined in the `terraform.tfvars` file (sample ARNs is provided, replace with Eventbus ARNs as needed). The provider.tf file also lists the AWS Regions of the fan-in Eventbus and central Eventbus (replace these based on where your Eventbuses exist and where you want your central bus to be created).
This pattern is deployed using Terraform to create a central EventBridge bus, EventBridge rules on fan-in buses and all IAM resources required. The Eventbuses to aggregate can be defined in the `terraform.tfvars` file (sample ARNs is provided, replace with Eventbus ARNs as needed). The provider.tf file also lists the AWS Regions of the fan-in Eventbus and central Eventbus (replace these based on where your Eventbuses exist and where you want your central bus to be created).

Note: The pattern assumes you already have a minimum of 2 eventbuses that you want to aggregate onto a central Eventbus (the central bus will be created as part of this deployment). The fan-in Eventbuses can be created manually or using any of the Infrastructure-as-code platforms.

Expand All @@ -29,26 +29,57 @@ Important: this application uses various AWS services and there are costs associ
```
3. In the provider.tf file, add the Regions where the Eventbuses exist in the provider block with alias "others". Also add the Region where you want the central Eventbus to be created in the provider block with alias "central"

4. In the terraform.tfvars file, add the ARNs of the existing Eventbuses you want to aggregate. Note that all of these fan-in Eventbuses should exist in the same Region.
4. If you do not already have Eventbuses to aggregate, create a minimum of 2 Eventbuses in the fan-in Region:
```
aws events create-event-bus --name eventBus1 --region us-east-2
aws events create-event-bus --name eventBus2 --region us-east-2
```

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Added AWS CLI commands to create the fan-in event buses (the pattern assumes they already exist, but most readers need to create them first)


5. In the terraform.tfvars file, add the ARNs of the existing Eventbuses you want to aggregate. Note that all of these fan-in Eventbuses should exist in the same Region.

5. From the command line, initialize Terraform:
6. From the command line, initialize Terraform:
```
terraform init
```
6. From the command line, apply the configuration in the main.tf file and follow the prompts:
7. From the command line, apply the configuration in the main.tf file and follow the prompts:
```
terraform apply
```

## How it works

This application picks the configurations from terraform.tfvars files to create Eventbridge rules on all entered Eventbus for fan-in to a central Eventbus. The terraform creates the rules, along with the required IAM roles and policies and configures the target as the central Eventbus which it also creates. The central Eventbus has a rule that routes all events to a Cloudwatch log group for testing.
This application picks the configurations from terraform.tfvars files to create EventBridge rules on all entered Eventbus for fan-in to a central Eventbus. The terraform creates the rules, along with the required IAM roles and policies and configures the target as the central Eventbus which it also creates. The central Eventbus has a rule that routes all events to a CloudWatch log group for testing.

## Testing

Note: To test this pattern, you will need a minimum of 2 existing event buses in the fan-in Region.

Login to the AWS account where the terraform is deployed. Publish an event on any of the fan-in Eventbuses. Switch to the Region where the central Eventbus is created and navigate to Cloudwatch logs. You should see the event you published in the log group of the Central Eventbus.
1. Publish a test event on each of the fan-in Eventbuses:
```
aws events put-events --region us-east-2 --entries '[
{
"EventBusName": "eventBus1",
"Source": "demo.test",
"DetailType": "TestEvent",
"Detail": "{\"message\": \"hello from eventBus1\"}"
}
]'
aws events put-events --region us-east-2 --entries '[
{
"EventBusName": "eventBus2",
"Source": "demo.test",
"DetailType": "TestEvent",
"Detail": "{\"message\": \"hello from eventBus2\"}"
}
]'
```
Each command should return `"FailedEntryCount": 0`.

2. After a few seconds, check the log group of the central Eventbus in the central Region:
```
aws logs tail /aws/events/central-bus-logs/logs --region ca-central-1 --since 10m
```
You should see both published events as JSON, including `"source": "demo.test"` and `"region": "us-east-2"`. You can also view the log group `/aws/events/central-bus-logs/logs` in the CloudWatch console.


## Cleanup
Expand All @@ -57,7 +88,12 @@ Login to the AWS account where the terraform is deployed. Publish an event on an
```
terraform destroy
```
2. If you created the Eventbuses in the deployment step 4, delete them:
```
aws events delete-event-bus --name eventBus1 --region us-east-2
aws events delete-event-bus --name eventBus2 --region us-east-2
```
----
Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.

SPDX-License-Identifier: MIT-0
SPDX-License-Identifier: MIT-0
7 changes: 5 additions & 2 deletions eventbridge-fan-in-terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ resource "aws_cloudwatch_event_rule" "ebrule" {
local.account_id
]
})
is_enabled = true

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

╷
│ Warning: Argument is deprecated
│ 
│   with aws_cloudwatch_event_rule.ebrule,
│   on main.tf line 15, in resource "aws_cloudwatch_event_rule" "ebrule":
│   15:   is_enabled = true
│ 
│ is_enabled is deprecated. Use state instead.
╵

state = "ENABLED"
}

//IAM role and policy for Event rule
Expand Down Expand Up @@ -86,6 +86,7 @@ data "aws_iam_policy_document" "log_policy" {
}

resource "aws_cloudwatch_log_resource_policy" "log-resource-policy" {
provider = aws.central
policy_document = data.aws_iam_policy_document.log_policy.json
policy_name = "eventbridge-log-publishing-policy"
}
Expand All @@ -95,7 +96,8 @@ module "central_eventbridge" {
providers = {
aws = aws.central
}
source = "terraform-aws-modules/eventbridge/aws"
source = "terraform-aws-modules/eventbridge/aws"
version = "~> 4.3"

bus_name = "central-event-bus"

Expand Down Expand Up @@ -128,6 +130,7 @@ module "central_eventbridge" {

//Create targets on each rule to send events to Central Eventbus
resource "aws_cloudwatch_event_target" "EBtargets" {
provider = aws.others
for_each = tomap(aws_cloudwatch_event_rule.ebrule)
event_bus_name = each.key
rule = each.value.name
Expand Down
12 changes: 0 additions & 12 deletions eventbridge-fan-in-terraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
output "all_rules" {
value = resource.aws_cloudwatch_event_rule.ebrule
}

output "ebRole" {
value = aws_iam_role.event_bus_invoke_central_event_bus.arn
}

output "central_bus_arn" {
value = module.central_eventbridge.eventbridge_bus_arn
}

output "central_bus_rule_arn" {
value = module.central_eventbridge.eventbridge_rule_arns
}
4 changes: 2 additions & 2 deletions eventbridge-fan-in-terraform/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.9.0"
version = "~> 6.0"
}
}
}
Expand All @@ -17,4 +17,4 @@ provider "aws" {
provider "aws" {
region = "us-east-2"
alias = "others"
}
}
10 changes: 0 additions & 10 deletions eventbridge-fan-in-terraform/src/app.js

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: It's unused code

This file was deleted.