AWS Learning
Messaging & Streaming

Amazon EventBridge

Event Bus, Rules, Patterns, Pipes, Scheduler, Archive & Replay

Tổng quan

Amazon EventBridge là serverless event bus service để xây dựng event-driven architectures. Nó là phiên bản nâng cấp của CloudWatch Events.

┌─────────────────────────────────────────────────────────────────┐
│                    EVENTBRIDGE OVERVIEW                         │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   EVENT SOURCES                EVENT BUS              TARGETS   │
│   ┌──────────────┐         ┌─────────────┐      ┌───────────┐   │
│   │ AWS Services │────────→│             │─────→│  Lambda   │   │
│   │ (S3, EC2...) │         │             │      └───────────┘   │
│   └──────────────┘         │  EventBridge│      ┌───────────┐   │
│   ┌──────────────┐         │   Event Bus │─────→│    SQS    │   │
│   │  Custom Apps │────────→│             │      └───────────┘   │
│   └──────────────┘         │  ┌────────┐ │      ┌───────────┐   │
│   ┌──────────────┐         │  │ Rules  │ │─────→│    SNS    │   │
│   │   SaaS Apps  │────────→│  └────────┘ │      └───────────┘   │
│   │(Zendesk,etc) │         │             │      ┌───────────┐   │
│   └──────────────┘         └─────────────┘─────→│Step Funcs │   │
│                                                 └───────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

EventBridge vs SNS vs SQS

FeatureEventBridgeSNSSQS
ModelEvent BusPub/SubQueue
FilteringAdvanced (100+ rules)BasicNone
Targets20+ AWS servicesLimitedConsumer apps
SchemaSchema RegistryNoNo
ReplayArchive & ReplayNoNo
SaaS IntegrationYesNoNo

  1. Core Concepts
  2. Event Buses
  3. Rules & Event Patterns
  4. Targets
  5. Event Replay & Archive
  6. Schema Registry
  7. Pipes
  8. Scheduler
  9. Security
  10. Best Practices

1. Core Concepts

1.1 Events

┌─────────────────────────────────────────────────────────────────┐
│                      EVENT STRUCTURE                            │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   {                                                             │
│     "version": "0",                                             │
│     "id": "12345678-1234-1234-1234-123456789012",               │
│     "detail-type": "EC2 Instance State-change Notification",    │
│     "source": "aws.ec2",                                        │
│     "account": "123456789012",                                  │
│     "time": "2024-01-01T00:00:00Z",                             │
│     "region": "us-east-1",                                      │
│     "resources": [                                              │
│       "arn:aws:ec2:us-east-1:123456789012:instance/i-1234"      │
│     ],                                                          │
│     "detail": {                                                 │
│       "instance-id": "i-1234567890abcdef0",                     │
│       "state": "stopped"                                        │
│     }                                                           │
│   }                                                             │
│                                                                 │
│   Key Fields:                                                   │
│   • source      → Who sent the event                            │
│   • detail-type → What type of event                            │
│   • detail      → Actual event data (varies by source)          │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

1.2 Architecture Components

┌─────────────────────────────────────────────────────────────────┐
│              EVENTBRIDGE COMPONENTS                             │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   ┌────────────────────────────────────────────────────────┐    │
│   │                     EVENT BUS                          │    │
│   │  ┌─────────────────────────────────────────────────┐    │   │
│   │  │                   RULES                         │    │   │
│   │  │  ┌───────────────┐    ┌───────────────┐        │     │   │
│   │  │  │ Rule 1        │    │ Rule 2        │        │     │   │
│   │  │  │ ┌───────────┐ │    │ ┌───────────┐ │        │     │   │
│   │  │  │ │Event      │ │    │ │Event      │ │        │     │   │
│   │  │  │ │Pattern    │ │    │ │Pattern    │ │        │     │   │
│   │  │  │ └───────────┘ │    │ └───────────┘ │        │     │   │
│   │  │  │       ↓       │    │       ↓       │        │     │   │
│   │  │  │ ┌───────────┐ │    │ ┌───────────┐ │        │     │   │
│   │  │  │ │ Targets   │ │    │ │ Targets   │ │        │     │   │
│   │  │  │ │(Lambda,   │ │    │ │(SQS,      │ │        │     │   │
│   │  │  │ │ SQS...)   │ │    │ │ SNS...)   │ │        │     │   │
│   │  │  │ └───────────┘ │    │ └───────────┘ │        │     │   │
│   │  │  └───────────────┘    └───────────────┘        │     │   │
│   │  └─────────────────────────────────────────────────┘    │   │
│   └────────────────────────────────────────────────────────┘    │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

