Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 308
Gary Sharpe Avatar asked Jul 17 '15 21:07

Gary Sharpe


People also ask

How do I publish a message to the SNS topic?

Sign in to the Amazon SNS console . In the left navigation pane, choose Topics. On the Topics page, select a topic, and then choose Publish message. The console opens the Publish message to topic page.

Can Lambda trigger an SNS topic?

Amazon SNS and AWS Lambda are integrated so you can invoke Lambda functions with Amazon SNS notifications. When a message is published to an SNS topic that has a Lambda function subscribed to it, the Lambda function is invoked with the payload of the published message.


1 Answers

Yes, you could write a Lambda function that publishes to an SNS topic. The code running in Lambda has access to the full AWS SDK for Java or Javascript, whichever your function is using. You just need to make sure you give the IAM role executing the function access to publish to your topic. In Javascript:

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); }; 

It is also possible to handle SNS messages using Lambda functions. You might take a look at the sns-message function blueprint, offered through the Create a Lambda function button on the Lambda console.

like image 103
James Avatar answered Sep 27 '22 19:09

James