Close

Building Event-Driven Architectures with Amazon EventBridge

Amazon EventBridge

As businesses scale, they face increasing challenges in maintaining efficient communication between systems, ensuring real-time responsiveness, and building scalable architectures. Event-driven architectures (EDA) address this challenge by allowing services to communicate asynchronously through events.

At the heart of AWS’s event-driven solutions is Amazon EventBridge, a serverless event bus service that simplifies event routing and processing. It helps developers build decoupled, scalable, and cost-effective workflows with minimal operational overhead.

This blog will explore what Amazon EventBridge iswhy it’s beneficial for organizations, and its key features. We will also dive into use cases, including a fraud detection workflow basic simulation, to demonstrate how EventBridge can solve problems effectively

Services Used

Amazon EventBridge Components

  1. Event Bus: Routes events to targets based on rules.
  2. Event Rules: Filter and forward events to the appropriate targets.
  3. EventBridge Pipes: Simplifies point-to-point integration with filtering and enrichment.
  4. Schema Registry: Manages event schemas and enables validation and type-safe code generation.

Other AWS Services

  1. Amazon SQS: Provides durable and reliable message storage for flagged transactions.
  2. Amazon SNS: Sends real-time fraud alerts to the security team.
  3. AWS Lambda: Processes and enriches events by adding additional data, such as customer risk profiles.
  4. Amazon DynamoDB: Stores customer data, such as risk scores, for enrichment during event processing.
  5. AWS CloudWatch: Monitors and debugs workflows, capturing metrics and logs for insights.

Why Use Amazon EventBridge?

1. Decoupled Communication

  • Problem: In tightly coupled systems, if one service fails or changes, it impacts other services.
  • EventBridge Solution: It enables producer-consumer independence, meaning producers (e.g., applications) don’t need to know about the consumers (e.g., notification systems). Events are routed seamlessly, reducing dependencies and increasing flexibility.

2. Real-Time Event Routing

  • Problem: Legacy systems often rely on batch processing, leading to delays in response.
  • EventBridge Solution: EventBridge routes events in near real-time, enabling fast responses to critical business triggers like order processing or fraud detection.

3. Centralized Event Management

  • Problem: Managing event-driven architectures with multiple event sources and targets can become chaotic.
  • EventBridge Solution: It provides a centralized event bus to manage all events, including AWS services, SaaS integrations, and custom applications, in a single place.

4. Cost-Efficient and Serverless

  • Problem: Hosting and managing custom messaging solutions is costly and operationally complex.
  • EventBridge Solution: As a serverless service, you only pay for the events processed, eliminating infrastructure management costs.

5. Flexible Integration with AWS Ecosystem

  • Problem: Developers often need to integrate services like SQS, SNS, and Lambda with minimal code.
  • EventBridge Solution: Built-in integrations make it easy to route events to popular AWS services and external SaaS applications like Zendesk, Datadog, and more

1.Asynchronous Processing

  • When services publish events without waiting for responses, you reduce bottlenecks and improve scalability.
  • Example: An e-commerce system publishes an OrderPlaced event, and different services (inventory, shipping, billing) act on it independently.

2.Real-Time Notifications

  • EventBridge routes critical events to services like SNS or Lambda for instant notification or processing.
  • Example: Sending alerts for high-risk transactions.

3.System Integration

  • EventBridge connects disparate systems, both within AWS and external SaaS tools.
  • Example: Integrating Salesforce or Shopify with your backend workflows.

4.Workflow Orchestration

  • It enables sophisticated workflows where multiple actions depend on an event trigger.
  • Example: Automatically creating customer tickets in Zendesk when certain events occur.

Enable Schema Validation

  • Use the Schema Registry to validate event structure, ensuring only valid data flows through the system.
  • Generate strongly-typed code bindings for safer and faster development.

Use Dead Letter Queues (DLQs)

  • Configure DLQs for event targets to capture failed messages for debugging and reprocessing.

Filter Events Effectively

  • Write specific event patterns to route only relevant events to each target. This reduces noise and processing costs.

Combine EventBridge with Other AWS Services

  • Pair EventBridge with SQS for durable event storage, SNS for notifications, and Lambda for processing.

Plan for Schema Evolution

  • Version schemas in the Schema Registry to accommodate changes while maintaining backward compatibility.

Monitor and Audit with CloudWatch

  • Use CloudWatch metrics and logs to monitor event throughput, error rates, and latency.

Event Bus

  • A channel to receive and route events.
  • Types: Default (AWS events)Custom (application events), and SaaS (third-party events).

Event Rules

  • Filter and route events to one or more targets based on JSON patterns.

EventBridge Pipes

  • Simplifies point-to-point integration with built-in filteringbatching, and enrichment steps.

Schema Registry

  • Stores event schemas to ensure validation and type-safe integration.

Built-In Integrations

  • Native support for AWS services (e.g., Lambda, SQS, SNS) and SaaS platforms (e.g., Datadog, Zendesk)

