Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AWS Lambda Function to send an SMS message via the Twilio API
console.log('Loading event');
// Twilio Credentials
var accountSid = '';
var authToken = '';
var fromNumber = '';
var https = require('https');
var queryString = require('querystring');
// Lambda function:
exports.handler = function (event, context) {
console.log('Running event');
// Send an SMS message to the number provided in the event data.
// End the lambda function when the send function completes.
SendSMS(event.to, 'Hello from Lambda Functions!',
function (status) { context.done(null, status); });
};
// Sends an SMS message using the Twilio API
// to: Phone number to send to
// body: Message body
// completedCallback(status) : Callback with status message when the function completes.
function SendSMS(to, body, completedCallback) {
// The SMS message to send
var message = {
To: to,
From: fromNumber,
Body: body
};
var messageString = queryString.stringify(message);
// Options and headers for the HTTP request
var options = {
host: 'api.twilio.com',
port: 443,
path: '/2010-04-01/Accounts/' + accountSid + '/Messages.json',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(messageString),
'Authorization': 'Basic ' + new Buffer(accountSid + ':' + authToken).toString('base64')
}
};
// Setup the HTTP request
var req = https.request(options, function (res) {
res.setEncoding('utf-8');
// Collect response data as it comes back.
var responseString = '';
res.on('data', function (data) {
responseString += data;
});
// Log the responce received from Twilio.
// Or could use JSON.parse(responseString) here to get at individual properties.
res.on('end', function () {
console.log('Twilio Response: ' + responseString);
completedCallback('API request sent successfully.');
});
});
// Handler for HTTP request errors.
req.on('error', function (e) {
console.error('HTTP error: ' + e.message);
completedCallback('API request completed with error(s).');
});
// Send the HTTP request to the Twilio API.
// Log the message we are sending to Twilio.
console.log('Twilio API call: ' + messageString);
req.write(messageString);
req.end();
}
@ghost

This comment has been minimized.

Copy link

ghost commented Jul 5, 2015

Thanks for the write up, I was able to use it for my usecase.

@rozhok

This comment has been minimized.

Copy link

rozhok commented Aug 10, 2015

Thank you, this saved me lot of time.

@swam92

This comment has been minimized.

Copy link

swam92 commented Sep 15, 2015

Great

@gbarnabic

This comment has been minimized.

Copy link

gbarnabic commented Sep 25, 2015

I tried this code within an Amazon Echo Alexa Skill and it didn't work. Obviously you have to be able to make external API calls from AWS. I must be missing a critical concept. I can't post on the Amazon discussion forums because I'm a newbie. Any thoughts by anyone who may read this?

@VHERE

This comment has been minimized.

Copy link

VHERE commented Sep 30, 2015

I have tried this code and my message only stays in queued and delivery always fails. can you suggest where i might be doing wrong??

@rcrathore

This comment has been minimized.

Copy link

rcrathore commented Oct 9, 2015

very helpful. thanks

@roydondsouza

This comment has been minimized.

Copy link

roydondsouza commented Feb 4, 2017

For people having timeout issues. Check if your lambda function is configured for a VPC. If yes, check if the subnet has access to the Internet. http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Internet_Gateway.html

@falent

This comment has been minimized.

Copy link

falent commented Mar 27, 2017

roydondsouza could you please write how to setup a VPC for sending SMS with Twilio?

@roydondsouza

This comment has been minimized.

@jwswj

This comment has been minimized.

Copy link

jwswj commented Sep 17, 2017

This was really useful. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.