Skip to content

Senora-dev/template-eb-rules

Repository files navigation

EventBridge Rules Template

Terraform AWS License

Production-ready AWS EventBridge rules infrastructure managed by Terraform. Create multiple event-driven and scheduled rules from JSON configuration with built-in monitoring, dead letter queues, and multi-environment support.

Features

Core Capabilities

  • JSON-Driven Configuration: Define multiple EventBridge rules in JSON files
  • Schedule & Event Patterns: Support for both cron/rate schedules and event patterns
  • Multiple Target Types: Lambda, SQS, SNS, ECS, Batch, Kinesis, and more
  • Multi-Environment: Separate dev, staging, and production configurations

Production-Ready Features

  • Dead Letter Queue: Automatic DLQ for failed event deliveries
  • KMS Encryption: Optional encryption for DLQ and SNS topics
  • CloudWatch Monitoring: Pre-configured alarms and metrics
  • CloudWatch Logs: Event debugging and audit trails
  • SNS Alerts: Email notifications for failures
  • Comprehensive Tagging: Cost allocation and resource tracking

Developer Experience

  • Makefile Automation: One-command deployments
  • CI/CD Ready: GitHub Actions workflows included
  • Security Scanning: TFLint and Checkov integration
  • Pre-commit Hooks: Automated validation before commits
  • Drift Detection: Monitor infrastructure changes

Quick Start

Prerequisites

  • AWS CLI configured
  • Terraform >= 1.6.0
  • Make (optional but recommended)

Basic Deployment

# 1. Configure your rules
vim rules.json
# Edit rules.json with your EventBridge rules

# 2. Configure environment
vim envs/dev/terraform.tfvars

# 3. Deploy
make deploy-all ENV=dev

Using Terraform Directly

cd infra
terraform init
terraform plan -var-file=../envs/dev/terraform.tfvars
terraform apply -var-file=../envs/dev/terraform.tfvars

Project Structure

template-eventbridge-rules/
├── .github/workflows/      # CI/CD pipelines
├── docs/                   # Comprehensive documentation
├── envs/                   # Environment-specific configs
│   ├── dev/
│   ├── staging/
│   └── prod/
├── examples/               # Usage examples
│   ├── scheduled-lambda/
│   ├── s3-event-processor/
│   └── multi-target/
├── infra/                  # Terraform infrastructure
│   ├── main.tf
│   ├── variables.tf
│   └── outputs.tf
├── scripts/                # Automation scripts
├── rules.json              # EventBridge rules configuration
├── Makefile                # Automation commands
└── README.md

Rules Configuration

JSON Format

Define your EventBridge rules in JSON format:

[
  {
    "rule_name": "daily-report",
    "description": "Generate daily report at 9 AM UTC",
    "schedule_expression": "cron(0 9 * * ? *)",
    "enabled": true,
    "targets": {
      "lambda": {
        "arn": "arn:aws:lambda:us-east-1:123456789012:function:report-generator"
      }
    },
    "tags": {
      "Purpose": "Reporting"
    }
  },
  {
    "rule_name": "s3-upload-processor",
    "description": "Process new S3 uploads",
    "event_pattern": "{\"source\":[\"aws.s3\"],\"detail-type\":[\"Object Created\"]}",
    "enabled": true,
    "targets": {
      "sqs": {
        "arn": "arn:aws:sqs:us-east-1:123456789012:processing-queue"
      }
    }
  }
]

Rule Properties

Property Type Required Description
rule_name string Yes Unique rule name
description string No Rule description
schedule_expression string Conditional Cron or rate expression (use this OR event_pattern)
event_pattern string Conditional Event pattern JSON (use this OR schedule_expression)
enabled boolean No Enable/disable rule (default: true)
targets object No Target configuration (Lambda, SQS, SNS, etc.)
tags object No Additional tags for this rule

Makefile Commands

# Deployment
make deploy-all ENV=dev      # Complete deployment
make plan ENV=prod           # Show planned changes
make apply ENV=staging       # Apply changes
make destroy ENV=dev         # Destroy infrastructure

# Validation
make validate                # Validate Terraform
make validate-rules          # Validate rules JSON
make fmt                     # Format code
make security-scan           # Run security scans

# Monitoring
make outputs ENV=prod        # Show outputs
make list-rules ENV=prod     # List all rules
make check-dlq ENV=prod      # Check DLQ status
make rule-status ENV=dev     # Show rule statuses

# Quality
make pre-commit              # Run pre-commit checks
make drift-detect ENV=prod   # Detect configuration drift

Configuration

Environment Variables

Key variables to configure in envs/{ENV}/terraform.tfvars:

