Complex Event Filtering with AWS EventBridge Pipes, Rules and No Custom Code

Listen to this Post

AWS EventBridge is a powerful service that simplifies event-driven architectures by allowing multiple AWS services to work together without the need for custom AWS Lambda functions. One of its key features, EventBridge Pipes, enables you to create “functionless” event-driven architectures, reducing the need for custom code.

Example: EventBridge Pipes in Action

Here’s a practical example of how you can use EventBridge Pipes to filter and route events without writing Lambda functions:


<h1>Create an EventBridge rule to trigger an SNS topic</h1>

aws events put-rule --name "MyEventRule" --event-pattern "{\"source\":[\"aws.ec2\"],\"detail-type\":[\"EC2 Instance State-change Notification\"],\"detail\":{\"state\":[\"stopped\"]}}"

<h1>Add a target to the rule (e.g., an SNS topic)</h1>

aws events put-targets --rule "MyEventRule" --targets "Id"="1","Arn"="arn:aws:sns:us-east-1:123456789012:MyTopic"

EventBridge Scheduler

EventBridge also includes a scheduler that allows you to automate tasks on a recurring or one-time basis. For example, you can schedule an EC2 instance to start or stop at specific times:


<h1>Schedule an EC2 instance to stop every day at 10 PM</h1>

aws scheduler create-schedule --name "StopEC2Instance" --schedule-expression "cron(0 22 * * ? *)" --target "Arn=arn:aws:ec2:us-east-1:123456789012:instance/i-0abcdef1234567890,RoleArn=arn:aws:iam::123456789012:role/EventBridgeSchedulerRole"

Event Filtering with Pipes

EventBridge Pipes allow you to filter and transform events before they reach their destination. Here’s how you can set up a pipe to filter events from an SQS queue and send them to an SNS topic:


<h1>Create an EventBridge pipe</h1>

aws pipes create-pipe --name "MyEventPipe" --source "arn:aws:sqs:us-east-1:123456789012:MyQueue" --target "arn:aws:sns:us-east-1:123456789012:MyTopic" --filter-criteria "{\"Filters\":[{\"Pattern\":\"{\\"source\\":[\\"aws.ec2\\"],\\"detail-type\\":[\\"EC2 Instance State-change Notification\\"],\\"detail\\":{\\"state\\":[\\"stopped\\"]}}\"}]}"

What Undercode Say

AWS EventBridge is a game-changer for building event-driven architectures without the overhead of custom Lambda functions. By leveraging EventBridge Pipes, Rules, and the Scheduler, you can create efficient, scalable, and maintainable workflows. Here are some additional commands and tips to enhance your AWS automation:

  • List EventBridge Rules:
    aws events list-rules
    

  • Describe an EventBridge Rule:

    aws events describe-rule --name "MyEventRule"
    

  • Delete an EventBridge Rule:

    aws events delete-rule --name "MyEventRule"
    

  • Enable EventBridge Rule:

    aws events enable-rule --name "MyEventRule"
    

  • Disable EventBridge Rule:

    aws events disable-rule --name "MyEventRule"
    

  • Create a CloudWatch Alarm for EventBridge Metrics:

    aws cloudwatch put-metric-alarm --alarm-name "EventBridgeErrorAlarm" --metric-name "FailedInvocations" --namespace "AWS/Events" --statistic "Sum" --period 300 --threshold 1 --comparison-operator "GreaterThanOrEqualToThreshold" --evaluation-periods 1 --alarm-actions "arn:aws:sns:us-east-1:123456789012:MyTopic"
    

  • Monitor EventBridge Logs in CloudWatch:

    aws logs filter-log-events --log-group-name "/aws/events/MyEventRule" --filter-pattern "ERROR"
    

By mastering these commands and concepts, you can unlock the full potential of AWS EventBridge and build robust, serverless architectures. For further reading, check out the AWS EventBridge Documentation.

References:

Reported By: Darryl Ruggles – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification ✅Featured Image