Skip to main content

EventBus

The EventBus construct is a higher level CDK construct that makes it easy to create an EventBridge Event Bus. You can create a bus that has a list of rules and targets. And you can publish messages to it from any part of your serverless app.

You can have two types of targets; Function targets (with a Lambda function) or Queue targets (with an SQS queue). See the examples for more details.

Examplesโ€‹

Using the minimal configโ€‹

import { EventBus } from "@serverless-stack/resources";

new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: "src/function1.handler",
myTarget2: "src/function2.handler"
},
},
},
});

Note that, myRule here is simply a key to identify the rule.

Configuring rulesโ€‹

Lazily adding rulesโ€‹

Add rules after the EventBus has been created.

const bus = new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: "src/target1.main",
myTarget2: "src/target2.main",
},
},
},
});

bus.addRules(this, {
myRule2: {
pattern: { source: ["myevent"] },
targets: {
myTarget3: "src/target3.main",
myTarget4: "src/target4.main",
},
},
});

Configuring the Ruleโ€‹

Configure the internally created CDK Rule instance.

new EventBus(stack, "Bus", {
rules: {
myRule: {
ruleName: "MyRule",
pattern: { source: ["myevent"] },
targets: {
myTarget1: "src/target1.main",
myTarget2: "src/target2.main",
},
},
},
});

Configuring Function targetsโ€‹

Adding targetsโ€‹

You can directly pass in the path to the Function.

new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: "src/target1.main",
},
},
},
});

Specifying function props for all targetsโ€‹

You can extend the minimal config, to set some function props and have them apply to all the rules.

new EventBus(stack, "Bus", {
defaults: {
function: {
timeout: 20,
environment: { tableName: table.tableName },
permissions: [table],
},
},
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: "src/target1.main",
myTarget2: "src/target2.main",
},
},
},
});

Configuring an individual targetโ€‹

Configure each Lambda function separately.

new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: {
function: {
srcPath: "src/",
handler: "target1.main",
environment: { tableName: table.tableName },
permissions: [table],
},
},
},
},
},
});

Note that, you can set the defaultFunctionProps while using the function per target. The function will just override the defaultFunctionProps. Except for the environment, the layers, and the permissions properties, that will be merged.

new EventBus(stack, "Bus", {
defaults: {
function: {
timeout: 20,
environment: { tableName: table.tableName },
permissions: [table],
},
},
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: {
function: {
handler: "src/target1.main",
timeout: 10,
environment: { bucketName: bucket.bucketName },
permissions: [bucket],
},
},
myTarget2: "src/target2.main",
},
},
},
});

So in the above example, the target1 function doesn't use the timeout that is set in the defaultFunctionProps. It'll instead use the one that is defined in the function definition (10 seconds). And the function will have both the tableName and the bucketName environment variables set; as well as permissions to both the table and the bucket.

Configuring the targetโ€‹

Configure the internally created CDK Target.

import { RuleTargetInput } from 'aws-cdk-lib/aws-events';

new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: {
function: "src/target1.main",
cdk: {
target: {
retryAttempts: 20,
message: RuleTargetInput.fromEventPath('$.detail'),
},
},
},
},
},
},
});

In the example above, the function is invoked with the contents of the detail property on the event, instead of the envelope - i.e. the original payload put onto the EventBus.

Attaching permissions for all targetsโ€‹

Allow all the targets in the entire EventBus to access S3.

const bus = new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: "src/target1.main",
myTarget2: "src/target2.main",
},
},
},
});

bus.attachPermissions(["s3"]);

Attaching permissions for a specific targetโ€‹

Allow one of the targets to access S3.

const bus = new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: "src/target1.main",
myTarget2: "src/target2.main",
},
},
},
});

bus.attachPermissionsToTarget("myRule", 0, ["s3"]);

Here we are referring to the rule using the rule key, myRule.

Configuring Queue targetsโ€‹

