AWS EventBridge is a serverless event bus service. Setting up AWS EventBridge Rules with Terraform lets you create and manage AWS EventBridge rules using Terraform, including event patterns, schedules, and targets. This guide covers how to 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.
AWS EventBridge is a serverless event bus that lets your applications, AWS services, and supported SaaS partners publish and react to events in near real-time. 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 formerly CloudWatch Events 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.
What is AWS EventBridge
EventBridge has three main concepts:
- 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
- Rule - Defines the event pattern or schedule
- Target - The resource that receives the event
AWS EventBridge already includes a default event bus for every account. This event bus is automatically configured to receive events from AWS services, like S3:PutObject, which is the event emitted from S3 when an object is created.
A sample S3 Object Created event looks like:
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"
}
}
By default, every event bus can have up to 300 configured rules.
Prerequisites and Project Structure
Before configuring EventBridge with Terraform:
- 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 structure is:
terraform-eventbridge/
├── main.tf
├── variables.tf
├── outputs.tf
├── modules/
│ └── eventbridge/
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
└── patterns/
└── events.json
Managing Event Buses with Terraform
Creating a custom event bus gives isolation and control over event routing and permissions.
```hcl
Event Bus
resource "awscloudwatcheventbus" "main" {
name = "${var.projectname}-bus"
tags = merge(
var.tags,
{
Name = "${var.project_name}-bus"
}
)
}
```
Using the default bus with Terraform requires a data block:
hcl
data "aws_cloudwatch_event_bus" "default" {
name = "default"
}
Event Bus Policy allows cross-account PutEvents:
```hcl
Event Bus Policy
resource "awscloudwatcheventbuspolicy" "main" {
eventbusname = awscloudwatcheventbus.main.name
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "AllowOtherAccountsPutEvents"
Effect = "Allow"
Principal = {
AWS = var.allowedaccountids
}
Action = "events:PutEvents"
Resource = awscloudwatcheventbus.main.arn
}
]
})
}
```
Configuring EventBridge Rules in Terraform
EventBridge rules can be schedule-based or pattern-based.
Schedule Rule
```hcl
Schedule Rule
resource "awscloudwatcheventrule" "schedule" {
name = "${var.projectname}-schedule"
description = "Schedule-based rule"
eventbusname = awscloudwatcheventbus.main.name
scheduleexpression = "rate(5 minutes)"
tags = merge(
var.tags,
{
Name = "${var.project_name}-schedule"
}
)
}
```
Pattern-based Rule
Pattern rules match incoming events using event_pattern.
```hcl
Pattern-based Rule
resource "awscloudwatcheventrule" "pattern" {
name = "${var.projectname}-pattern"
description = "Pattern-based rule"
eventbusname = awscloudwatcheventbus.main.name
eventpattern = jsonencode({
"source": ["myapp.orders"]
})
enabled = true
}
```
The most common use-case creates a custom bus, logging, rules and targets.
Event Targets in EventBridge using Terraform
EventBridge can target Lambda, SQS, CloudWatch Logs, and API Destination targets.
A Terraform module example for targets:
hcl
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
}
]
}
The module supports IAM policy attachments and various ways to create and attach additional policies.
Using Schedulers with Terraform
A big feature of EventBridge is the ability to create task scheduler rules. These rules trigger custom events to EventBridge at a specific time or periodically.
EventBridge Scheduler is now available in all AWS Regions, so the examples work consistently across environments including GovCloud and most new regions.
We can use the same awscloudwatcheventrule resource to define a scheduled rule, but we’ll use the property scheduleexpression instead of the event_pattern.
An expression with a cron pattern defines minute, hour, day of the month, month, day of the week, and year of execution.
For example, the expression 0 12 * * ? * will run every day at 12:00 PM UTC.
Let’s say you need to back up your data storage every week. To do that, you can set up a scheduler rule with a cron expression to be triggered every week. This event will be sent to a target responsible for backing up your data storage weekly at the designated time.
Terraform Module for EventBridge
Terraform module to create EventBridge resources.
- 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 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"
}
}
Minimal usage:
hcl
module "eventbridge" {
source = "terraform-aws-modules/eventbridge/aws"
bus_name = "my-bus"
tags = {
Name = "my-bus"
}
}
Monitoring EventBridge with Terraform
You can now push the code to your repository and confirm the Terraform plan.
To test, you can add a new file to your S3 bucket and then go to the rule monitoring to see if an event was matched for that rule.
The metrics can take a few minutes to show on CloudWatch or the monitoring tab.
Best Practices for Terraform and EventBridge Integration
- Use dedicated event buses for isolation and apply policies for cross-account PutEvents
- Keep rule counts below 300 per bus
- Enable logging with logconfig and logdelivery to CloudWatch Logs and S3
- Use dead-letter queues for targets to avoid lost events
- Use input_transformer to shape payloads before delivery
- Prefer EventBridge Scheduler for new standalone schedules over cron-style rules
- Tag resources consistently with Name and project tags
Resource Overview
| Resource Type | Terraform Resource | Purpose |
| awscloudwatcheventbus | Event Bus | Where events arrive |
| awscloudwatcheventbuspolicy | Event Bus Policy | Permissions for PutEvents |
| awscloudwatcheventrule | Rule | Schedule or pattern matching |
| Event Targets | Target configuration | Lambda, SQS, CloudWatch Logs, API Destination |
| EventBridge Scheduler | Scheduler | Cron and rate based scheduling |
Conclusion
Building event-driven architectures with Terraform EventBridge means defining event buses, rules with event patterns and schedules, and wiring targets with retries and dead-letter queues. Terraform gives repeatable infrastructure for custom buses, logging, archives, replays, pipes, schedules and schedule groups, and conditional creation of resources.
The combination of awscloudwatcheventrule for both pattern and scheduleexpression rules, data sources for default buses, and modular patterns for targets and transformations allows teams to manage AWS EventBridge rules using Terraform with full lifecycle control.
Using the terraform-aws-modules/eventbridge/aws module accelerates delivery by handling bus, logging, rules and targets in one declaration, while manual HCL provides granular control over bus policies, schedule expressions, and event patterns for complex event-driven workflows.