SNS/SQS for node.js

How to interface SNS, SQS for node.js to AWS?

If you want to be able to create a SNS/SQS bindings from code and not to do it from the AWS Console with node.js you can do it like this.

var AWS = require('aws-sdk'), util = require('util');

// configure AWS
AWS.config.update({
    'region': 'us-east-1',
    'accessKeyId': 'akId',
    'secretAccessKey': 'secretKey'
});

var sns = new AWS.SNS().client;
var sqs = new AWS.SQS().client;

This will be enough to initialize the required data to be able to connect to the system.

First let’s create a topic:

sns.createTopic({
    'Name': 'demo'
}, function (err, result) {

    if (err !== null) {
        console.log(util.inspect(err));
        return;
    }

    console.log(util.inspect(result));

});

The following is the output:

{
    ResponseMetadata: {
        RequestId: 'bdceeca8-bbb6-511f-b186-a54e7c61fb0e'
    },
    TopicArn: 'arn:aws:sns:us-east-1:<AWSID>:demo'
}

Now that we have the TopicArn we can proceed to create the SQS Queue.

sqs.createQueue({
    'QueueName': 'demo'
}, function (err, result) {

    if (err !== null) {
        console.log(util.inspect(err));
        return;
    }

    console.log(util.inspect(result));

});

The output is:

{
    ResponseMetadata: {
        RequestId: '07d264eb-e8c5-5821-9720-2d0d215a8652'
    },
    QueueUrl: 'https://sqs.us-east-1.amazonaws.com/<AWSID>/demo'
}

Now we have the QueueUrl which we can use to get the ARN for the Queue

sqs.getQueueAttributes({
    QueueUrl: 'https://sqs.us-east-1.amazonaws.com/<AWSID>/demo',
    AttributeNames: ["QueueArn"]
}, function (err, result) {

    if (err !== null) {
        console.log(util.inspect(err));
        return;
    }

    console.log(util.inspect(result));

});

This will give us the QueueArn

{
    ResponseMetadata: {
        RequestId: '05be9746-9f9c-5c5a-94e6-dea0550f02f7'
    },
    Attribute: {
        Name: 'QueueArn',
        Value: 'arn:aws:sqs:us-east-1:<AWSID>:demo'
    }
}

Now we have everything we need to create a subscription and policy.

First let’s create the subscription

sns.subscribe({
    'TopicArn': 'arn:aws:sns:us-east-1:<AWSID>:demo',
    'Protocol': 'sqs',
    'Endpoint': 'arn:aws:sqs:us-east-1:<AWSID>:demo'
}, function (err, result) {

    if (err !== null) {
        console.log(util.inspect(err));
        return;
    }

    console.log(util.inspect(result));

});

The output from this query is:

{
    ResponseMetadata: {
        RequestId: '136057ff-0423-5f63-b42c-1e19c80948ad'
    },
    SubscriptionArn: 'arn:aws:sns:us-east-1:<AWSID>:demo:7825661f-130e-4e0c-bc29-9ad397660aaa'
}

This will enable us to publish to the SQS Queue, the only thing left now is to add the permissions that will enable the Topic to write to the SQS Queue.** **

var queueUrl = "https://sqs.us-east-1.amazonaws.com/<AWSID>/demo";
var topicArn = "arn:aws:sns:us-east-1:<AWSID>:demo";
var sqsArn = "arn:aws:sqs:us-east-1:<AWSID>:demo";

var attributes = {
    "Version": "2008-10-17",
    "Id": sqsArn + "/SQSDefaultPolicy",
    "Statement": [{
            "Sid": "Sid" + new Date().getTime(),
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "SQS:SendMessage",
            "Resource": sqsArn,
            "Condition": {
                "ArnEquals": {
                    "aws:SourceArn": topicArn
                }
            }
        }
    ]
};

sqs.setQueueAttributes({
    QueueUrl: queueUrl,
    Attributes: {
        'Policy': JSON.stringify(attributes)
    }
}, function (err, result) {

    if (err !== null) {
        console.log(util.inspect(err));
        return;
    }

    console.log(util.inspect(result));
});

And the response is:

{
    ResponseMetadata: {
        RequestId: '1487064f-38fa-55dd-a772-4fa9f5530f48'
    }
}

Now we have everything setup so when we publish something in a topic the queue will also be populated.