Aws
AwsIntermediate

AWS EventBridge Patterns: Building Event-Driven Architectures

DeveloperHat Team
4 min read
EventBridgeServerlessIntegrationArchitecture

TL;DR

Learn how to implement event-driven architectures using AWS EventBridge, including patterns, best practices, and real-world examples

import { MermaidWrapper } from '@/components/mermaid-wrapper'

AWS EventBridge (formerly CloudWatch Events) is a serverless event bus service that makes it easy to connect applications using data from your own applications, SaaS applications, and AWS services. This guide explores common EventBridge patterns and best practices.

{

flowchart LR

subgraph Sources["Event Sources"]

direction TB

AWS["fa:fa-cloud AWS Services"]

SaaS["fa:fa-plug SaaS Apps"]

Custom["fa:fa-code Custom Apps"]

end

subgraph EventBridge["Amazon EventBridge"]

direction TB

DefaultBus["Default Event Bus"]

PartnerBus["Partner Event Bus"]

CustomBus["Custom Event Bus"]

Rules["Event Rules & Filters"]

end

subgraph Targets["Service Targets"]

direction TB

Compute["Compute Services"]

subgraph ComputeDetails[" "]

direction TB

Lambda["fa:fa-function Lambda"]

ECS["fa:fa-docker ECS/Fargate"]

end

Messaging["Messaging Services"]

subgraph MessagingDetails[" "]

direction TB

SNS["fa:fa-comment-alt SNS"]

SQS["fa:fa-envelope SQS"]

end

Integration["Integration Services"]

subgraph IntegrationDetails[" "]

direction TB

StepFunctions["fa:fa-sitemap Step Functions"]

EventBridge2["fa:fa-exchange-alt EventBridge"]

end

end

Sources --> EventBridge

EventBridge --> Rules

Rules --> Targets

style EventBridge fill:#FF9900,stroke:#FF9900,color:white

style Rules fill:#232F3E,stroke:#232F3E,color:white

style Lambda fill:#232F3E,stroke:#232F3E,color:white

style ECS fill:#232F3E,stroke:#232F3E,color:white

style SNS fill:#232F3E,stroke:#232F3E,color:white

style SQS fill:#232F3E,stroke:#232F3E,color:white

style StepFunctions fill:#232F3E,stroke:#232F3E,color:white

style EventBridge2 fill:#232F3E,stroke:#232F3E,color:white

}

$1

$1

``javascript

const AWS = require('aws-sdk');

const eventbridge = new AWS.EventBridge();

// Create a scheduled rule

const params = {

Name: 'DailyBackupRule',

ScheduleExpression: 'cron(0 0 * ? )', // Run daily at midnight

State: 'ENABLED',

Targets: [{

Id: 'BackupLambda',

Arn: 'arn:aws:lambda:region:account-id:function:backup-function'

}]

};

eventbridge.putRule(params).promise();

`

$1

`json

{

"source": ["aws.s3"],

"detail-type": ["AWS API Call via CloudTrail"],

"detail": {

"eventSource": ["s3.amazonaws.com"],

"eventName": ["PutObject", "DeleteObject"],

"requestParameters": {

"bucketName": ["my-important-bucket"]

}

}

}

`

$1

`javascript

// Set up cross-account event bus permissions

const permissionParams = {

Action: 'events:PutEvents',

Principal: '111122223333', // Target account ID

StatementId: 'CrossAccountAccess'

};

eventbridge.putPermission(permissionParams).promise();

`

$1

$1

`python

import boto3

import json

eventbridge = boto3.client('events')

def lambda_handler(event, context):

# Send event to EventBridge

response = eventbridge.put_events(

Entries=[

{

'Source': 'custom.myapp',

'DetailType': 'order.created',

'Detail': json.dumps({

'orderId': '12345',

'amount': 99.99,

'customer': 'john.doe'

}),

'EventBusName': 'default'

}

]

)

return response

`

$1

`javascript

// Configure partner event source

const partnerParams = {

Name: 'zendesk-events',

EventBusName: 'partner-events-bus'

};

eventbridge.createPartnerEventSource(partnerParams).promise();

`

$1

`yaml

Resources:

EventRule:

Type: AWS::Events::Rule

Properties:

Name: MyEventRule

EventPattern:

source:

- custom.myapp

Targets:

- Arn: !GetAtt MyLambda.Arn

Id: ProcessEvents

DeadLetterConfig:

Arn: !GetAtt DLQueue.Arn

RetryPolicy:

MaximumRetryAttempts: 3

MaximumEventAgeInSeconds: 3600

`

$1

$1

`python

import boto3

cloudwatch = boto3.client('cloudwatch')

Get EventBridge invocation metrics

response = cloudwatch.get_metric_statistics(

Namespace='AWS/Events',

MetricName='Invocations',

Dimensions=[

{

'Name': 'RuleName',

'Value': 'MyEventRule'

}

],

StartTime='2024-02-01T00:00:00Z',

EndTime='2024-02-15T00:00:00Z',

Period=3600,

Statistics=['Sum']

)

`

$1

`javascript

const testEvent = {

version: '0',

id: 'test-event',

'detail-type': 'order.created',

source: 'custom.myapp',

account: '123456789012',

time: '2024-02-20T12:00:00Z',

region: 'us-east-1',

detail: {

orderId: '12345',

amount: 99.99

}

};

const testParams = {

Event: JSON.stringify(testEvent),

EventPattern: JSON.stringify(eventPattern)

};

eventbridge.testEventPattern(testParams).promise();

``

$1

1. Event Schema Management

- Use consistent event schemas

- Document event patterns

- Version your events

2. Performance Optimization

- Use specific event patterns

- Implement retry policies

- Monitor event age

3. Security Considerations

- Use IAM roles and policies

- Encrypt sensitive data

- Audit event access

4. Cost Management

- Monitor event usage

- Use appropriate event buses

- Clean up unused rules

$1

Common issues and solutions:

1. Event Not Triggering

- Verify event pattern syntax

- Check IAM permissions

- Review CloudWatch Logs

2. Target Invocation Failures

- Monitor DLQ messages

- Check target configuration

- Review retry policies

3. Performance Issues

- Analyze event latency

- Review throttling metrics

- Check event bus quotas

$1

1. [AWS EventBridge Documentation](https://docs.aws.amazon.com/eventbridge/) - Official documentation

2. [Event Patterns](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-event-patterns.html) - Pattern syntax guide

3. [EventBridge Quotas](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-quota.html) - Service limits

4. [EventBridge Schemas](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-schema.html) - Schema registry

5. [EventBridge Security](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-security.html) - Security best practices

6. [EventBridge API Reference](https://docs.aws.amazon.com/eventbridge/latest/APIReference/) - API documentation

7. [EventBridge Targets](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-targets.html) - Target configuration

8. [EventBridge Monitoring](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-monitoring.html) - Monitoring guide

Why This Matters

Understanding the business and technical context helps you make informed decisions rather than blindly following patterns.

Trade-offs to Consider

Every architectural decision involves trade-offs. Consider your specific requirements, team expertise, and scale when evaluating options.

When NOT to Use This

Knowing when a solution doesn't apply is as valuable as knowing when it does. Consider alternatives for your specific situation.

Decision Framework

Use this framework to evaluate whether this approach is right for your use case based on your specific constraints and requirements.