How do I create an AWS SNS topic subscription in TypeScript?
In this guide, we will create an AWS SNS topic and subscribe an endpoint to it using Pulumi in TypeScript. We will cover the creation of the SNS topic and the subscription, explaining each step along the way.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create an SNS topic
const snsTopic = new aws.sns.Topic("myTopic", {
name: "my-topic",
});
// Create an SNS topic subscription
const snsSubscription = new aws.sns.TopicSubscription("mySubscription", {
topic: snsTopic.arn, // Reference the ARN of the SNS topic created above
protocol: "email", // The protocol for the endpoint (e.g., email, sms, lambda, etc.)
endpoint: "example@example.com", // The endpoint to receive notifications
});
// Export the ARN of the SNS topic
export const topicArn = snsTopic.arn;
// Export the ARN of the SNS subscription
export const subscriptionArn = snsSubscription.arn;
Key Points
- We used the
aws.sns.Topic
resource to create an SNS topic. - We used the
aws.sns.TopicSubscription
resource to subscribe an endpoint to the SNS topic. - The
protocol
field defines the type of endpoint (e.g., email, sms). - The
endpoint
field specifies the actual endpoint that will receive the notifications. - We exported the ARNs of both the SNS topic and the subscription for reference.
Summary
In this example, we created an SNS topic and subscribed an email endpoint to it using Pulumi in TypeScript. We covered the creation of the SNS topic and subscription, and exported their ARNs for further use. This setup can be easily modified to use different protocols and endpoints as needed.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.