2. Event Buses

2.1 Types of Event Buses

┌─────────────────────────────────────────────────────────────────┐
│                    EVENT BUS TYPES                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   1. DEFAULT EVENT BUS:                                         │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  • Receives events from AWS services                    │   │
│   │  • One per account per region                           │   │
│   │  • Cannot delete                                        │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   2. CUSTOM EVENT BUS:                                          │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  • For your own application events                      │   │
│   │  • Can create multiple                                  │   │
│   │  • Can share cross-account                              │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   3. PARTNER EVENT BUS:                                         │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  • SaaS integrations (Zendesk, Datadog, Auth0...)       │   │
│   │  • Created when you enable partner integration          │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

2.2 Cross-Account Event Sharing

┌─────────────────────────────────────────────────────────────────┐
│              CROSS-ACCOUNT EVENTS                               │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Account A (Source)                Account B (Target)          │
│   ┌───────────────────┐            ┌───────────────────┐        │
│   │ Event Bus         │            │ Event Bus         │        │
│   │ ┌───────────────┐ │            │ ┌───────────────┐  │       │
│   │ │ Rule          │ │            │ │ Rule          │  │       │
│   │ │ Target: →──────┼┼────────────┼→│               │  │       │
│   │ │ Account B Bus │ │            │ │ Target:Lambda │  │       │
│   │ └───────────────┘ │            │ └───────────────┘  │       │
│   └───────────────────┘            └───────────────────┘        │
│                                                                 │
│   Requirements:                                                 │
│   • Resource-based policy on target event bus                   │
│   • Allow source account to put events                          │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

3. Rules & Event Patterns

3.1 Event Pattern Matching

┌─────────────────────────────────────────────────────────────────┐
│                   EVENT PATTERN EXAMPLES                        │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   BASIC MATCHING:                                               │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  Match all EC2 state changes:                           │   │
│   │  {                                                      │   │
│   │    "source": ["aws.ec2"],                               │   │
│   │    "detail-type": ["EC2 Instance State-change"]         │   │
│   │  }                                                      │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   CONTENT FILTERING:                                            │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  Match only "stopped" state:                            │   │
│   │  {                                                      │   │
│   │    "source": ["aws.ec2"],                               │   │
│   │    "detail": {                                          │   │
│   │      "state": ["stopped"]                               │   │
│   │    }                                                    │   │
│   │  }                                                      │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

3.2 Advanced Pattern Operators

OperatorExampleMatches
Exact"state": ["running"]state = "running"
Prefix"source": [{"prefix": "aws."}]source starts with "aws."
Suffix"file": [{"suffix": ".png"}]file ends with ".png"
Anything-but"state": [{"anything-but": "running"}]state != "running"
Numeric"price": [{"numeric": [">", 100]}]price > 100
Exists"detail.error": [{"exists": true}]error field exists
IP"ip": [{"cidr": "10.0.0.0/24"}]IP in CIDR range
OR"state": ["running", "stopped"]state = running OR stopped

3.3 Complex Pattern Example

{
  "source": ["aws.ec2"],
  "detail-type": ["EC2 Instance State-change Notification"],
  "detail": {
    "state": ["terminated", "stopped"],
    "instance-id": [{"prefix": "i-prod-"}]
  }
}

→ Match EC2 events when production instances are stopped or terminated


4. Targets

4.1 Supported Targets

