Skip to content

Instantly share code, notes, and snippets.

@nparsons08 nparsons08/lambda.js

Created Aug 24, 2018
What would you like to do?
// Dependencies
const AWS = require('aws-sdk');
const sns = new AWS.SNS();
const moment = require('moment');
exports.handler = (event, context, callback) => {
// Parse incoming event payload
const payload = JSON.parse(event.Records[0].body)[0].new[0];
// This should be your database
const cache = {
actors: ['Nick'],
verbs: {
tweet: 'tweeted'
const actor = cache.actors[0]; // Hardcoding user, should be a database lookup
const verb = cache.verbs[payload.verb]; // Hardcoding verb (should be a ternary or if/else statement)
const tweet = payload.tweet; // The example always contains a tweet
const time = moment(payload.time).format('MMMM Do YYYY [at] h:mm:ss A'); // Time is ISO-8601 formatted
// Build params
const params = {
Message: `${actor} ${verb} "${tweet}" on ${time}`, // SMS message that gets sent to the subscriber (AKA your device)
TopicArn: 'YOUR_AWS_SNS_TOPIC_ARN' // Only works on localhost (hardcode value for AWS deployments)
// Send a SMS via AWS SNS
sns.publish(params, (err, data) => {
if (err) {
return callback(err.stack);
} else {
callback(null, data);
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.