A Pulumi package for creating and managing Amazon Web Services (AWS) cloud resources with infrastructure-as-code.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Amazon SNS is a pub/sub messaging service for application-to-application and application-to-person communication.
import * as aws from "@pulumi/aws";
// Create a topic and subscribe an email
const alertTopic = new aws.sns.Topic("alerts", {
displayName: "Critical Alerts",
});
new aws.sns.TopicSubscription("email-sub", {
topic: alertTopic.arn,
protocol: "email",
endpoint: "ops@example.com",
});
// Subscribe a Lambda function
new aws.sns.TopicSubscription("lambda-sub", {
topic: alertTopic.arn,
protocol: "lambda",
endpoint: processorFunction.arn,
});
// Create FIFO topic for ordered messages
const orderTopic = new aws.sns.Topic("orders", {
fifoTopic: true,
contentBasedDeduplication: true,
});SNS topics enable pub/sub messaging between publishers and subscribers.
class Topic extends pulumi.CustomResource {
constructor(name: string, args?: TopicArgs, opts?: pulumi.CustomResourceOptions);
readonly arn: pulumi.Output<string>;
readonly name: pulumi.Output<string>;
}
interface TopicArgs {
name?: pulumi.Input<string>;
displayName?: pulumi.Input<string>;
fifoTopic?: pulumi.Input<boolean>;
contentBasedDeduplication?: pulumi.Input<boolean>;
kmsMasterKeyId?: pulumi.Input<string>;
tags?: pulumi.Input<{[key: string]: pulumi.Input<string>}>;
}Example - Create topic with encryption
const encryptedTopic = new aws.sns.Topic("secure-notifications", {
displayName: "Secure Notifications",
kmsMasterKeyId: kmsKey.id,
tags: {
Environment: "production",
DataClassification: "sensitive",
},
});Example - FIFO topic for ordered delivery
const orderProcessing = new aws.sns.Topic("order-events", {
name: "order-events.fifo",
fifoTopic: true,
contentBasedDeduplication: true,
displayName: "Order Processing Events",
});
// Publish to FIFO topic requires MessageGroupId
// Example in application code:
// sns.publish({
// TopicArn: orderProcessing.arn,
// Message: JSON.stringify(orderData),
// MessageGroupId: customerId,
// });Subscribe endpoints to receive messages from SNS topics.
class TopicSubscription extends pulumi.CustomResource {
constructor(name: string, args: TopicSubscriptionArgs, opts?: pulumi.CustomResourceOptions);
}
interface TopicSubscriptionArgs {
topic: pulumi.Input<string>;
protocol: pulumi.Input<"email" | "email-json" | "sqs" | "sms" | "lambda" | "http" | "https">;
endpoint: pulumi.Input<string>;
filterPolicy?: pulumi.Input<string>;
}Example - Multiple subscription types
const topic = new aws.sns.Topic("notifications");
// Email subscription
new aws.sns.TopicSubscription("email", {
topic: topic.arn,
protocol: "email",
endpoint: "admin@example.com",
});
// SQS subscription for durable processing
new aws.sns.TopicSubscription("queue", {
topic: topic.arn,
protocol: "sqs",
endpoint: processingQueue.arn,
});
// Lambda subscription for real-time processing
new aws.sns.TopicSubscription("lambda", {
topic: topic.arn,
protocol: "lambda",
endpoint: handlerFunction.arn,
});
new aws.lambda.Permission("sns-invoke", {
action: "lambda:InvokeFunction",
function: handlerFunction.name,
principal: "sns.amazonaws.com",
sourceArn: topic.arn,
});
// HTTP endpoint subscription
new aws.sns.TopicSubscription("webhook", {
topic: topic.arn,
protocol: "https",
endpoint: "https://api.example.com/webhook",
});Example - Filter policy for selective delivery
const events = new aws.sns.Topic("app-events");
// Only receive error events
new aws.sns.TopicSubscription("error-handler", {
topic: events.arn,
protocol: "lambda",
endpoint: errorHandler.arn,
filterPolicy: JSON.stringify({
eventType: ["error", "critical"],
}),
});
// Only receive order events with high value
new aws.sns.TopicSubscription("high-value-orders", {
topic: events.arn,
protocol: "sqs",
endpoint: priorityQueue.arn,
filterPolicy: JSON.stringify({
eventType: ["order"],
orderValue: [{ numeric: [">=", 1000] }],
}),
});Control who can publish to or subscribe to topics.
class TopicPolicy extends pulumi.CustomResource {
constructor(name: string, args: TopicPolicyArgs, opts?: pulumi.CustomResourceOptions);
}
interface TopicPolicyArgs {
arn: pulumi.Input<string>;
policy: pulumi.Input<string>;
}Example - Cross-account publishing
const sharedTopic = new aws.sns.Topic("shared-events");
new aws.sns.TopicPolicy("cross-account", {
arn: sharedTopic.arn,
policy: pulumi.all([sharedTopic.arn]).apply(([topicArn]) =>
JSON.stringify({
Version: "2012-10-17",
Statement: [{
Effect: "Allow",
Principal: {
AWS: "arn:aws:iam::123456789012:root",
},
Action: ["sns:Publish"],
Resource: topicArn,
}],
})
),
});const topic = new aws.sns.Topic("notifications", {
displayName: "Application Notifications",
});
new aws.sns.TopicSubscription("email-sub", {
topic: topic.arn,
protocol: "email",
endpoint: "admin@example.com",
});
export const topicArn = topic.arn;Install with Tessl CLI
npx tessl i tessl/npm-pulumi--aws