┌──────────────────────────────────────────────────────────────────┐
│                     TARGET TYPES                                 │
├──────────────────────────────────────────────────────────────────┤
│                                                                  │
│   COMPUTE:                    MESSAGING:                         │
│   • Lambda                    • SQS                              │
│   • ECS Task                  • SNS                              │
│   • Step Functions            • Kinesis Data Streams             │
│   • Batch Job                 • Kinesis Firehose                 │
│   • EC2 (Create Snapshot)     • EventBridge Bus (another)        │
│                                                                  │
│   ORCHESTRATION:              API:                               │
│   • CodePipeline              • API Gateway                      │
│   • CodeBuild                 • API Destination (HTTP)           │
│   • Systems Manager                                              │
│                                                                  │
│   STORAGE:                    OTHER:                             │
│   • S3 (via Batch)            • CloudWatch Log Group             │
│   • Redshift                  • Incident Manager                 │
│                                                                  │
│   ⚠️ Max 5 targets per rule                                      │
│                                                                  │
└──────────────────────────────────────────────────────────────────┘

4.2 API Destinations

┌─────────────────────────────────────────────────────────────────┐
│                  API DESTINATIONS                               │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Send events to ANY HTTP endpoint:                             │
│                                                                 │
│   EventBridge → API Destination → External API                  │
│                       │                                         │
│                       ↓                                         │
│              ┌─────────────────┐                                │
│              │ Connection      │                                │
│              │ (API Key,       │                                │
│              │  OAuth, Basic)  │                                │
│              └─────────────────┘                                │
│                                                                 │
│   Use Cases:                                                    │
│   • Slack/Teams notifications                                   │
│   • Webhook integrations                                        │
│   • Third-party APIs                                            │
│   • Internal microservices                                      │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

4.3 Input Transformation

┌─────────────────────────────────────────────────────────────────┐
│                INPUT TRANSFORMATION                             │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Transform event before sending to target:                     │
│                                                                 │
│   Original Event:                                               │
│   {                                                             │
│     "detail": {                                                 │
│       "instance-id": "i-123",                                   │
│       "state": "stopped"                                        │
│     }                                                           │
│   }                                                             │
│                                                                 │
│   Input Transformer:                                            │
│   InputPathsMap: { "id": "$.detail.instance-id" }               │
│   InputTemplate: "Instance <id> was stopped!"                   │
│                                                                 │
│   Target receives:                                              │
│   "Instance i-123 was stopped!"                                 │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

5. Event Replay & Archive

5.1 Archive

┌─────────────────────────────────────────────────────────────────┐
│                      ARCHIVE                                    │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Event Bus                       Archive                       │
│   ┌──────────────┐              ┌──────────────┐                │
│   │ Events       │─────────────→│ Stored       │                │
│   │              │              │ Events       │                │
│   └──────────────┘              └──────────────┘                │
│                                       │                         │
│                                       ↓                         │
│                               ┌──────────────┐                  │
│                               │   Replay     │                  │
│                               │ (to event    │                  │
│                               │    bus)      │                  │
│                               └──────────────┘                  │
│                                                                 │
│   Settings:                                                     │
│   • Retention: 1 day - Indefinitely                             │
│   • Filter: Optional event pattern                              │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

5.2 Replay Use Cases

Use CaseDescription
DebuggingReplay events to debug failed processing
TestingTest new rules with historical events
RecoveryRe-process events after fixing bugs
New ConsumersCatch up new services with past events

6. Schema Registry

6.1 Overview

┌─────────────────────────────────────────────────────────────────┐
│                   SCHEMA REGISTRY                               │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Store and manage event schemas:                               │
│                                                                 │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │                                                         │   │
│   │   Events → Schema Discovery → Schema Registry           │   │
│   │                                      │                   │  │
│   │                                      ↓                  │   │
│   │                             ┌─────────────────┐          │  │
│   │                             │ Code Bindings   │          │  │
│   │                             │ (Java, Python,  │          │  │
│   │                             │  TypeScript)    │          │  │
│   │                             └─────────────────┘          │  │
│   │                                                         │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   Features:                                                     │
│   • Auto-discover schemas from events                           │
│   • Version control                                             │
│   • Generate code bindings                                      │
│   • OpenAPI 3.0 / JSONSchema Draft 4                            │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

7. Pipes

7.1 EventBridge Pipes