Use Case: Fraud Detection Workflow Basic Simulation

This is a simplified scenario designed to demonstrate how Amazon EventBridge and its related components work together in an event-driven architecture. In a real production environment, implementing a fraud detection system would require additional considerations such as robust security measures, high availability, scalability, and compliance with industry standards

Scenario

A financial services application detects and blocks fraudulent transactions in real-time:

  1. Event Source: Transactions are published to EventBridge.
  2. Filtering: Rules route only high-risk transactions (e.g., amount > $5,000).
  3. Processing: Lambda calculates a fraud score using customer data from DynamoDB.
  4. Alerts: SNS sends fraud alerts to the security team.
  5. Review: SQS stores flagged transactions for manual review.

Architecture Overview

Key Components:

  • Event Bus: Routes events based on rules.
  • EventBridge Pipe: Filters and enriches flagged events.
  • SQS: Stores flagged transactions for further analysis.
  • SNS: Sends alerts to the security team.
  • DynamoDB: Stores customer data and risk profiles.
  • Lambda: Processes events and calculates fraud scores

Step 1: Define the Transaction Event Schema

Create a schema to validate TransactionEvent:

{
"type": "object",
"properties": {
"transaction_id": { "type": "string" },
"account_id": { "type": "string" },
"amount": { "type": "number" },
"currency": { "type": "string" },
"country": { "type": "string" },
"timestamp": { "type": "string", "format": "date-time" }
},
"required": ["transaction_id", "account_id", "amount", "currency", "country", "timestamp"]
}

Register the schema in the EventBridge Schema Registry

aws schemas create-schema \
--registry-name FraudDetectionRegistry \
--schema-name TransactionEvent \
--type OpenApi3 \
--content file://transaction-schema.json

Step 2: Set Up the Event Bus

1. Create a custom evcent bus

aws events create-event-bus --name FraudDetectionBus

2. Publish a test event

aws events put-events --entries file://transaction-event.json --event-bus-name FraudDetectionBus

Step 3: Create Event Rules

Filter transactions where amount > 5,000:

aws events put-rule \
--name HighValueTransactionRule \
--event-bus-name FraudDetectionBus \
--event-pattern '{
"source": ["com.myapp.transactions"],
"detail": {
"amount": [{"numeric": [">", 5000]}]
}
}'

Step 4: Set Up SQS for Manual Review

  1. Create an SQS queue:
aws sqs create-queue --queue-name FlaggedTransactionsQueue

2. Add SQS as the target:

aws events put-targets \
--rule HighValueTransactionRule \
--event-bus-name FraudDetectionBus \
--targets '[
{"Id": "FlaggedQueue", "Arn": "<SQS_QUEUE_ARN>"}
]'

Step 5: Enrich Events with EventBridge Pipes

1.Write a Lambda function( not mandatory to stricly use Lambda up to your choice) to fetch customer risk scores:

import boto3
import json

dynamodb = boto3.client('dynamodb')

def lambda_handler(event, context):
for record in event['Records']:
transaction = json.loads(record['body'])
account_id = transaction['account_id']
customer_data = dynamodb.get_item(
TableName='CustomerRiskTable',
Key={'account_id': {'S': account_id}}
)
if 'Item' in customer_data:
transaction['risk_score'] = int(customer_data['Item']['risk_score']['N'])
else:
transaction['risk_score'] = 0 # Default risk score : can test different case up to you
print(transaction)
return event

2.Deploy the Lambda function.

3.Create a Pipe for event enrichment:

aws pipes create-pipe \
--name FraudDetectionPipe \
--source <SQS_QUEUE_ARN> \
--target <SNS_TOPIC_ARN> \
--enrichment <LAMBDA_FUNCTION_ARN>

Step 6: Send Fraud Alerts via SNS

  1. Create an SNS topic:
aws sns create-topic --name FraudAlerts

2. Subscribe to the topic

aws sns subscribe \
--topic-arn <SNS_TOPIC_ARN> \
--protocol email \
--notification-endpoint [email protected]

Testing the Workflow

  1. Publish a test transaction
 
aws events put-events --entries file://transaction-event.json --event-bus-name FraudDetectionBus

2. Verify:

 

  • Enrichment logs in CloudWatch.
  • Fraud alert emails from SNS.
  • Flagged transactions in the SQS queue.

Conclusion

Amazon EventBridge provides a robust framework for building real-time, event-driven architectures that are flexible, scalable, and cost-efficient. From simplifying workflows with Pipes to ensuring event validity with Schema Registry, EventBridge is a game-changer for businesses looking to modernize their systems.

By implementing workflows you can unlock the full potential of event-driven architectures to deliver faster, more reliable, and decoupled systems.

 

Further Reading

This blog provides actionable insights and hands-on steps to help you build effective workflows using EventBridge

 

Article author: Guram Jalaghonia