Terraform-Driven AWS EventBridge Rules and Buses

AWS EventBridge is a serverless event bus service. The service was formerly known as CloudWatch Events. It allows you to create and manage rules using Terraform, including event patterns, schedules, and targets. Terraform provides declarative configuration for event buses, rules, targets, permissions, connections, destinations, pipes, schedules and schedule groups. With Terraform and Spacelift you can create event buses, define rules with event patterns, build scheduled rules with both awscloudwatchevent_rule and the dedicated EventBridge Scheduler, wire up Lambda, SQS, CloudWatch Logs, and API Destination targets, transform event payloads, and set up retries, dead-letter queues, and archives, with working code at every step.

Prerequisites and Project Structure

Working with EventBridge through Terraform requires a baseline setup and a clear project layout.

  • AWS CLI configured with appropriate permissions
  • Terraform installed (version 1.0.0 or later)
  • Basic understanding of event-driven architectures
  • Familiarity with JSON/YAML

A common project layout used in guides is:

terraform-eventbridge/ ├── main.tf ├── variables.tf ├── outputs.tf ├── modules/ │ └── eventbridge/ │ ├── main.tf │ ├── variables.tf │ └── outputs.tf └── patterns/ └── events.json

This structure separates bus definition, rule definition, and reusable patterns for event matching.

What Is AWS EventBridge

AWS EventBridge is a serverless event bus service that allows you to listen to events from your applications, supported third-party applications, and AWS services. It simplifies building event-driven architectures by serving as the central event hub and enabling applications to publish, subscribe to, and react to events in near real-time.

AWS EventBridge also provides built-in support for creating schedulers that emit events at scheduled times. Amazon EventBridge is the event bus that ties AWS services together. When an EC2 instance changes state, when a CodePipeline deployment fails, when an S3 object is created - EventBridge can catch those events and route them to targets like Lambda functions, SQS queues, or Step Functions.

EventBridge scheduled rules can also run cron-style scheduled tasks in AWS, although EventBridge Scheduler is the recommended service for new standalone schedules.

How EventBridge Works centers on the Event Bus concept.

  • Event Bus - Where events arrive. The default bus receives events from AWS services in your account; some services, such as S3 object events, require event delivery to EventBridge to be enabled first

The default bus receives events from AWS services, like S3:PutObject, which is the event emitted from S3 when an object is created.

Example event shape from the default bus:

json { "version": "0", "id": "17793124-05d4-b198-2fde-7ededc63b103", "detail-type": "Object Created", "source": "aws.s3", "account": "123456789012", "time": "2021-11-12T00:00:00Z", "region": "ca-central-1", "resources": ["arn:aws:s3:::example-bucket"], "detail": { "version": "0", "bucket": { "name": "example-bucket" }, "object": { "key": "example-key", "size": 5, "etag": "b1946ac92492d2347c6235b4d2611184", "version-id": "IYV3p45BT0ac8hjHg1houSdS1a.Mro8e", "sequencer": "00617F08299329D189" }, "request-id": "N4N7GDK58NMKJ12R", "requester": "123456789012", "source-ip-address": "1.2.3.4", "reason": "PutObject" } }

Managing Event Buses with Terraform

AWS EventBridge already includes a default event bus for every account. This event bus is automatically configured to receive events from AWS services.

To use the default bus with Terraform, we need to declare a data block pointing to the event bus name:

hcl data "aws_cloudwatch_event_bus" "default" { name = "default" }

By default, every event bus can have up to 300 configured rules.

Creating a custom bus:

hcl resource "aws_cloudwatch_event_bus" "main" { name = "${var.project_name}-bus" tags = merge( var.tags, { Name = "${var.project_name}-bus" } ) }

Event bus policy for cross-account publishing:

hcl resource "aws_cloudwatch_event_bus_policy" "main" { event_bus_name = aws_cloudwatch_event_bus.main.name policy = jsonencode({ Version = "2012-10-17" Statement = [ { Sid = "AllowOtherAccountsPutEvents" Effect = "Allow" Principal = { AWS = var.allowed_account_ids } Action = "events:PutEvents" Resource = aws_cloudwatch_event_bus.main.arn } ] }) }

Configuring EventBridge Rules in Terraform

Rules can be schedule-based or pattern-based.

Schedule rule example:

hcl resource "aws_cloudwatch_event_rule" "schedule" { name = "${var.project_name}-schedule" description = "Schedule-based rule" event_bus_name = aws_cloudwatch_event_bus.main.name schedule_expression = "rate(5 minutes)" tags = merge( var.tags, { Name = "${var.project_name}-schedule" } ) }

Pattern-based rules allow filtering by source, detail-type, and custom event patterns encoded as JSON.