┌─────────────────────────────────────────────────────────────────┐
│                   EVENTBRIDGE PIPES                             │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Point-to-point integration with optional processing:          │
│                                                                 │
│   Source → (Filter) → (Enrichment) → Target                     │
│                                                                 │
│   ┌────────┐  ┌────────┐  ┌────────────┐  ┌────────┐            │
│   │ Source │→ │ Filter │→ │ Enrichment │→ │ Target │            │
│   │        │  │(pattern)│  │ (Lambda)   │  │       │            │
│   └────────┘  └────────┘  └────────────┘  └────────┘            │
│                                                                 │
│   Sources:             Targets:                                 │
│   • DynamoDB Streams   • Lambda                                 │
│   • Kinesis Streams    • Step Functions                         │
│   • SQS                • API Gateway                            │
│   • Kafka (MSK)        • EventBridge Bus                        │
│   • MQ (ActiveMQ)      • SNS, SQS, Kinesis                      │
│                                                                 │
│   Use Cases:                                                    │
│   • Stream processing with enrichment                           │
│   • Replace Lambda glue code                                    │
│   • DynamoDB → EventBridge integration                          │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

7.2 Pipes vs Rules

PipesRules
ModelPoint-to-pointFan-out
SourcesStreaming (DynamoDB, Kinesis, SQS, Kafka)Event Bus
EnrichmentYes (Lambda, API Gateway, Step Functions)No (use input transformer)
Max Targets15
Use CaseStream to targetEvent routing

8. Scheduler

8.1 EventBridge Scheduler

┌─────────────────────────────────────────────────────────────────┐
│                 EVENTBRIDGE SCHEDULER                           │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   Schedule tasks at scale (millions of schedules):              │
│                                                                 │
│   SCHEDULE TYPES:                                               │
│   ┌─────────────────────────────────────────────────────────┐   │
│   │  Rate-based:    rate(5 minutes)                         │   │
│   │  Cron:          cron(0 12 * * ? *)  ← Every day at noon │   │
│   │  One-time:      at(2024-12-31T23:59:00)                 │   │
│   └─────────────────────────────────────────────────────────┘   │
│                                                                 │
│   vs CloudWatch Events scheduled rules:                         │
│   • More scalable (millions vs thousands)                       │
│   • Better time zone support                                    │
│   • Flexible retry policy                                       │
│   • One-time schedules                                          │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

8.2 Scheduler Targets

Same as EventBridge rules: Lambda, Step Functions, SQS, SNS, etc.


9. Security

9.1 IAM & Resource Policies

┌─────────────────────────────────────────────────────────────────┐
│                     SECURITY                                    │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│   IAM POLICIES:                                                 │
│   • events:PutEvents     → Send events                          │
│   • events:PutRule       → Create rules                         │
│   • events:PutTargets    → Add targets to rules                 │
│                                                                 │
│   RESOURCE-BASED POLICIES:                                      │
│   • On Event Bus: Allow cross-account event sending             │
│   • On Rules: Allow other accounts to manage                    │
│                                                                 │
│   ENCRYPTION:                                                   │
│   • Events encrypted in transit (HTTPS)                         │
│   • Archive encryption with KMS                                 │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

10. Best Practices

10.1 Design Patterns

PatternDescription
Event SourcingStore all events, replay to rebuild state
Fan-outOne event triggers multiple targets
Cross-AccountCentral event bus, distributed consumers
Event EnrichmentAdd context via Pipes + Lambda

10.2 Recommendations

PracticeRecommendation
Event DesignUse consistent naming, include correlation IDs
FilteringFilter at EventBridge, not in target
Dead Letter QueueConfigure DLQ for each target
RetryUse built-in retry with backoff
ArchiveEnable for critical events

11. Common Exam Questions

Q1: EventBridge vs SNS?

A: EventBridge = advanced filtering, schema registry, replay. SNS = simple pub/sub.

Q2: Max targets per rule?

A: 5 targets

Q3: How to integrate with third-party APIs?

A: API Destinations with Connection (auth)

Q4: How to replay past events?

A: Archive events, then create Replay

Q5: EventBridge Pipes vs Rules?

A: Pipes = point-to-point + enrichment. Rules = fan-out routing.

Q6: Cross-account events?

A: Resource-based policy on target event bus


Tài liệu tham khảo