We announced
Managing Topics in Amazon SNS
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 AWS.SNS
client class:
Prerequisite Tasks
To set up and run this example, you must first complete these tasks:
-
Install Node.js. For more information about installing Node.js, see the Node.js website
. -
Create a shared configurations file with your user credentials. For more information about providing a credentials JSON file, see Loading Credentials in Node.js from the Shared Credentials File.
Creating a Topic
In this example, use a Node.js module to create an Amazon SNS topic. Create a Node.js
module with the file name sns_createtopic.js
. Configure the SDK
as previously shown.
Create an object to pass the Name
for the new topic to the
createTopic
method of the AWS.SNS
client class. To
call the createTopic
method, create a promise for invoking an Amazon SNS
service object, passing the parameters object. Then handle the response
in the promise callback. The data
returned by the promise contains the
ARN of the topic.
// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set region AWS.config.update({ region: "REGION" }); // Create promise and SNS service object var createTopicPromise = new AWS.SNS({ apiVersion: "2010-03-31" }) .createTopic({ Name: "TOPIC_NAME" }) .promise(); // Handle promise's fulfilled/rejected states createTopicPromise .then(function (data) { console.log("Topic ARN is " + data.TopicArn); }) .catch(function (err) { console.error(err, err.stack); });
To run the example, type the following at the command line.
node sns_createtopic.js
This sample 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 Node.js
module with the file name sns_listtopics.js
. Configure the SDK
as previously shown.
Create an empty object to pass to the listTopics
method of the
AWS.SNS
client class. To call the listTopics
method,
create a promise for invoking an Amazon SNS service object, passing the parameters
object. Then handle the response
in the promise callback. The
data
returned by the promise contains an array of your topic
ARNs.
// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set region AWS.config.update({ region: "REGION" }); // Create promise and SNS service object var listTopicsPromise = new AWS.SNS({ apiVersion: "2010-03-31" }) .listTopics({}) .promise(); // Handle promise's fulfilled/rejected states listTopicsPromise .then(function (data) { console.log(data.Topics); }) .catch(function (err) { console.error(err, err.stack); });
To run the example, type the following at the command line.
node sns_listtopics.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 Node.js
module with the file name sns_deletetopic.js
. Configure the SDK
as previously shown.
Create an object containing the TopicArn
of the topic to delete to
pass to the deleteTopic
method of the AWS.SNS
client
class. To call the deleteTopic
method, create a promise for invoking an
Amazon SNS service object, passing the parameters object. Then handle the
response
in the promise callback.
// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set region AWS.config.update({ region: "REGION" }); // Create promise and SNS service object var deleteTopicPromise = new AWS.SNS({ apiVersion: "2010-03-31" }) .deleteTopic({ TopicArn: "TOPIC_ARN" }) .promise(); // Handle promise's fulfilled/rejected states deleteTopicPromise .then(function (data) { console.log("Topic Deleted"); }) .catch(function (err) { console.error(err, err.stack); });
To run the example, type the following at the command line.
node sns_deletetopic.js
This sample 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 Node.js module with the file name
sns_gettopicattributes.js
. Configure the SDK as previously
shown.
Create an object containing the TopicArn
of a topic to delete to pass
to the getTopicAttributes
method of the AWS.SNS
client
class. To call the getTopicAttributes
method, create a promise for
invoking an Amazon SNS service object, passing the parameters object. Then handle the
response
in the promise callback.
// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set region AWS.config.update({ region: "REGION" }); // Create promise and SNS service object var getTopicAttribsPromise = new AWS.SNS({ apiVersion: "2010-03-31" }) .getTopicAttributes({ TopicArn: "TOPIC_ARN" }) .promise(); // Handle promise's fulfilled/rejected states getTopicAttribsPromise .then(function (data) { console.log(data); }) .catch(function (err) { console.error(err, err.stack); });
To run the example, type the following at the command line.
node sns_gettopicattributes.js
This sample 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 Node.js module with the file name
sns_settopicattributes.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 setTopicAttributes
method of the
AWS.SNS
client class. To call the setTopicAttributes
method, create a promise for invoking an Amazon SNS service object, passing the
parameters object. Then handle the response
in the promise
callback.
// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set region AWS.config.update({ region: "REGION" }); // Create setTopicAttributes parameters var params = { AttributeName: "ATTRIBUTE_NAME" /* required */, TopicArn: "TOPIC_ARN" /* required */, AttributeValue: "NEW_ATTRIBUTE_VALUE", }; // Create promise and SNS service object var setTopicAttribsPromise = new AWS.SNS({ apiVersion: "2010-03-31" }) .setTopicAttributes(params) .promise(); // Handle promise's fulfilled/rejected states setTopicAttribsPromise .then(function (data) { console.log(data); }) .catch(function (err) { console.error(err, err.stack); });
To run the example, type the following at the command line.
node sns_settopicattributes.js
This sample code can be found here on
GitHub