The Internet of Things on AWS



11 June 2018

IoT, what is IoT?

IoT is the Internet of Things. We all have heard about IoT but has anyone heard of Kevin Ashton? Well, let me introduce you to the man who coined the term “Internet of Things”. He is a British technology pioneer. Well in simple terms, the Internet of things is the network of day-to-day usable things around us, to listen and react to it, just like we humans do. Internet of Things basically consists of the following things:

  • Internet
  • Things

The Internet is nothing but the network to connect the things with each other. Things can be anything (Physical) vehicles, home appliances, device and other things embedded with sensors, electronics, software, and actuators for connectivity purposes. Hence, in this blog, I would be walking you through IoT service provided by AWS. Okay, so AWS (Amazon Web Services) is one of the biggest and strong service providers across the world. AWS provides lots of services. Amongst which, we will be looking at AWS-IoT. As AWS asks, “If you knew the state of everything in the world, and could reason on top of the data: What problems would you solve?” The answer to this question is “Anything and everything.” Let me clear your thoughts if you know the state of a device(physical thing), says the main door of a house. If it’s closed then you can utilize this information and show it in an application which is user’s phone who is away from home and wouldn’t it be great if that user knows if the door is opened abruptly just by a notification on his phone. He knows something is wrong. He can call the cops. This is just an example. We can achieve much more, given proper information. pngbase64ec5d641f71f23bff  In IoT, everything is dependent on the state. All that IoT does is collects the data and sends it to the user. Yes, we have to configure what should be sent and what all data needs to be collected from things. IoT works on the publish-subscribe model.

Benefits of AWS-Internet of Things:

  • Connect and manage your devices
  • Secure device connections and data
  • Process and act upon device data

We will be using the AWS IoT Core service for connecting to the device and interacting with the cloud applications and other devices. AWS IoT supports billions of devices and trillions of messages. It can process and route these message to the AWS endpoints and to other devices securely and reliably with the help of MQTT. (MQTT is an ISO standard publish-subscribe-based messaging protocol) 0  Note: All the examples are in Node.js Steps:

1. Import the IoT SDK.

const AWS = require('aws-sdk'); 

const awsIot = require('aws-iot-device-sdk'); 

const iot: new AWS.Iot({ apiVersion: "2015-05-28" }); 

const iotData: new AWS.IotData({ endpoint: yourIoTHost, apiVersion: yourIoTApiVersion });

2. Create thing with the unique name.

/* thingName: The unique thing name */ 

iot.createThing({ thingName: <YourThingName> }, (err, thing) => 

{ if (err) { callback(err, null); } 

else { callback(null, thing); } });


Created things can be managed under Manage >> Things tab on IoT Console.

3. Create a certificate and set it as active.

/** * setAsActive: A boolean flag for specifying whether the certificate is active. */ 

iot.createKeysAndCertificate({ setAsActive: true }, (err, certificate) =>

 { if (err) { callback(err, null); } 

else { callback(null, certificate); } });

4. Create the policy for the thing newly created things.

/** * policyName: The policy name.  * policyDocument: A JSON format data specifying details about policy.  */ iot.createPolicy({ policyName: <YourPolicyName>, policyDocument: <YourPolicyDocument> }, (err, policy) =>

 { if (err) { callback(err, null); } 

else { callback(null, policy); } });

Furthermore the policy, you can add restrictions of publishing and subscribe to the thing. While defining the policy document, you can specify the resources that would be interacting with the thing using “Resource” key from the “Statement” from policy document. It is set to “*” to allow everyone.

5. Attach the policy with its own certificates.

/**  * policyName: The name of the policy to attach.  * target: The target where the policy is attached.  */ iot.attachPolicy({ policyName: <PolicyToAttach>, target: <YourTargetPolicy> } , (err, attachPolicyData) => 

{ if (err) { callback(err, null); }

 else { callback(null, ''); } });

6. Attach the thing principle with its own certificates.

/** * thingName: The name of the thing. * principal: certificate ARN. */ iot.attachThingPrincipal({ thingName:<YourThingName>, principal: <YourPrincipal> }, (err, attachThingData) => 

{ if (err) { callback(err, null); } 

else { callback(null, ''); } });

Publish a message to the thing: AWS provides us with the physical configurable device to publish messages to the thing using AWS IoT Button. Or else we can do this using code as shown below:

/** * topic: The name of the MQTT topic. (thingName) * qos: The Quality of Service (QoS) level. * payload: The state information, in JSON format. (message) */ iotData.publish({ topic: <ThingName>, payload: JSON.stringify({ message:<YourMessage> }), qos: 0 }, (err, data) => { if (err) { callback(err, null); } else { callback(null, “”); } });

Subscribe to the thing:

As one can publish the data to the thing, Consequently one can listen to data using subscribe method of AWS-IoT.

var awsIot = require(‘aws-iot-device-sdk’);

var myThing = awsIot.device({ keyPath: <YourPrivateKeyPath>, certPath: <YourCertificatePath>, caPath: <YourRootCACertificatePath>, clientId: <YourUniqueClientIdentifier>, host: <YourCustomEndpoint> });

myThing .on(‘connect’, function() { console.log(‘connected’); myThing.subscribe(‘topic_to_subscribe’); });

myThing .on(‘message’, function(topic, payload) { console.log(‘message’, topic, payload.toString()); });

Lambda Function: Lambda function is yet another strong service provided by AWS. Basically, this is a function which can be invoked on the rule and can perform any of the given set of actions. It is often termed as Server-less implementation. Lambda functions are based on rules. Therefore, You need to create rules to invoke the lambda function and you need to create a lambda function to perform the specific action which is to be performed on invocation of the function. For creating lambda function you need to create a role in AWS using  IAM Role. Another example for lambda function would be, you can pass the message to a thing, which would invoke a lambda function which would have a push notification trigger as an action. So if you get a message on your thing, you would also get it as a notification on your phone.


List of actions supported by Lambda function can be seen here. While creating rules in AWS IoT, we define two things:

  • Criteria of rule
  • Action to be performed when criteria are matched

Here is the example of a rule: { “rule”: { “sql”: “SELECT * FROM ‘thingName'”, “ruleDisabled”: false, “actions”: [ { “lambda”: { “functionArn”: “ARNofYourFunction” } } ] } } Concluding that AWS provides lots of options for IoT. Example Rule JSON for all actions can be found here. jpegbase6483b508ab721735a3

In addition to the above AWS provides the following services as well:

Request a quote