Variable Description Default
environment Environment name (dev/staging/prod) -
project_name Project identifier eventbridge
aws_region AWS region us-east-1
rules_path Path to rules JSON file ../rules.json
enable_dlq Enable Dead Letter Queue true
enable_encryption Enable KMS encryption true
enable_monitoring Enable CloudWatch alarms true
create_sns_topic Create SNS for alerts false (dev), true (prod)
alert_email Email for alarm notifications -

See docs/CONFIGURATION.md for complete reference.

Monitoring

CloudWatch Alarms

Pre-configured alarms:

  • Failed Invocations: Triggers when event delivery fails
  • DLQ Messages: Triggers when messages appear in DLQ
  • Configurable thresholds per environment

Metrics

Monitor key metrics:

  • Invocations count
  • Failed invocations
  • Throttled events
  • Dead letter queue depth

Dead Letter Queue

Failed events are automatically sent to a DLQ for:

  • Troubleshooting failed deliveries
  • Replay capability
  • Audit trail

Check DLQ status:

make check-dlq ENV=prod

Security

Best Practices Implemented

  • Encryption: KMS encryption for DLQ and SNS (optional)
  • IAM: Least privilege access for EventBridge
  • Audit: CloudWatch Logs for event tracking
  • Validation: Pre-commit hooks and CI/CD checks
  • Scanning: TFLint and Checkov security scans

Security Scanning

make security-scan   # Run TFLint + Checkov
make tflint          # Run TFLint only
make checkov         # Run Checkov only

Examples

Scheduled Lambda Invocation

{
  "rule_name": "hourly-cleanup",
  "schedule_expression": "rate(1 hour)",
  "targets": {
    "lambda": {
      "arn": "arn:aws:lambda:us-east-1:123456789012:function:cleanup"
    }
  }
}

S3 Event Processing

{
  "rule_name": "s3-processor",
  "event_pattern": "{\"source\":[\"aws.s3\"],\"detail-type\":[\"Object Created\"]}",
  "targets": {
    "sqs": {
      "arn": "arn:aws:sqs:us-east-1:123456789012:processor-queue"
    }
  }
}

Multiple Targets

{
  "rule_name": "multi-target-rule",
  "schedule_expression": "cron(0 12 * * ? *)",
  "targets": {
    "lambda": {
      "arn": "arn:aws:lambda:us-east-1:123456789012:function:processor"
    },
    "sns": {
      "arn": "arn:aws:sns:us-east-1:123456789012:notifications"
    }
  }
}

See examples/ for more use cases.

Troubleshooting

Rules Not Triggering

  1. Check rule is enabled: make rule-status ENV=dev
  2. Verify schedule expression/event pattern syntax
  3. Check CloudWatch Logs: /aws/events/{project}-{env}
  4. Review target permissions

Failed Invocations

  1. Check DLQ: make check-dlq ENV=prod
  2. Review CloudWatch alarms
  3. Verify target ARNs are correct
  4. Check target resource permissions

Common Issues

See docs/TROUBLESHOOTING.md for detailed debugging guides.

CI/CD

GitHub Actions

Automated workflows:

  • Validation: Format, validate, and lint on every push
  • Security: Checkov scans on PRs
  • Plan: Show Terraform plan on PRs
  • Apply: Auto-deploy on merge (optional)

Pre-commit Hooks

Install hooks:

pip install pre-commit
pre-commit install

Hooks run automatically on commit:

  • Terraform format
  • Terraform validate
  • TFLint
  • Checkov
  • JSON validation

Cost Estimation

Typical monthly costs:

Component Usage Est. Cost
EventBridge 1M events/month $1.00
CloudWatch Logs 1GB $0.50
SQS DLQ Minimal usage $0.10
KMS 1 key $1.00
Total ~$2.60/month

EventBridge offers:

  • First 14M events/month: FREE (new accounts)
  • $1.00 per million events thereafter

Development

Adding New Rules

  1. Edit rules.json
  2. Validate: make validate-rules
  3. Plan: make plan ENV=dev
  4. Apply: make apply ENV=dev

Testing

# Validate configuration
make validate

# Check formatting
make fmt

# Run security scans
make security-scan

# Pre-commit checks
make pre-commit

License

MIT License - see LICENSE for details.

Support

Acknowledgments

Built with:


Template Version: 1.0.0 Last Updated: November 2025 Maintained by: Senora.dev

About

Create event-driven workflows from a single JSON file - schedules, triggers, and pipelines - with DLQs, monitoring, and multi-environment support built in. production-ready EventBridge rules with DLQs, monitoring, alarms, and multi-environment support, so you can focus on your application logic, not AWS wiring.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors