Filtering and routing events between AWS services with EventBridge
TypeScriptAWS EventBridge is a serverless event bus service that makes it easy to connect applications together using data from your applications, integrated Software-as-a-Service (SaaS) applications, and AWS services. EventBridge delivers a stream of real-time data from event sources, such as AWS services, and routes that data to targets like AWS Lambda.
In the context of EventBridge, the event rule determines how events are routed to targets based on either a schedule or an
eventPattern
. TheeventPattern
defines the structure of the JSON event that will trigger the rule.Here's a simple use case in TypeScript using Pulumi: Suppose you want to filter and route events from an AWS service (say, an S3 bucket) to an AWS Lambda function. Whenever an object is uploaded to the S3 bucket, you want an event to be routed to invoke a Lambda function.
Below is a program illustrating how you accomplish this task using Pulumi. This TypeScript program defines an EventBridge rule that filters for S3 bucket events (
s3:ObjectCreated:*
) and routes them to a Lambda function specified by an ARN.import * as aws from "@pulumi/aws"; // Assuming we already have an existing Lambda Function identified by its ARN const lambdaArn = "arn:aws:lambda:us-west-2:123456789012:function:process-upload"; // Create an EventBridge rule that triggers on an S3 object creation const s3EventRule = new aws.cloudwatch.EventRule("s3EventRule", { eventPattern: JSON.stringify({ source: ["aws.s3"], detailType: ["AWS API Call via CloudTrail"], detail: { eventName: ["PutObject", "PostObject"], requestParameters: { bucketName: [] // Specify bucket names here if you want to scope to specific buckets } } }), }); // Define an EventBridge target that points to the Lambda function const s3EventTarget = new aws.cloudwatch.EventTarget("s3EventTarget", { rule: s3EventRule.name, arn: lambdaArn, }); // Grant the EventBridge rule permission to invoke the Lambda function const lambdaPermission = new aws.lambda.Permission("lambdaPermission", { action: "lambda:InvokeFunction", function: lambdaArn, principal: "events.amazonaws.com", sourceArn: s3EventRule.arn, }); // Link the target to the rule s3EventRule.onEvent("s3EventRuleTarget", s3EventTarget);
In this program:
- We define an EventBridge rule (
s3EventRule
) with an event pattern filtering for 'PutObject' and 'PostObject' events from S3 (EventRule). - We create an EventTarget (
s3EventTarget
) specifying the ARN of our Lambda function so that when the rule is triggered, the target is invoked (EventTarget). - We allow EventBridge to invoke our Lambda function by setting up the necessary permissions (
lambdaPermission
).
This program sets up event-driven architecture that invokes a Lambda function whenever an object is created within an S3 bucket. You can customize the
eventPattern
to target specific buckets or any other AWS service events supported by EventBridge. To fit your exact use case, you'll need to replace thelambdaArn
variable with the ARN of your own Lambda function and specify the exact bucket names within theeventPattern
if needed.- We define an EventBridge rule (