SNS tốt cho fan-out đơn giản, nhưng khi bạn cần archive để replay 7 ngày sau, hoặc route event dựa trên content của message, SNS không làm được. Khi bạn cần route event dựa trên nội dung (content-based), archive để replay, hoặc nhận event từ 200+ AWS services + SaaS partners — EventBridge là câu trả lời.


  flowchart LR
    Sources["200+ AWS services<br/>+ Custom app + SaaS"] --> Bus["EventBridge Bus"]
    Bus --> Rules["Rules: event pattern matching"]
    Rules --> Lambda["Lambda"]
    Rules --> SQS["SQS"]
    Rules --> SF["Step Functions"]
    Rules --> API["API Destination<br/>Slack, Datadog..."]
    Bus --> Archive["Archive + Replay"]
import {
  EventBridgeClient,
  PutEventsCommand,
} from "@aws-sdk/client-eventbridge";

await eb.send(
  new PutEventsCommand({
    Entries: [
      {
        Source: "com.myapp.order",
        DetailType: "OrderCreated",
        Detail: JSON.stringify({
          orderId: "ORD-001",
          userId: "alice",
          total: 250000,
        }),
        EventBusName: "myapp-bus",
      },
    ],
  })
);
# Rule: high-value order → Lambda
aws events put-rule --name high-value-order --event-bus-name myapp-bus --event-pattern '{
  "source": ["com.myapp.order"],
  "detail-type": ["OrderCreated"],
  "detail": {"total": [{"numeric": [">", 100]}]}
}'
aws events put-targets --rule high-value-order --event-bus-name myapp-bus \
  --targets "Id=1,Arn=arn:aws:lambda:..."

Archive + Replay + API Destination

aws events create-archive --archive-name order-archive \
  --event-source-arn $BUS_ARN --retention-days 7

aws events create-api-destination --name slack-alert \
  --connection-arn $CONNECTION_ARN \
  --invocation-endpoint https://hooks.slack.com/services/... --http-method POST

Khi nào EventBridge thay vì SNS/SQS

SNS fan-out đơn giản chỉ filter theo message attribute. EventBridge filter theo content của message (event pattern), có thể match JSON path, numeric range, exists/not-exists. Nếu routing logic phức tạp hơn “attribute = value”, EventBridge giảm code xử lý ở consumer.

Pitfall: event pattern quá rộng gây trigger nhầm Lambda nhiều lần. Ví dụ pattern {"source": ["com.myapp"]} match mọi event từ app, kể cả event mà Lambda không cần xử lý. Luôn dùng pattern cụ thể nhất có thể.

EventBridge là event router cho hệ thống event-driven, chọn đúng bus + rule pattern quyết định maintainability về sau.

Bài sau: Phần 28: Step Functions — workflow orchestration