0 votes
1 view
in AWS by (19.2k 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.6k 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);


Welcome to Intellipaat Community. Get your technical queries answered by top developers !