Specifying the Queue directlyโ€‹

You can directly pass in a Queue.

const myQueue = new Queue(this, "MyQueue");

new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: myQueue
},
},
},
});

Configuring the targetโ€‹

Configure the internally created CDK Target.

new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: {
queue: myQueue,
cdk: {
target: {
messageGroupId: "group1",
},
},
},
},
},
},
});

Receiving AWS eventsโ€‹

When an AWS service in your account emits an event, it goes to your accountโ€™s default event bus.

import * as events from "aws-cdk-lib/aws-events";

new EventBus(stack, "Bus", {
cdk: {
eventBus: events.EventBus.fromEventBusName(
this, "ImportedBus", "default"
),
},
rules: {
myRule: {
pattern: { source: ["aws.codebuild"] },
targets: {
myTarget1: "src/target1.main",
myTarget2: "src/target2.main",
},
},
},
});

Advanced examplesโ€‹

Configuring the EventBusโ€‹

Configure the internally created CDK EventBus instance.

new EventBus(stack, "Bus", {
cdk: {
eventBus: {
eventBusName: "MyEventBus",
},
},
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: "src/target1.main",
myTarget2: "src/target2.main",
},
},
},
});

Importing an existing EventBusโ€‹

Override the internally created CDK EventBus instance.

import * as events from "aws-cdk-lib/aws-events";

new EventBus(stack, "Bus", {
cdk: {
eventBus: events.EventBus.fromEventBusName(
this, "ImportedBus", eventBusArn
),
},
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: "src/target1.main",
myTarget2: "src/target2.main",
},
},
},
});

Using existing Lambda functions as targetsโ€‹

import * as lambda from "aws-cdk-lib/aws-lambda";

new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget: {
cdk: {
function: lambda.Function.fromFunctionName(stack, "ITarget", "my-function"),
},
},
},
},
},
});

Sharing an EventBus across stacksโ€‹

You can create the EventBus construct in one stack, and add rules in other stacks. To do this, return the EventBus from the stack function

stacks/MainStack.ts
import { EventBus, App, StackContext } from "@serverless-stack/resources";

export function MainStack({ stack }: StackContext) {
const bus = new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: "src/target1.main",
myTarget2: "src/target2.main",
},
},
},
});

return {
bus
}
}

Then import the auth construct into another stack with use and call addRules. Note that the AWS resources for the added routes will be created in AnotherStack.

stacks/AnotherStack.ts
import { EventBus, StackContext } from "@serverless-stack/resources";
import { MainStack } from "./MainStack"

export function AnotherStack({ stack }: StackContext) {
const { bus } = use(MainStack);
bus.addRules(stack, {
myRule2: {
targets: {
myTarget3: "src/target3.main",
myTarget4: "src/target4.main",
},
},
});
}

Constructorโ€‹

new EventBus(scope, id, props)

Parameters

EventBusPropsโ€‹

defaults.function?โ€‹

Type : FunctionProps

The default function props to be applied to all the Lambda functions in the EventBus. The environment, permissions and layers properties will be merged with per route definitions if they are defined.

new EventBus(stack, "Bus", {
defaults: {
function: {
timeout: 20,
}
},
});

rules?โ€‹

Type : Record<string, EventBusRuleProps>

The rules for the eventbus

new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget: "src/function.handler"
},
},
},
});

cdk.eventBus?โ€‹

Type : IEventBus | EventBusProps

Override the internally created EventBus

new EventBus(stack, "Bus", {
cdk: {
eventBus: {
eventBusName: "MyEventBus",
},
}
});

cdk.id?โ€‹

Type : string

Allows you to override default id for this construct.

Propertiesโ€‹

An instance of EventBus has the following properties.

eventBusArnโ€‹

Type : string

The ARN of the internally created EventBus instance.

eventBusNameโ€‹

Type : string

The name of the internally created EventBus instance.

idโ€‹

Type : string

