{"id":4137,"date":"2022-02-18T13:58:00","date_gmt":"2022-02-18T12:58:00","guid":{"rendered":"https:\/\/blog.besharp.it\/?p=4137"},"modified":"2023-03-24T18:13:15","modified_gmt":"2023-03-24T17:13:15","slug":"aws-iot-core-rules-in-action-a-practical-guide-and-a-real-smart-world-use-case","status":"publish","type":"post","link":"https:\/\/blog.besharp.it\/aws-iot-core-rules-in-action-a-practical-guide-and-a-real-smart-world-use-case\/","title":{"rendered":"AWS IoT Core Rules in action: a practical guide and a real-(smart) world use case"},"content":{"rendered":"\n

Introduction<\/h2>\n\n\n\n

The Internet of Things (IoT) has become one of the coolest and most sought-after terms of recent times in Information Technology. In fact, both in the industrial and retail sectors, we find more and more connected objects accessible to the Internet and constantly controllable.<\/p>\n\n\n\n

Our previous article<\/a> presented an overview of the IoT world focused on AWS. Thanks to its services, it offers us many advantages for developing IoT applications on the cloud, a centralized point of management and monitoring of devices thanks to its managed services, always available, in high availability, and with managed scaling.<\/p>\n\n\n\n

In this article, we will detail a specific part of the IoT ecosystem on AWS: the rules engine.<\/p>\n\n\n\n

Case study: Smart cities<\/h2>\n\n\n\n

The services related to the IoT world on AWS are constantly expanding. This article will analyze the main components acting directly from the console.<\/p>\n\n\n\n

In fact, we will see how to configure a device by simulating the sending of some data from it and then triggering specific actions based on the messages produced by the devices. As discussed in the previous article, these messages are sent and received through the MQTT protocol through a publisher\/subscriber mechanism. The HTTP \/ HTTPS protocol is also supported on AWS, but in IoT contexts of certain types, the MQTT protocol is preferable. We will not go into the merits of this choice since it is not the purpose of this article and was partly treated in the previous one.<\/p>\n\n\n\n

To give context to the practical exercise, let’s imagine creating an IoT application for a Smart Cities project. Therefore, we are talking about an urban scenario in which various types of devices can continuously (and not, based on connectivity) send their data to the cloud and carry out specific actions if evaluated correctly.<\/p>\n\n\n\n

Things setup<\/h2>\n\n\n\n

Let’s start the deep dive by creating objects (Things) on the AWS IoT Core. To enter into the merits of the case study, let’s imagine having to configure sensors installed on street lamps, in the irrigation systems of our cities, or on traffic lights for the intelligent regulation of traffic.<\/p>\n\n\n\n

By entering the AWS IoT section on the console, under the Manage menu item, it will be possible to create objects, precisely Things, by configuring some properties, such as associating them with a group, a category, or a type of billing. In addition, it is possible to assign specific tags (attributes) that can be used throughout the AWS IoT ecosystem. In our case, we have given the name of the city to which the object belongs as an attribute.<\/p>\n\n\n\n

<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

<\/p>\n\n\n\n

During the object creation phase, it will be necessary to download the associated certificates. The certificates will be used by the device itself to forward messages on MQTT topics. In this article, we will not go into the details of how it is possible to download the certificates on the device; for now, we will limit ourselves to considering the case of devices already present in the field with certificates in the field. This part will be better described in the following article (Device management)<\/p>\n\n\n\n

However, we recommend that you save the certificates to a private Amazon S3 bucket, so they can be available just in case. For completeness, the certificates associated with a device can be more than one and it is possible to retrieve them if necessary.<\/p>\n\n\n\n

Device shadow<\/h2>\n\n\n\n

When configuring our devices, we have chosen to use the Classic Shadow.<\/p>\n\n\n\n

What is the shadow of a device? The shadow is the device’s virtual representation, usable by other applications, regardless of their connection status. The same device, web application, and numerous other services can create, update, delete shadows using various channels, including MQTT topics. Shadows are saved on the AWS cloud and, therefore, always available.<\/p>\n\n\n\n

By default, some MQTT topics are available through here you can manage the shadow:<\/p>\n\n\n\n

<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

<\/p>\n\n\n\n

We can obviously create additional MQTT topics for our IoT application, but they will not be able to manage the device shadow.<\/p>\n\n\n\n

How is device data represented in the shadow? It is nothing more than a document in JSON format precisely containing the state of the device, divided into the following parts:<\/p>\n\n\n\n