Managing Topics in Amazon SNS - AWS SDK for JavaScript

The AWS SDK for JavaScript V3 API Reference Guide describes in detail all the API operations for the AWS SDK for JavaScript version 3 (V3).

Managing Topics in Amazon SNS

JavaScript code example that applies to Node.js execution

This Node.js code example shows:

  • How to create topics in Amazon SNS to which you can publish notifications.

  • How to delete topics created in Amazon SNS.

  • How to get a list of available topics.

  • How to get and set topic attributes.

The Scenario

In this example, you use a series of Node.js modules to create, list, and delete Amazon SNS topics, and to handle topic attributes. The Node.js modules use the SDK for JavaScript to manage topics using these methods of the SNS client class:

Prerequisite Tasks

To set up and run this example, you must first complete these tasks:

  • Set up the project environment to run these Node TypeScript examples, and install the required AWS SDK for JavaScript and third-party modules. Follow the instructions on GitHub.

  • Create a shared configurations file with your user credentials. For more information about providing a shared credentials file, see Shared config and credentials files in the AWS SDKs and Tools Reference Guide.

Important

These examples demonstrate how to import/export client service objects and command using ECMAScript6 (ES6).

Creating a Topic

In this example, use a Node.js module to create an Amazon SNS topic.

Create a libs directory, and create a Node.js module with the file name snsClient.js. Copy and paste the code below into it, which creates the Amazon SNS client object. Replace REGION with your AWS Region.

import { SNSClient } from "@aws-sdk/client-sns"; // The AWS Region can be provided here using the `region` property. If you leave it blank // the SDK will default to the region set in your AWS config. export const snsClient = new SNSClient({});

This example code can be found here on GitHub.

Create a Node.js module with the file name create-topic.js. Configure the SDK as previously shown, including installing the required clients and packages.

Create an object to pass the Name for the new topic to the CreateTopicCommand method of the SNS client class. To call the CreateTopicCommand method, create an asynchronous function invoking an Amazon SNS service object, passing the parameters object. The data returned contains the ARN of the topic.

Note

Replace TOPIC_NAME with the name of the topic.

import { CreateTopicCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; /** * @param {string} topicName - The name of the topic to create. */ export const createTopic = async (topicName = "TOPIC_NAME") => { const response = await snsClient.send( new CreateTopicCommand({ Name: topicName }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: '087b8ad2-4593-50c4-a496-d7e90b82cf3e', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // TopicArn: 'arn:aws:sns:us-east-1:xxxxxxxxxxxx:TOPIC_NAME' // } return response; };

To run the example, enter the following at the command prompt.

node create-topic.js

This example code can be found here on GitHub.

Listing Your Topics

In this example, use a Node.js module to list all Amazon SNS topics.

Create a libs directory, and create a Node.js module with the file name snsClient.js. Copy and paste the code below into it, which creates the Amazon SNS client object. Replace REGION with your AWS Region.

import { SNSClient } from "@aws-sdk/client-sns"; // The AWS Region can be provided here using the `region` property. If you leave it blank // the SDK will default to the region set in your AWS config. export const snsClient = new SNSClient({});

This example code can be found here on GitHub.

Create a Node.js module with the file name list-topics.js. Configure the SDK as previously shown, including installing the required clients and packages.

Create an empty object to pass to the ListTopicsCommand method of the SNS client class. To call the ListTopicsCommand method, create an asynchronous function invoking an Amazon SNS service object, passing the parameters object. The data returned contains an array of your topic Amazon Resource Names (ARNs).

import { ListTopicsCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; export const listTopics = async () => { const response = await snsClient.send(new ListTopicsCommand({})); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: '936bc5ad-83ca-53c2-b0b7-9891167b909e', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // Topics: [ { TopicArn: 'arn:aws:sns:us-east-1:xxxxxxxxxxxx:mytopic' } ] // } return response; };

To run the example, enter the following at the command prompt.

node list-topics.js

This sample code can be found here on GitHub.

Deleting a Topic

In this example, use a Node.js module to delete an Amazon SNS topic.

Create a libs directory, and create a Node.js module with the file name snsClient.js. Copy and paste the code below into it, which creates the Amazon SNS client object. Replace REGION with your AWS Region.

import { SNSClient } from "@aws-sdk/client-sns"; // The AWS Region can be provided here using the `region` property. If you leave it blank // the SDK will default to the region set in your AWS config. export const snsClient = new SNSClient({});

This example code can be found here on GitHub.

Create a Node.js module with the file name delete-topic.js. Configure the SDK as previously shown, including installing the required clients and packages.

Create an object containing the TopicArn of the topic to delete to pass to the DeleteTopicCommand method of the SNS client class. To call the DeleteTopicCommand method, create an asynchronous function invoking an Amazon SNS client service object, passing the parameters object.

Note

Replace TOPIC_ARN with the Amazon Resource Name (ARN) of the topic you are deleting.

import { DeleteTopicCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; /** * @param {string} topicArn - The ARN of the topic to delete. */ export const deleteTopic = async (topicArn = "TOPIC_ARN") => { const response = await snsClient.send( new DeleteTopicCommand({ TopicArn: topicArn }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: 'a10e2886-5a8f-5114-af36-75bd39498332', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // } // } };

To run the example, enter the following at the command prompt.

node delete-topic.js

This example code can be found here on GitHub.

Getting Topic Attributes

In this example, use a Node.js module to retrieve attributes of an Amazon SNS topic.

Create a libs directory, and create a Node.js module with the file name snsClient.js. Copy and paste the code below into it, which creates the Amazon SNS client object. Replace REGION with your AWS Region.

import { SNSClient } from "@aws-sdk/client-sns"; // The AWS Region can be provided here using the `region` property. If you leave it blank // the SDK will default to the region set in your AWS config. export const snsClient = new SNSClient({});

This example code can be found here on GitHub.

Create a Node.js module with the file name get-topic-attributes.js. Configure the SDK as previously shown.

Create an object containing the TopicArn of a topic to delete to pass to the GetTopicAttributesCommand method of the SNS client class. To call the GetTopicAttributesCommand method, invoking an Amazon SNS client service object, passing the parameters object.

Note

Replace TOPIC_ARN with the ARN of the topic.

import { GetTopicAttributesCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; /** * @param {string} topicArn - The ARN of the topic to retrieve attributes for. */ export const getTopicAttributes = async (topicArn = "TOPIC_ARN") => { const response = await snsClient.send( new GetTopicAttributesCommand({ TopicArn: topicArn, }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: '36b6a24e-5473-5d4e-ac32-ff72d9a73d94', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // }, // Attributes: { // Policy: '{...}', // Owner: 'xxxxxxxxxxxx', // SubscriptionsPending: '1', // TopicArn: 'arn:aws:sns:us-east-1:xxxxxxxxxxxx:mytopic', // TracingConfig: 'PassThrough', // EffectiveDeliveryPolicy: '{"http":{"defaultHealthyRetryPolicy":{"minDelayTarget":20,"maxDelayTarget":20,"numRetries":3,"numMaxDelayRetries":0,"numNoDelayRetries":0,"numMinDelayRetries":0,"backoffFunction":"linear"},"disableSubscriptionOverrides":false,"defaultRequestPolicy":{"headerContentType":"text/plain; charset=UTF-8"}}}', // SubscriptionsConfirmed: '0', // DisplayName: '', // SubscriptionsDeleted: '1' // } // } return response; };

To run the example, enter the following at the command prompt.

node get-topic-attributes.js

This example code can be found here on GitHub.

Setting Topic Attributes

In this example, use a Node.js module to set the mutable attributes of an Amazon SNS topic.

Create a libs directory, and create a Node.js module with the file name snsClient.js. Copy and paste the code below into it, which creates the Amazon SNS client object. Replace REGION with your AWS Region.

import { SNSClient } from "@aws-sdk/client-sns"; // The AWS Region can be provided here using the `region` property. If you leave it blank // the SDK will default to the region set in your AWS config. export const snsClient = new SNSClient({});

This example code can be found here on GitHub.

Create a Node.js module with the file name set-topic-attributes.js. Configure the SDK as previously shown.

Create an object containing the parameters for the attribute update, including the TopicArn of the topic whose attributes you want to set, the name of the attribute to set, and the new value for that attribute. You can set only the Policy, DisplayName, and DeliveryPolicy attributes. Pass the parameters to the SetTopicAttributesCommand method of the SNS client class. To call the SetTopicAttributesCommand method, create an asynchronous function invoking an Amazon SNS client service object, passing the parameters object.

Note

Replace ATTRIBUTE_NAME with the name of the attribute you are setting, TOPIC_ARN with the Amazon Resource Name (ARN) of the topic whose attributes you want to set, and NEW_ATTRIBUTE_VALUE with the new value for that attribute.

import { SetTopicAttributesCommand } from "@aws-sdk/client-sns"; import { snsClient } from "../libs/snsClient.js"; export const setTopicAttributes = async ( topicArn = "TOPIC_ARN", attributeName = "DisplayName", attributeValue = "Test Topic", ) => { const response = await snsClient.send( new SetTopicAttributesCommand({ AttributeName: attributeName, AttributeValue: attributeValue, TopicArn: topicArn, }), ); console.log(response); // { // '$metadata': { // httpStatusCode: 200, // requestId: 'd1b08d0e-e9a4-54c3-b8b1-d03238d2b935', // extendedRequestId: undefined, // cfId: undefined, // attempts: 1, // totalRetryDelay: 0 // } // } return response; };

To run the example, enter the following at the command prompt.

node set-topic-attributes.js

This example code can be found here on GitHub.