├── README.md ├── event.json └── index.js /README.md: -------------------------------------------------------------------------------- 1 | # AWS Lambda Twilio Signal Talk 2 | This short demo shows how you can use the power of AWS Lambda to perform complex tasks based on events. Imagine you have a motion activated camera that uploads a picture to an S3 bucket. Rather than having queues, stateful servers, and other maintenance prone activities just imagine having a function. A function that runs in the cloud whenever you upload a new picture. It checks to see if that picture contains a face and will shoot you an MMS if it does. 3 | 4 | ## Setup 5 | There are a few simple steps to get setup. 6 | 7 | ### S3 Buckets 8 | 9 | ### AWS Lambda Function 10 | 11 | #### OpenCV Library Compilation 12 | You can compile the library on any AWS Amazon Linux instance by following the instructions below or you can download the statically compiled library here: 13 | 14 | ### Twilio Credentials 15 | -------------------------------------------------------------------------------- /event.json: -------------------------------------------------------------------------------- 1 | { 2 | "Records": [ 3 | { 4 | "eventVersion": "2.0", 5 | "eventSource": "aws:s3", 6 | "awsRegion": "us-east-1", 7 | "eventTime": "1970-01-01T00:00:00.000Z", 8 | "eventName": "ObjectCreated:Put", 9 | "userIdentity": { 10 | "principalId": "AIDAJDPLRKLG7UEXAMPLE" 11 | }, 12 | "requestParameters": { 13 | "sourceIPAddress": "127.0.0.1" 14 | }, 15 | "responseElements": { 16 | "x-amz-request-id": "C3D13FE58DE4C810", 17 | "x-amz-id-2": "FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD" 18 | }, 19 | "s3": { 20 | "s3SchemaVersion": "1.0", 21 | "configurationId": "testConfigRule", 22 | "bucket": { 23 | "name": "motion-activated-camera", 24 | "ownerIdentity": { 25 | "principalId": "A3NL1KOZZKExample" 26 | }, 27 | "arn": "arn:aws:s3:::motion-activated-camera" 28 | }, 29 | "object": { 30 | "key": "motion-activated-camera/camera1/1425667473.jpg", 31 | "size": 1024, 32 | "eTag": "d41d8cd98f00b204e9800998ecf8427e" 33 | } 34 | } 35 | } 36 | ] 37 | } 38 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | console.log('Loading event'); 2 | var AWS = require('aws-sdk'); 3 | var async = require('async'); 4 | var util = require('util'); 5 | var cv = require('cv'); 6 | var twilio = require('twilio')('API_KEY')('AUTH_TOKEN') 7 | var s3 = new AWS.S3(); 8 | var rcptNumber = '+16506900657' 9 | var fromNumber = '+12345968901' 10 | 11 | exports.handler = function(event, context) { 12 | console.log("Reading options from event:\n", util.inspect(event, {depth: 5})); 13 | var imgBucket = event.Records[0].s3.bucket.name; 14 | var imgKey = event.Records[0].s3.object.key; 15 | var typeMatch = imgKey.match(/\.([^.]*)$/); 16 | if (!typeMatch) { 17 | console.error('unable to infer image type for key ' + imgKey); 18 | return; 19 | } 20 | var imageType = typeMatch[1]; 21 | if (imageType != "jpg" && imageType != "png") { 22 | console.log('skipping non-image ' + imgKey); 23 | return; 24 | } 25 | async.waterfall([ 26 | function download(next) { 27 | s3.getObject({Bucket: imgBucket, Key: imgKey}, next); 28 | }, 29 | function detect(image, next) { 30 | cv.readImage(image.Body, function(err, im) { 31 | im.detectObject(cv.FACE_CASCADE, {}, function(err, faces) { 32 | if(!faces.length) { 33 | next(false, null); 34 | } else { 35 | for (var i=0;i