IoT applications rely on vast arrays of intelligent, networked devices, but this makes them vulnerable to availability and connectivity challenges.
Applications need a stable network to communicate with IoT devices in real time. Otherwise, devices can disconnect or be overwhelmed with requests from multiple applications. This can lead to synchronization and status issues that can impair IoT data collection and usage.
The AWS IoT Device Shadow service is designed to address these connectivity and synchronization challenges by acting as a communication link between applications and IoT devices. Let’s take a closer look at the service and consider how it can enhance an organization's IoT applications on AWS.
Learn device shadow basics
A device shadow is a file written in open JSON format that acts as a repository to cache detailed status and intended state information for an IoT device. Thus, a shadow file retains the current state of the device, as well as the desired state of the device.
Every IoT device has an associated shadow. Both the device and the application can read and write to the shadow. The AWS IoT platform keeps each shadow file updated with data sent to and from the device. Thus, with proper network connectivity, an application can send commands and gather data directly from the IoT device.
However, if the IoT device is busy or offline, the application can still issue commands or change configurations for the IoT device by writing to the shadow file. It can also read the latest data written to the file by the device.
When the connection is reestablished, or the device is no longer busy, it can read the shadow file to implement any changes or write new data to it. In effect, a shadow file ensures every IoT device is always logically available to the application -- whether or not the physical device is really available. This makes device shadows particularly attractive for application use cases with intermittent connectivity, low bandwidth, limited computing or uncertain power sources.
Inside a device shadow file
With AWS IoT services, it's important to recognize the overall content of the device shadow file, which consists of six general elements: the device state, metadata, a timestamp, a client token, version information and possibly the addition of arrays to hold data being exchanged.
Device shadows consist of desired and reported states. The desired state represents the intended or future state of the IoT device, while the reported state contains the current state of the IoT device. An application can write to the device shadow file to set or update the desired state of a device without connecting or communicating directly with the device. A connected device will write reported state information to the device shadow file, which the applications can then read, even if that device is no longer connected.
Shadow files also add metadata about the desired and reported state contents. This could include timestamps for each attribute, so devices and applications can determine precisely when each attribute was updated.
Additional timestamps note when messages are sent by AWS IoT. When used in conjunction with data about desired and reported state, an IoT device can determine the age of each attribute and whether new content should replace current content.
A client token is a unique identifier that can be applied to an IoT device. The identifier allows the device to associate requests and responses through communication protocols, such as MQTT. This enables the IoT device to determine which messages are intended for the particular device.
Version designations are updated every time the file receives a request or a response. Version designations help ensure the correct version is used. This can prevent an application from writing over a device shadow with an older version.
Finally, device shadow files support the inclusion of arrays, which can hold data intended to be passed between the IoT device and the application. However, an array is treated as an all-or-nothing data structure, so writing one means replacing the entire array, rather than just updating individual elements.
How device shadows work in AWS
The AWS Device Shadow service lets organizations store the state of IoT devices for up to a year. Shadow files can be maintained indefinitely if they're updated, but they will expire within a year if they aren't.
Software developers can use the device shadow function in AWS IoT Core in several ways. For example, they can use the AWS API to create cloud and mobile workloads that gather data and send commands to IoT devices without dealing with the underlying communications programming involved. This also simplifies programming work, because the API is agnostic to the underlying communication and security protocols used by the specific IoT devices.
Developers can opt to have applications communicate directly with IoT devices using the Device Gateway and Rules Engine in AWS IoT Core. However, the device shadow function is usually tied to a registry in AWS IoT Core. The registry provides a logical and searchable identification for each IoT device. It can also hold device metadata, such as models, manufacturers, support information, certificates and even the shadow file. As a result, developers can organize and track devices to improve IoT management tasks.
Use these AWS Device Shadow tasks
There are essentially three tasks that you can perform with a device shadow: get, update and delete.
The get action accesses the last state stored in the shadow file and returns the complete JSON document and all metadata to the requester. In effect, it displays the current or reported state of an IoT device.
The delete action removes the JSON document file and its contents from the data store. This is useful when an IoT device is removed from service. Once deleted, a device shadow file cannot be recovered, but it can be re-created.
The update action sets the desired state of the device represented in the shadow. If the shadow does not yet exist, this action will create the shadow. Data is accompanied by a timestamp, enabling other applications and devices to determine the difference between the desired and reported states.