1. SNS is the key to good architecture and help builds a pluggable pattern that makes for easy extension of your
application down the road. This is critcal for IoT system design since in many cases you'll want not just
guranteed message delivery but guranteed actions on those messages.
For this lab we'll setup an SNS topic, route messages from AWS IoT to SNS and then use Lambda to process
those messages but we'll also loop in SQS. SQS is used to recover from a failure during lambda processing.
Let's setup a new SNS topic.
Log into the AWS Console.
Open the SNS dashboard and create a new topic, we'll call this "iotSNS".
Make a note of the ARN for this new topic.
Lab 2 - The SNS Hook
Step 1
2. Let's setup a new SQS queue.
Open the SQS dashboard and create a new queue, we'll call this "iotSQS".
Make a note of the ARN for this new queue.
Step 2
3. Subscribe the queue to the SNS topic created earlier. Right the queue and select "Subscribe Queue to
SNS topic".
At this point any messages coming into our SNS topic also get sent to the SQS queue. We are not sending
them to SQS directly from AWS IoT since we want SNS to be our primary entry point.
Step 3
4. Next we'll create a Lambda function to process our messages. The lambda function is not going to act on
incoming messages directly. Instead it will pull batches from the SQS queue. We do this so that incoming
messages are our trigger for message processing.
Open the AWS Lambda dashboard.
Create a new Lambda and use the sns-message blue print.
Your event source will be SNS and use the iotSNS topic.
We'll call our Lambda function, "iotLambda".
You can use the follow sample code to start your lambda function.
console.log('Loading our IoT function ...');
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
var message = event.Records[0].Sns.Message;
console.log('From SNS:', message);
context.succeed(message);
};
When prompted enable your event source now.
Note: If you want to test your lambda function you need to use the following code instead, this code isn't
waiting for a Record object. Again, this is only for testing the lambda -> cloudwatch function.
JavaScript
5. console.log('Loading our IoT function ...');
exports.handler = function(event, context) {
console.log('Received event:', JSON.stringify(event, null, 2));
context.succeed(true);
};
Let's test what we have so far. We'll add a rule and action to our AWS IoT setup that will listen to all topics and
pass that message onto our SNS topic.
Open the AWS IoT dashboard and click Create Resource, select Rule.
Create a rule with the following values:
Name : "thingrule"
Attribute : "*"
Topic Filter : "iot"
Choose the SNS action.
For your SNS target select the iotSNS that we created earlier.
For your role name, either use an existing role with sufficient permissions to call SNS or follow the wizard
to create that for you.
Click Add Action and then Create.
Step 4
JavaScript
6. Modify your thingtest.js to look like the following, notice we added in our topic name (iot) and we're still just
posting some test data.
Step 5
7. var awsIot = require('aws-iot-device-sdk');
var device = awsIot.device({
"host": "data.iot.us-west-2.amazonaws.com",
"port": 8883,
"clientId": "1234",
"thingName": "thingtest",
"caPath": "./root-CA.cer",
"certPath": "./certificate.pem.crt",
"keyPath": "./private.pem.key",
"region": "us-west-2"
});
var message = {
val1: "Value 1",
val2: "Value 2",
val3: "Value 3",
message: "Test Message"
};
device.on('connect', function() {
console.log('Connected!');
setTimeout(function() {
device.publish('iot', JSON.stringify(message));
console.log('Pushed message to Topic...');
}, 2500);
});
Now lets run this
node thingtest.js
We'll want to go see if our message is now in SQS along with the logs for our Lambda function which should
have logged our test data.
Note: If you see messages in SQS but not in Lambda you didn't enable your event source in the Lambda
function.
Here you can see our data being logged so we can now move on to processing the Queue.
JavaScript
Bash