A module based approach is commonly used to create custom bus, logging, rules and targets together. The terraform-aws-modules/eventbridge module:

  • Creates AWS EventBridge Resources (bus, rules, targets, permissions, connections, destinations, pipes, schedules and schedule groups)
  • Attach resources to an existing EventBridge bus
  • Support AWS EventBridge Archives and Replays
  • Conditional creation for many types of resources
  • Support IAM policy attachments and various ways to create and attach additional policies

Example module usage:

hcl module "eventbridge" { source = "terraform-aws-modules/eventbridge/aws" bus_name = "my-bus" log_config = { include_detail = "FULL" level = "INFO" } log_delivery = { cloudwatch_logs = { destination_arn = "arn:aws:logs:us-east-1:123456789012:log-group:my-log-group" } s3 = { destination_arn = "arn:aws:s3:::my-log-bucket" } } rules = { orders = { description = "Capture all order data" event_pattern = jsonencode({ "source" : ["myapp.orders"] }) enabled = true } } targets = { orders = [ { name = "send-orders-to-sqs" arn = aws_sqs_queue.queue.arn dead_letter_arn = aws_sqs_queue.dlq.arn }, { name = "send-orders-to-kinesis" arn = aws_kinesis_stream.this.arn dead_letter_arn = aws_sqs_queue.dlq.arn input_transformer = local.kinesis_input_transformer }, { name = "log-orders-to-cloudwatch" arn = aws_cloudwatch_log_group.this.arn } ] } tags = { Name = "my-bus" } }

A minimal module declaration:

hcl module "eventbridge" { source = "terraform-aws-modules/eventbridge/aws" bus_name = "my-bus" tags = { Name = "my-bus" } }

Event Targets in EventBridge Using Terraform

The guide walks through wiring targets such as Lambda, SQS, CloudWatch Logs, and API Destination targets, transform event payloads, and set up retries, dead-letter queues, and archives.

A table of common EventBridge resource types managed via Terraform:

| Resource Category | Terraform Representation |
| awscloudwatcheventbus | Event bus |
| aws
cloudwatcheventrule | Rule with pattern or schedule |
| awscloudwatcheventtarget | Target attachment |
| aws
cloudwatcheventbus_policy | Bus permissions |
| EventBridge Scheduler | Dedicated scheduler for new standalone schedules |

A table of module capabilities:

| Capability | Support |
| Create bus, rules, targets, permissions, connections, destinations, pipes, schedules and schedule groups | Yes |
| Attach resources to an existing EventBridge bus | Yes |
| Support AWS EventBridge Archives and Replays | Yes |
| Conditional creation for many types of resources | Yes |
| Support IAM policy attachments and various ways to create and attach additional policies | Yes |

Using Schedulers with Terraform

EventBridge provides built-in support for creating schedulers that emit events at scheduled times. EventBridge scheduled rules can also run cron-style scheduled tasks in AWS, although EventBridge Scheduler is the recommended service for new standalone schedules.

Terraform configurations can use awscloudwatcheventrule for scheduleexpression such as rate(5 minutes), and also use the dedicated EventBridge Scheduler resources for more advanced scheduling needs.

Monitoring and Best Practices

Set up monitoring from day one.

The following Terraform configuration creates the resources described above. Each resource includes proper tagging, security settings, and follows AWS best practices.

This downloads the AWS provider plugin and initializes the backend.

Always review the plan before applying. Check that only the expected resources will be created.

Terraform will create all resources in the correct order, handling dependencies automatically.

After applying, verify your resources are running correctly.

Managing AWS resources with Terraform brings consistency, version control, and automation to your infrastructure. The configurations in this guide follow production best practices and can be extended to match your specific requirements. Start with these foundations and iterate as your infrastructure needs evolve.

Conclusion

Terraform provides a complete workflow for building event-driven architectures with AWS EventBridge managed by Terraform — custom buses, rules, and cross-account events. The tutorial provides production-ready Terraform code you can adapt for your own infrastructure.

Key operational points that emerge from the reference material are the separation between the default event bus and custom buses, the 300 rule limit per bus by default, the need to enable event delivery for services such as S3 object events, and the preference for EventBridge Scheduler over classic scheduled rules for new standalone schedules. Using modules for bus, logging, rules and targets reduces repetition and enforces consistent tagging and IAM policies. The combination of event patterns, input transformers, dead-letter queues, retries, archives and replays gives operators full control over reliability and observability. Consistent use of Terraform for EventBridge ensures version-controlled, repeatable infrastructure that aligns with AWS best practices for serverless event routing.

Sources

  1. thecloudpanda.com
  2. spacelift.io
  3. oneuptime.com
  4. github.com
  5. terraformpilot.com

Related Posts