Monday, February 7, 2022

Publish and subscribe to AWS SNS topic through NodeJS

 Today, we are going to discuss how to publish a message to AWS SNS topic and how to subscribe to the SNS topic so that the subscriber gets the messages published to the SNS topic. We should follow the below steps:

  1.  Create an user and user group and note down its accessKeyId and secretAccessKey
  2. Assign this user the required permission for SNS handling
  3. Create a topic and note down its ARN
  4. Install aws-sdk libraray
  5. Save user credential in a shared file in your local
  6. Create a simple node express application and create routes for publish and subscribe
Now, lets discuss about each steps here:
1) Create an user and user group and note down its accessKeyId and secretAccessKey: I have discussed in detail about this steps in my previous blog under the heading "File handling in AWS S3 by NodeJs". I am giving the link below. Please go through it.


2) Assign this user the required permission for SNS handling: Here, we can create a custom policy for SNS handling and then assign it to the group to which the user belongs or we can assign a default SNS policy already provided by AWS. Here, I am going to assign default SNS policy.
First, go to the IAM service in AWS console and then click on your User name to whom you want to provide permission. 


Now, click on Add permissions button and then click on "Attach existing policies directly". Now, search for SNS and select "AmazonSNSFullAccess". 
Important Note: This policy selection is only for demo purpose. In real time, we should provide the least required access for any service to an user. Always avoid providing the full access.


3) Create a topic and note down its ARN: Now, go to the SNS service in AWS console and click on the Topics tab present in the left side. Further, click on the Create topic button. In the create topic window, select "Standard" radio button and add your Topic name and display name. You can keep rest of the fields as default.

Now, your topic will be created. You should note down the ARN of the topic. It is going to be used in NodeJS code.

4) Install aws-sdk libraray: Use the command like npm install aws-sdk

5) Save user credential in a shared file in your local: In this steps, we should save the user credential so that node js application can read it while establishing connection with AWS. We can refer below AWS documentation for this step:


6) Create a simple node express application and create routes for publish and subscribe: Here, we should create a node js and express application and expose two routes. 
One is used to subscribe the email id to the SNS topic represented by the ARN of the topic in the code. Once, the route is triggered, it will trigger SNS service to subscribe the given email id to the SNS topic. You will get a confirmation email on this email id from AWS. This email will have a link and you will have to click on the link to complete the subscription.
Second route is used to publish message to the SNS topic and further SNS topic will forward the message to all the subscribed emails. Here, we have to provide the Subject and Body as a part of message. 
I am giving here the code for your reference:

const express = require('express');
const dotenv = require('dotenv');
const path = require('path');
const AWS = require('aws-sdk');

AWS.config.update({region: 'us-east-1'});
const sns = new AWS.SNS();

const app = express();
app.use(express.json());

dotenv.config({
  path: path.join(__dirname, './.env')
});

app.listen(8000, () => {
  console.log(`Listening port 8000`);
});


//Get sns details
app.get('/mysns', (req, res) => res.send({"status":"Ok", sns}));

//Subscribe SNS topic
app.post('/subscribe', async(req, res) => {
  try {
    const params = {
      Protocol: 'Email',
      TopicArn: process.env.TOPIC_ARN,
      Endpoint: req.body.email
    };
    const result = await sns.subscribe(params).promise();
    res.send(result);
  } catch (error) {
    console.log(error);
    throw error;
  }  
});

//Publish to SNS topic
app.post('/publish', async(req, res) => {
  try {
    const params = {
      Subject: req.body.subject,
      TopicArn: process.env.TOPIC_ARN,
      Message: req.body.message
    };
    const result = await sns.publish(params).promise();
    res.send(result);
  } catch (error) {
    console.log(error);
    throw error;
  }  
});



 






No comments:

Post a Comment

Please provide your precious comments and suggestion