cdk.eventBusโ€‹

Type : IEventBus

The internally created CDK EventBus instance.

Methodsโ€‹

An instance of EventBus has the following methods.

addRulesโ€‹

addRules(scope, rules)

Parameters

Add rules after the EventBus has been created.

bus.addRules(stack, {
myRule2: {
pattern: { source: ["myevent"] },
targets: {
myTarget3: "src/function3.handler"
myTarget4: "src/function4.handler"
},
},
});

attachPermissionsโ€‹

attachPermissions(permissions)

Parameters

Add permissions to all event targets in this EventBus.

bus.attachPermissions(["s3"]);

attachPermissionsToTargetโ€‹

attachPermissionsToTarget(ruleKey, targetName, permissions)

Parameters

Add permissions to a specific event bus rule target

const bus = new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: "src/function1.handler"
myTarget2: "src/function2.handler"
},
},
},
});

bus.attachPermissionsToTarget("myRule", 0, ["s3"]);

bindโ€‹

bind(constructs)

Parameters

  • constructs Array<SSTConstruct>

Binds the given list of resources to all event targets in this EventBus.

bus.bind([STRIPE_KEY, bucket]);

bindToTargetโ€‹

bindToTarget(ruleKey, targetName, constructs)

Parameters

  • ruleKey string
  • targetName string
  • constructs Array<SSTConstruct>

Binds the given list of resources to a specific event bus rule target

const bus = new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
targets: {
myTarget1: "src/function1.handler"
myTarget2: "src/function2.handler"
},
},
},
});

bus.bindToTarget("myRule", 0, [STRIPE_KEY, bucket]);

getRuleโ€‹

getRule(key)

Parameters

  • key string

Get a rule

bus.getRule("myRule");

EventBusRulePropsโ€‹

Used to configure an EventBus rule

Fields to match on the detail field

new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { detail: { FOO: 1 } },
},
},
});

pattern.detailType?โ€‹

Type : Array<string>

A list of detailTypes to filter on

new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { detailType: ["foo"] },
},
},
});

pattern.source?โ€‹

Type : Array<string>

A list of sources to filter on

new EventBus(stack, "Bus", {
rules: {
myRule: {
pattern: { source: ["myevent"] },
},
},
});

targets?โ€‹

Type : Record<string, string | Function | Queue | EventBusFunctionTargetProps | EventBusQueueTargetProps>

Configure targets for this rule. Can be a function or queue

new EventBus(stack, "Bus", {
rules: {
myRule: {
targets: {
myTarget1: "src/function.handler",
myTarget2: new EventBus(stack, "MyQueue"),
}
},
},
});

cdk.rule?โ€‹

Type : Omit<RuleProps, "eventBus" | "targets">

Configure the internally created CDK Rule instance.

new EventBus(stack, "Bus", {
rules: {
myRule: {
cdk: {
rule: {
ruleName: "my-rule",
enabled: false,
},
},
targets: {
myTarget1: "test/lambda.handler",
},
},
},
});

EventBusQueueTargetPropsโ€‹

queueโ€‹

Type : Queue

The queue to trigger

new EventBus(stack, "Bus", {
rules: {
myRule: {
targets: {
myTarget: {
type: "queue",
queue: new EventBus(stack, "Queue")
}
}
},
},
});

typeโ€‹

Type : "queue"

String literal to signify that the target is a queue

cdk.target?โ€‹

Type : SqsQueueProps

EventBusFunctionTargetPropsโ€‹

Used to configure an EventBus function target

function?โ€‹

Type : string | Function | FunctionProps

The function to trigger

new EventBus(stack, "Bus", {
rules: {
myRule: {
targets: {
myTarget: { function: "src/function.handler" },
}
},
},
});

type?โ€‹

Type : "function"

String literal to signify that the target is a function

cdk.function?โ€‹

Type : IFunction

cdk.target?โ€‹

Type : LambdaFunctionProps