- AWS Lambda - Additional Example
- Monitoring and TroubleShooting using Cloudwatch
- AWS Lambda@Edge with CloudFront
- Lambda Function with Custom User Applications
- Lambda Function with Amazon Kinesis
- Lambda Function with CloudTrail
- Lambda Function with Amazon SNS
- Lambda Function with Scheduled Events
- Lambda Function with Amazon DynamoDB
- Lambda Function with Amazon S3
- Working with Amazon API Gateway
- Deleting Lambda Function
- AWS Executing & Invoking Lambda Function
- Creating & Deploying using Serverless Framework
- Creating & Deploying using AWS CLI
- Creating & Deploying using AWS Console
- Configuring Lambda Function
- Function in C#
- Function in Go
- Function in Python
- Function in Java
- Function in NODEJS
- Building the Lambda function
- AWS Lambda - Introduction
- AWS Lambda - Environment Setup
- AWS Lambda - Overview
- AWS Lambda - Home
AWS Lambda Useful resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Using 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
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 −

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 −

Step 2
Cpck Simple Notification Service and Create topic in it.

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

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 −

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.

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.

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.

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

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

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 −

Enter the Subject and Message details as shown below −

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 −

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.

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