
SNS SMS Triggered by NodeJS Lambda. But how?

From diagram above, how can we make it?
So what do we need before implement this? Simple.
- AWS SDK
- Lambda Function NodeJS 12.x
- SNS (Simple notification service)
- Mobile phone :roll:
- Phone number :!:
- Little bit code of Javascript
- Little bit know about IAM (Identity Access Management)






Hit Create role.
So let's get start. I assume you already have AWS account to playing with. Click at the Service :arrow: Search "Lambda"


So after this, simply you can hit Create function on the top right.


Then you can fill the function name with simple-send-sms
, and runtime withNode.js 12.x
.

Use existing role in the radio button that we created in IAM. Find the IAM role with name simple-send-sms-role
.
Hit the Create function button.

Good. Here is our initial code. Now we make some changes.
const AWS = require('aws-sdk');
const sns = new AWS.SNS({apiVersion: '2010-03-31'});
exports.handler = async (event) => {
var snsParams = {
Message: 'How are you brah?',
Subject: "SNS From Lambda",
PhoneNumber: '+6281xxxx'
};
let snsPush
try {
snsPush = await sns.publish(snsParams).promise()
console.log(snsPush)
} catch (e) {
console.log(e)
}
const response = {
statusCode: 200,
snsData: snsPush
};
return response;
};
Later I will explain code above step-by-step. Now, we have to test it.

On top right of the page, we can hit Configure test event.

Fill the event name with test1
just leave the payload as default. Then hit Create. After test created then just hit Test button.
After success send message to your number, then we need to create something cooler. We can set the parameter in the payload and send again to your number.
Change the test event just like this.
{
"message": "How are you brah?",
"subject": "hello world"
}
Change your code also, because you make some changes in your payload.
const AWS = require('aws-sdk');
const sns = new AWS.SNS({apiVersion: '2010-03-31'});
exports.handler = async (event) => {
var snsParams = {
Message: event.message, // here is your message payload.
Subject: "SNS From Lambda",
PhoneNumber: process.env.PHONE_NUMBER // Use environment variables
};
let snsPush
try {
snsPush = await sns.publish(snsParams).promise()
console.log(snsPush)
} catch (e) {
console.log(e)
}
const response = {
statusCode: 200,
snsData: snsPush
};
return response;
};
Now, from code changes above, we change message with payload, and add environment variables in the PhoneNumber
parameter. Then add the environment variables.
Scroll down the Lambda page, then find Environment variables panel.

Just simply hit Edit button or Manage environment variables.

Add the PHONE_NUMBER
variable. This environment variable is flexible, and it will shown in process.env.PHONE_NUMBER
at the code.
Congratulations!! you have complete how to send SMS with Lambda and SNS.
Note:
If you can't send SMS to your number, it may caused of quota limit. For your documentation, you can see this link: https://aws.amazon.com/premiumsupport/knowledge-center/sns-sms-spending-limit-increase/
Github repo: https://github.com/immma/AWS-simple-send-sms
Happy testing everybody!