Lambda Function with Amazon SNS

 Amazon SNS is a service used for push notification. In this chapter, we will explain working of AWS Lambda and Amazon SNS with the help of an example where will perform the following actions −

  • Create Topic in SNS Service and use AWS Lambda Add Topics to CloudWatch
  • Send SNS text message on phone number given.
Requisites

To create Topic in SNS Service and use AWS Lambda Add Topics to CloudWatch, we need not follow the steps given below −

  • Create Topic in SNS
  • Create Role for permission in IAM
  • Create AWS Lambda Function
  • Publish to topic to activate trigger
  • Check the message details in CloudWatch service.

To send SNS text message on phone number given, we need to do the following −

Add code in AWS Lambda to send message to your phone.

Example

In this example, we will create a topic in SNS. When details are entered in the topic to publish, AWS Lambda is triggered. The topic details are logged in CloudWatch and a message is sent on phone by AWS Lambda.

Here is a basic block diagram which explains the same −

Block Diagram Sns
Create Topic in SNS

You will have to follow the steps given below to create topic in SNS −

Step 1

Login to AWS Console and go to SNS service in Amazon as shown below −

Amazon Sns Service
Step 2

Click Simple Notification Service and Create topic in it.

Notification Service
Step 3

Then, you have to click Create new topic button as shown −

New Topic
Step 4

Enter the Topic name and Display name and click on Create topic. You should see the topic name in the display as follows −

Topic Name
Create Role for Permission in IAM

To create a Role to work with AWS Lambda and SNS service, we need to login to AWS console. Then, select IAM from Amazon services and click role from left side as shown below.

Role Permission

Observe that we have added policies for SNS, Lambda and CloudWatch. Add rolename and click Create role button to complete the process of role creation.

Added Policies
Create AWS Lambda Function

In this section, let us understand how to create AWS Lambda function using nodejs as the runtime.

For this purpose, login to AWS console and choose AWS Lambda from AWS services. Add the function name, role details etc and create the AWS Lambda function as shown.

Create Lambda
Add SNS Trigger

To add SNS trigger, enter SNS configuration details as shown −

Add Sns

Then, select SNS topic and Add the trigger to AWS Lambda function as shown −

Sns Topic

Then, add AWS lambda code given below −

exports.handler = function(event, context, callback) {
   console.log("AWS lambda and SNS trigger ");
   console.log(event);
   const sns = event.Records[0].Sns.Message;
   console.log(sns)
   callback(null, sns);
};

In the above code, event.Records[0].Sns.Message gives the message details added. We have added console logs to see them in CloudWatch. Now, save the Lambda function with required memory and time allocation.

Publish to Topic to Activate Trigger

Recall that we have already created topic in SNS in Step 1. We will now publish in the topic and see the details in CloudWatch which will be triggered by AWS Lambda −

Publish to Topic

First Select name of the topic you want to publish. Click on Publish to topic button −

Publish Topic

Enter the Subject and Message details as shown below −

Message Details

You can also select JSON message format to send in JSON style. Click Publish the message button at the end of the screen.

Check Message Details in CloudWatch Service

Log intoAWS console and open CloudWatch service. Click on logs on left side and select the logs for AWS Lambda function created. You can find the following display for the logs with messages created as shown above −

Check Message
Add Code in AWS Lambda to Send Message to your Phone

Here will use SNS Text messaging to send message on the phone using AWS Lambda. You can use the following code to update AWS Lambda code as follows −

const aws =  require("aws-sdk");
const sns = new aws.SNS({
   region:'us-east-1'
});
exports.handler = function(event, context, callback) {
   console.log("AWS lambda and SNS trigger ");
   console.log(event);
   const snsmessage = event.Records[0].Sns.Message;
   console.log(snsmessage);
   sns.publish({
      Message: snsmessage,
      PhoneNumber: '+911212121212'
   }, function (err, data) {
      if (err) {
         console.log(err);
         callback(err, null);
      } else {
         console.log(data);
         callback(null, data);
      }	
   });
};

We have added AWS SDK and the SNS service to use to send message. The message from the event coming from SNS is send as text message on the phone number given.

Observe the following code for example −

sns.publish({
   Message: snsmessage,
   PhoneNumber: '+911212121212'
}, function (err, data) {
   if (err) {
      console.log(err);
      callback(err, null);
   } else {
      console.log(data);
      callback(null, data);
   }	
});

Enter the topic now to see the message in cloudwatch and the phone number given above.

Enter Topic

Click Publish message to publish the message. You see a message on the phone number given as follows −

Publish Message

Comments

Popular posts from this blog

Java : Variables Declaring

LINUX Move and copy files using SSH

SQL Self JOIN