Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

Can you publish a message to an SNS topic using an AWS Lambda function backed by node.js?

1 Answer

0 votes
by (44.4k points)

You can write a Lambda function which publishes to an SNS topic. Lambda functions have full access to the AWS SDK for Java, Javascript, PHP or whichever script you are using to write. You will have to attach an IAM role which will allow the function to access SNS to publish it to your topic. Considering Javascript, it is like this:

console.log("Loading function");

var AWS = require("aws-sdk");

exports.handler = function(event, context) {

    var eventText = JSON.stringify(event, null, 2);

    console.log("Received event:", eventText);

    var sns = new AWS.SNS();

    var params = {

        Message: eventText, 

        Subject: "Test SNS From Lambda",

        TopicArn: "arn:aws:sns:us-west-2:123456789012:test-topic1"

    };

    sns.publish(params, context.done);

};

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...