| description | Docs for the sst.EventBus construct in the @serverless-stack/resources package |
|---|
import TabItem from '@theme/TabItem'; import MultiLanguageCode from '@site/src/components/MultiLanguageCode';
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.
new EventBus(scope: Construct, id: string, props: EventBusProps)Parameters
- scope
Construct - id
string - props
EventBusProps
The EventBus construct is designed to make it easy to get started with, while allowing for a way to fully configure it as well. Let's look at how, through a couple of examples.
import { EventBus } from "@serverless-stack/resources";
new EventBus(this, "Bus", {
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: ["src/target1.main", "src/target2.main"],
},
},
});Note that, rule1 here is simply a key to identify the rule.
Add rules after the EventBus has been created.
const bus = new EventBus(this, "Bus", {
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: ["src/target1.main", "src/target2.main"],
},
},
});
bus.addRules(this, {
rule2: {
eventPattern: { source: ["aws.codebuild"] },
targets: ["src/target3.main", "src/target4.main"],
},
});Create an empty EventBus construct and lazily add the rules.
const bus = new EventBus(this, "Bus");
bus.addRules(this, {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: ["src/target1.main", "src/target2.main"],
},
});You can directly pass in the path to the Function.
new EventBus(this, "Bus", {
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: ["src/target1.main"],
},
},
});If you wanted to configure each Lambda function separately, you can pass in the EventBusFunctionTargetProps.
new EventBus(this, "Bus", {
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: [
{
function: {
srcPath: "src/",
handler: "target1.main",
environment: { tableName: table.tableName },
permissions: [table],
},
},
],
},
},
});You can extend the minimal config, to set some function props and have them apply to all the rules.
new EventBus(this, "Bus", {
defaultFunctionProps: {
timeout: 20,
environment: { tableName: table.tableName },
permissions: [table],
},
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: ["src/target1.main", "src/target2.main"],
},
},
});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(this, "Bus", {
defaultFunctionProps: {
timeout: 20,
environment: { tableName: table.tableName },
permissions: [table],
},
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: [
{
function: {
handler: "src/target1.main",
timeout: 10,
environment: { bucketName: bucket.bucketName },
permissions: [bucket],
},
},
"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.
Configure the internally created CDK Target.
new EventBus(this, "Bus", {
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: [
{
function: "src/target1.main",
targetProps: {
retryAttempts: 20,
},
},
],
},
},
});Allow all the targets in the entire EventBus to access S3.
const bus = new EventBus(this, "Bus", {
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: ["src/target1.main", "src/target2.main"],
},
},
});
bus.attachPermissions(["s3"]);Allow one of the targets to access S3.
const bus = new EventBus(this, "Bus", {
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: ["src/target1.main", "src/target2.main"],
},
},
});
bus.attachPermissionsToTarget("rule1", 0, ["s3"]);Here we are referring to the rule using the rule key, rule1.
You can directly pass in a Queue.
const myQueue = new Queue(this, "MyQueue");
new EventBus(this, "Bus", {
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: [myQueue],
},
},
});Configure the internally created CDK Target.
new EventBus(this, "Bus", {
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: [
{
queue: myQueue,
targetProps: {
messageGroupId: "group1",
},
},
],
},
},
});Configure the internally created CDK EventBus instance.
new EventBus(this, "Bus", {
eventBridgeEventBus: {
eventBusName: "MyEventBus",
},
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: ["src/target1.main", "src/target2.main"],
},
},
});Configure the internally created CDK Rule instance.
new EventBus(this, "Bus", {
rules: {
rule1: {
ruleName: "MyRule",
eventPattern: { source: ["aws.codebuild"] },
targets: ["src/target1.main", "src/target2.main"],
},
},
});Override the internally created CDK EventBus instance.
import * as events from "@aws-cdk/aws-events";
new EventBus(this, "Bus", {
eventBridgeEventBus: events.EventBus.fromEventBusArn(
this, "ImportedBus", eventBusArn
),
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: ["src/target1.main", "src/target2.main"],
},
},
});You can create the EventBus construct in one stack, and add rules in other stacks. To do this, expose the EventBus as a class property.
import { EventBus, Stack } from "@serverless-stack/resources";
export class MainStack extends Stack {
constructor(scope, id, props) {
super(scope, id, props);
this.bus = new EventBus(this, "Bus", {
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: ["src/target1.main", "src/target2.main"],
},
},
});
}
}import { EventBus, App, Stack, StackProps } from "@serverless-stack/resources";
export class MainStack extends Stack {
public readonly bus: EventBus;
constructor(scope: App, id: string, props?: StackProps) {
super(scope, id, props);
this.bus = new EventBus(this, "Bus", {
rules: {
rule1: {
eventPattern: { source: ["aws.codebuild"] },
targets: ["src/target1.main", "src/target2.main"],
},
},
});
}
}Then pass the EventBus to a different stack. Behind the scenes, the EventBus Arn is exported as an output of the MainStack, and imported to AnotherStack.
const mainStack = new MainStack(app, "main");
new AnotherStack(app, "another", { bus: mainStack.bus });const mainStack = new MainStack(app, "main");
new AnotherStack(app, "another", { bus: mainStack.bus });Finally, call addRules. Note that the AWS resources for the added routes will be created in AnotherStack.
import { Stack } from "@serverless-stack/resources";
export class AnotherStack extends Stack {
constructor(scope, id, props) {
super(scope, id, props);
props.bus.addRules(this, {
rule2: {
targets: ["src/target3.main", "src/target4.main"],
},
});
}
}import { EventBus, App, Stack, StackProps } from "@serverless-stack/resources";
interface AnotherStackProps extends StackProps {
readonly bus: EventBus;
}
export class AnotherStack extends Stack {
constructor(scope: App, id: string, props: AnotherStackProps) {
super(scope, id, props);
props.bus.addRules(this, {
rule2: {
targets: ["src/target3.main", "src/target4.main"],
},
});
}
}An instance of EventBus contains the following properties.
Type: string
The ARN of the internally created CDK EventBus instance.
Type: string
The name of the internally created CDK EventBus instance.
Type: cdk.aws-events.EventBus
The internally created CDK EventBus instance.
An instance of EventBus contains the following methods.
addRules(scope: cdk.Construct, rules: { [key: string]: EventBusCdkRuleProps })Parameters
- scope
cdk.Construct - rules
{ [key: string]: EventBusCdkRuleProps }
An associative array where the key is a name to identify the rule and the value is the EventBusCdkRuleProps.
attachPermissions(permissions: Permissions)Parameters
- permissions
Permissions
Attaches the given list of permissions to all the targets in all the rules. This allows the functions to access other AWS resources.
Internally calls Function.attachPermissions.
attachPermissionsToTarget(ruleKey: string, targetIndex: number, permissions: Permissions)Parameters
-
ruleKey
string -
targetIndex
number -
permissions
Permissions
Attaches the given list of permissions to a specific target of a rule. This allows that function to access other AWS resources.
Internally calls Function.attachPermissions.
Type : { [key: string]: EventBusCdkRuleProps }, defaults to {}
The rules for this EventBus. Takes an associative array, where the key is a name to identify the rule and the value is the EventBusCdkRuleProps.
Type : cdk.aws-events.EventBusProps | cdk.aws-events.EventBus
Pass in a cdk.aws-events.EventBus value to override the default settings this construct uses to create the CDK EventBus internally.
Or, pass in an instance of the CDK cdk.aws-events.EventBus. SST will use the provided CDK EventBus instead of creating one internally.
Type : FunctionProps, defaults to {}
The default function props to be applied to all the Lambda functions in the targets. If the function is specified for a target, these default values are overridden. Except for the environment, the layers, and the permissions properties, these will be merged.
Type : FunctionDefinition
The function definition used to create the function for this target.
Type : cdk.aws-events-targets.LambdaFunctionProps, defaults to undefined
Or optionally pass in a CDK LambdaFunctionProps. This allows you to override the default settings this construct uses internally to create the target.
Type : Queue
The Queue construct that'll be added as a target to the bus.
Type : cdk.aws-events-targets.SqsQueueProps, defaults to undefined
Or optionally pass in the CDK SqsQueueProps. This allows you to override the default settings this construct uses internally to create the target.
EventBusCdkRuleProps extends cdk.aws-events.RuleProps with the following exceptions.
Type : (FunctionDefinition | EventBusFunctionTargetProps | Queue | EventBusQueueTargetProps)[]
A list of FunctionDefinition, EventBusFunctionTargetProps, Queue, or EventBusQueueTargetProps objects that'll be used to add the targets for the bus.
Use FunctionDefinition or EventBusFunctionTargetProps to add a Lambda function target.
Or, use Queue or EventBusQueueTargetProps to add a Queue target.