English 中文(简体)
Lambda Function with Amazon SNS
  • 时间:2024-09-17

Using Lambda Function with Amazon SNS


Previous Page Next Page  

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

    Pubpsh 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 pubpsh, 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

Cpck Simple Notification Service and Create topic in it.

Notification Service

Step 3

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

New Topic

Step 4

Enter the Topic name and Display name and cpck 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 cpck role from left side as shown below.

Role Permission

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

Added Popcies

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.

Pubpsh to Topic to Activate Trigger

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

Pubpsh to Topic

First Select name of the topic you want to pubpsh. Cpck on Pubpsh to topic button −

Pubpsh Topic

Enter the Subject and Message details as shown below −

Message Details

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

Check Message Details in CloudWatch Service

Log intoAWS console and open CloudWatch service. Cpck 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.pubpsh({
      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.pubpsh({
   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

Cpck Pubpsh message to pubpsh the message. You see a message on the phone number given as follows −

Pubpsh Message Advertisements