Skip to content

Bus

Reference doc for the `sst.aws.Bus` component.

The Bus component lets you add an Amazon EventBridge Event Bus to your app.

Create a bus

const bus = new sst.aws.Bus("MyBus");

Add a subscriber

bus.subscribe("MySubscriber", "src/subscriber.handler");

You can link the bus to other resources, like a function or your Next.js app.

new sst.aws.Nextjs("MyWeb", {
link: [bus]
});

Once linked, you can publish messages to the bus from your function code.

app/page.tsx
import { Resource } from "sst";
import { EventBridgeClient, PutEventsCommand } from "@aws-sdk/client-eventbridge";
const eb = new EventBridgeClient({});
await eb.send(new PutEventsCommand({
Entries: [
{
EventBusName: Resource.MyBus.name,
Source: "my.source",
Detail: JSON.stringify({ foo: "bar" }),
}
],
}));

Constructor

new Bus(name, args?, opts?)

Parameters

BusArgs

transform?

Type Object

Transform how this component creates its underlying resources.

transform.bus?

Type EventBusArgs | (args: EventBusArgs, opts: ComponentResourceOptions, name: string) => void

Transform the EventBus resource.

Properties

arn

Type Output<string>

The ARN of the EventBus.

name

Type Output<string>

The name of the EventBus.

nodes

Type Object

The underlying resources this component creates.

nodes.bus

Type EventBus

The Amazon EventBus resource.

SDK

Use the SDK in your runtime to interact with your infrastructure.


This is accessible through the Resource object in the SDK.

  • arn string

    The ARN of the EventBus.

  • name string

    The name of the EventBus.

Methods

subscribe

subscribe(name, subscriber, args?)

Parameters

  • name string

    The name of the subscription.
  • subscriber Input<string | FunctionArgs | “arn:aws:lambda:${string}”>

    The function that’ll be notified.
  • args? BusSubscriberArgs

    Configure the subscription.

Returns Output<BusLambdaSubscriber>

Subscribe to this EventBus.

bus.subscribe("MySubscription", "src/subscriber.handler");

Add a pattern to the subscription.

bus.subscribe("MySubscription", "src/subscriber.handler", {
pattern: {
source: ["my.source", "my.source2"],
price_usd: [{numeric: [">=", 100]}]
}
});

Customize the subscriber function.

bus.subscribe("MySubscription", {
handler: "src/subscriber.handler",
timeout: "60 seconds"
});

Or pass in the ARN of an existing Lambda function.

sst.config.ts
bus.subscribe("arn:aws:lambda:us-east-1:123456789012:function:my-function");

subscribeQueue

subscribeQueue(name, queue, args?)

Parameters

  • name string

    The name of the subscription.
  • queue Input<string | Queue>

    The ARN of the queue or Queue component that’ll be notified.
  • args? BusSubscriberArgs

    Configure the subscription.

Returns Output<BusQueueSubscriber>

Subscribe to this EventBus with an SQS Queue.

For example, let’s say you have a queue.

sst.config.ts
const queue = sst.aws.Queue("MyQueue");

You can subscribe to this bus with it.

sst.config.ts
bus.subscribeQueue(queue);

Add a filter to the subscription.

sst.config.ts
bus.subscribeQueue(queue, {
filter: {
price_usd: [{numeric: [">=", 100]}]
}
});

static subscribe

Bus.subscribe(name, busArn, subscriber, args?)

Parameters

  • name string

    The name of the subscription.
  • busArn Input<string>

    The ARN of the EventBus to subscribe to.
  • subscriber Input<string | FunctionArgs | “arn:aws:lambda:${string}”>

    The function that’ll be notified.
  • args? BusSubscriberArgs

    Configure the subscription.

Returns Output<BusLambdaSubscriber>

Subscribe to an EventBus that was not created in your app.

For example, let’s say you have an existing EventBus with the following ARN.

const busArn = "arn:aws:events:us-east-1:123456789012:event-bus/my-bus";

You can subscribe to it by passing in the ARN.

sst.aws.Bus.subscribe("MySubscription", busArn, "src/subscriber.handler");

Add a pattern to the subscription.

sst.aws.Bus.subscribe("MySubscription", busArn, "src/subscriber.handler", {
pattern: {
price_usd: [{numeric: [">=", 100]}]
}
});

Customize the subscriber function.

sst.aws.Bus.subscribe("MySubscription", busArn, {
handler: "src/subscriber.handler",
timeout: "60 seconds"
});

static subscribeQueue

Bus.subscribeQueue(name, busArn, queue, args?)

Parameters

  • name string

    The name of the subscription.
  • busArn Input<string>

    The ARN of the EventBus to subscribe to.
  • queue Input<string | Queue>

    The ARN of the queue or Queue component that’ll be notified.
  • args? BusSubscriberArgs

    Configure the subscription.

Returns Output<BusQueueSubscriber>

Subscribe to an existing EventBus with a previously created SQS Queue.

For example, let’s say you have an existing EventBus and SQS Queue with the following ARNs.

sst.config.ts
const busArn = "arn:aws:events:us-east-1:123456789012:event-bus/MyBus";
const queueArn = "arn:aws:sqs:us-east-1:123456789012:MyQueue";

You can subscribe to the bus with the queue.

sst.config.ts
sst.aws.Bus.subscribeQueue(busArn, queueArn);

Add a filter to the subscription.

sst.config.ts
sst.aws.Bus.subscribeQueue(busArn, queueArn, {
filter: {
price_usd: [{numeric: [">=", 100]}]
}
});

BusSubscriberArgs

pattern?

Type Input<Object>

Filter the messages that’ll be processed by the subscriber.

If any single property in the pattern doesn’t match an attribute assigned to the message, then the pattern rejects the message.

For example, if your EventBus message contains this in a JSON format.

{
source: "my.source",
detail: {
price_usd: 210.75
},
"detail-type": "orderPlaced",
}

Then this pattern accepts the message.

{
pattern: {
source: ["my.source", "my.source2"],
}
}

pattern.detail?

Type Record<string, any>

A JSON object of “detail” values to match against. “detail” contains the actual data or information associated with the event.

{
pattern: {
detail: {
price_usd: [{numeric: [">=", 100]}]
}
}
}

pattern.detailType?

Type any[]

A list of “detail-type” values to match against. “detail-type” typically defines the kind of event that is occurring.

{
pattern: {
detailType: ["orderPlaced"]
}
}

pattern.source?

Type any[]

A list of “source” values to match against. “source” indicates where the event originated.

{
pattern: {
source: "my.source"
}
}

transform?

Type Object

Transform how this subscription creates its underlying resources.

transform.rule?

Type EventRuleArgs | (args: EventRuleArgs, opts: ComponentResourceOptions, name: string) => void

Transform the EventBus rule resource.

transform.target?

Type EventTargetArgs | (args: EventTargetArgs, opts: ComponentResourceOptions, name: string) => void

Transform the EventBus target resource.