Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Shivansh Singh
Published

AWS Cloud Enabled Burglar Alarm

Idea is to use AWS IoT button as a switch when pressed sends notification to the homeowner that someone has tried to enter the home.

BeginnerFull instructions provided4,246
AWS Cloud Enabled Burglar Alarm

Things used in this project

Story

Read more

Schematics

Architecture of the Project

Code

Lambda Function

JavaScript
This is the lambda function created on AWS Lambda service. The function is a NodeJS function. When invoked, this function publishes a message (represented by "Message") to the SNS topic defined as "TopicArn".
var aws = require('aws-sdk');
var sns = new aws.SNS();

// handler method
exports.handler = function(event, context) {
    console.log('processed by lambda');
    var params = {
        Message: 'AWS IoT Button',
        // Below is sent as text message
        Subject: 'Alert! Front door is opened.',
        //You will have to create a topic defined below.
        TopicArn: 'arn:aws:sns:us-east-1:123456789:aws-iot-button-sns'
    };
    
    sns.publish(params, function(err, data) {
        if (err) {
            context.fail('ERROR:Calling SNS from Lambda: ' + err)
        } else {
            console.log('Great success! JSON: ' + JSON.stringify(data, null, ' '));
            context.succeed('SUCCESS');
        }
    });
};

AWS IAM Role policy

JSON
This is the IAM role policy for the lambda function, to allow it to execute and publish messages to the SNS topic.
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:*:*:*"
    },
    {
        "Sid": "Stmt145193837000",
        "Effect": "Allow",
        "Action": [
            "sns:Publish"
        ],
        "Resource": [
            "arn:aws:sns:us-east-1:123456789553:aws-iot-button-sns"
        ]
    }
  ]
}

Credits

Shivansh Singh

Shivansh Singh

6 projects • 23 followers

Comments