diff --git a/eventbridge-fan-in-terraform/README.md b/eventbridge-fan-in-terraform/README.md index 81d585fb17..aee890df23 100644 --- a/eventbridge-fan-in-terraform/README.md +++ b/eventbridge-fan-in-terraform/README.md @@ -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. @@ -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 + ``` + +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 @@ -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 \ No newline at end of file +SPDX-License-Identifier: MIT-0 diff --git a/eventbridge-fan-in-terraform/main.tf b/eventbridge-fan-in-terraform/main.tf index 13a3d137b0..0ecef95fdd 100644 --- a/eventbridge-fan-in-terraform/main.tf +++ b/eventbridge-fan-in-terraform/main.tf @@ -12,7 +12,7 @@ resource "aws_cloudwatch_event_rule" "ebrule" { local.account_id ] }) - is_enabled = true + state = "ENABLED" } //IAM role and policy for Event rule @@ -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" } @@ -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" @@ -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 diff --git a/eventbridge-fan-in-terraform/outputs.tf b/eventbridge-fan-in-terraform/outputs.tf index 65d314163d..2df8df9157 100644 --- a/eventbridge-fan-in-terraform/outputs.tf +++ b/eventbridge-fan-in-terraform/outputs.tf @@ -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 -} \ No newline at end of file diff --git a/eventbridge-fan-in-terraform/provider.tf b/eventbridge-fan-in-terraform/provider.tf index ec1976ed2b..8356ec71cb 100644 --- a/eventbridge-fan-in-terraform/provider.tf +++ b/eventbridge-fan-in-terraform/provider.tf @@ -2,7 +2,7 @@ terraform { required_providers { aws = { source = "hashicorp/aws" - version = "5.9.0" + version = "~> 6.0" } } } @@ -17,4 +17,4 @@ provider "aws" { provider "aws" { region = "us-east-2" alias = "others" -} \ No newline at end of file +} diff --git a/eventbridge-fan-in-terraform/src/app.js b/eventbridge-fan-in-terraform/src/app.js deleted file mode 100644 index cb3c4d9c1b..0000000000 --- a/eventbridge-fan-in-terraform/src/app.js +++ /dev/null @@ -1,10 +0,0 @@ -/*! Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * SPDX-License-Identifier: MIT-0 - */ - -'use strict' - -exports.handler = async (event) => { - // Lambda handler code - console.log(JSON.stringify(event, 0, null)) -} \ No newline at end of file