Skip to content

Topic

Source: src/AWS/SNS/Topic.ts

An Amazon SNS topic for fan-out messaging and notifications.

Topic owns the SNS topic lifecycle while raw AWS topic attributes remain available through the attributes prop so the full core pub/sub surface can be configured without waiting on additional typed wrappers. A topic name is auto-generated unless you provide one explicitly.

Standard Topic

import * as SNS from "alchemy/AWS/SNS";
const topic = yield* SNS.Topic("OrdersTopic");

Topic with Display Name

const topic = yield* SNS.Topic("NotificationsTopic", {
attributes: {
DisplayName: "App Notifications",
},
});

FIFO Topic

const topic = yield* SNS.Topic("OrdersFifoTopic", {
fifo: true,
attributes: {
ContentBasedDeduplication: "true",
},
});

Bind publish operations in the init phase and use them in runtime handlers.

// init
const publish = yield* SNS.Publish.bind(topic);
return {
fetch: Effect.gen(function* () {
// runtime
yield* publish({
Message: JSON.stringify({ orderId: "123" }),
Subject: "OrderCreated",
});
return HttpServerResponse.text("Published");
}),
};

Subscribe a Lambda function to process messages published to the topic. The subscription and invoke permissions are created automatically.

// init
yield* SNS.notifications(topic).subscribe((stream) =>
stream.pipe(
Stream.runForEach((message) =>
Effect.log(`Received: ${message.Message}`),
),
),
);