The Internet of Things on AWS – Official Blog
Enabling device maintenance across multiple time zones using AWS IoT Jobs
Introduction
A benefit of Internet of things (IoT) technology is the ability to connect your devices to the cloud and easily access them from around the world. While this benefit has unlocked many opportunities, it has also has created a major challenge of keeping devices updated. Managing and maintaining a fleet of IoT devices spread across multiple geographies and time zones is logistically difficult. To help solve this challenge, AWS IoT Jobs offers the ability to programmatically schedule over-the-air (OTA) updates for a global fleet of devices, across time zones, and schedule a time-window for these updates.
With the new recurring maintenance window schedule configuration feature, you can now schedule AWS IoT Jobs to only run in the exact time zone a device is deployed in, within a specific timeframe. For example, let’s say you have devices in factories across the world that need to receive a weekly OTA update, but only when the factories are closed on Saturdays, from 9PM to 6AM. Using a combination of AWS IoT Jobs, dynamic thing groups, and AWS IoT Device Shadow, you can group devices together by the time zone they report they’re in and implement a weekly OTA update to only be applied between 9PM and 6AM device local time on Saturdays.
In this blog post, we’ll detail what steps to take to fully create and enable an OTA update scheduling solution using AWS IoT Jobs, dynamic thing groups, and Device Shadows.
Prerequisites
To follow through this blog post, you will need:
- An AWS account
- Access to an AWS IoT Core supported region
- Access to an AWS IoT Device Management supported region
- Access to AWS CloudShell
Walkthrough
For the demonstration in this post, you will schedule a weekly OTA update that can only run between 9PM and 6AM device local time. To do this, you will create two AWS IoT dynamic thing groups, one for Central Standard Time (CST) and one for Pacific Standard Time (PST), that will each be automatically populated with devices according to their time zone. A device’s time zone will be reported by their corresponding AWS IoT Device Shadow. You will then create and configure two AWS IoT Jobs, one for each dynamic thing group. Each job will have a schedule configuration with the maintenance window set to reoccur weekly from 9PM to 6AM. The illustration below details what this solution will look like once fully implemented.
Step 1: Create AWS IoT dynamic thing groups
To create your AWS IoT dynamic thing groups (AWS CloudShell, AWS CLI):
- Enable AWS IoT Device Management fleet indexing by issuing the update-indexing-configuration command
- For this blog post, you will only need to enable thing indexing for the AWS IoT thing registry and classic shadows. You will use this thing index to create your AWS IoT dynamic thing groups with.
aws iot update-indexing-configuration --thing-indexing-configuration thingIndexingMode=REGISTRY_AND_SHADOW
- For this blog post, you will only need to enable thing indexing for the AWS IoT thing registry and classic shadows. You will use this thing index to create your AWS IoT dynamic thing groups with.
- Create the AWS IoT dynamic thing group for devices in the PST time zone by issuing the create-dynamic-thing-group command
aws iot create-dynamic-thing-group --thing-group-name "pstdevices" --query-string "shadow.reported.timezone:PST"
- Create the AWS IoT dynamic thing group for devices in the CST time zone by issuing the create-dynamic-thing-group command
aws iot create-dynamic-thing-group --thing-group-name "cstdevices" --query-string "shadow.reported.timezone:CST"
Step 2: Configure device time zones
To configure your device’s time zones (AWS CloudShell, AWS CLI):
- Create an AWS IoT thing for the device deployed in the PST time zone by issuing the create-thing command
aws iot create-thing --thing-name "vibrationdevicepsttimezone"
- Create an AWS IoT thing for the device deployed in the CST time zone by issuing the create-thing command
aws iot create-thing --thing-name "vibrationdevicecsttimezone"
- Create an AWS IoT Device Shadow for the device deployed in the PST time zone by issuing the update-thing-shadow command
aws iot-data update-thing-shadow --cli-binary-format raw-in-base64-out --thing-name vibrationdevicepsttimezone --payload '{"state":{"reported":{"timezone":"PST"}}}' "vibrationdevicepsttimezone.txt"
- Create an AWS IoT Device Shadow for the device deployed in the CST time zone by issuing the update-thing-shadow command
aws iot-data update-thing-shadow --cli-binary-format raw-in-base64-out --thing-name vibrationdevicecsttimezone --payload '{"state":{"reported":{"timezone":"CST"}}}' "vibrationdevicecsttimezone.txt"
- Verify that the AWS IoT thing for the device deployed in the PST time zone was added to the pstdevices AWS IoT dynamic group by issuing the list-thing-groups-for-thing command
aws iot list-thing-groups-for-thing --thing-name vibrationdevicepsttimezone
The output from the command should look similar to the following:
{ "thingGroups": [ { "groupName": "pstdevices", "groupArn": "arn:aws:iot:us-east-1:<AWSACCOUNTID>:thinggroup/pstdevices" } ] }
- Verify that the AWS IoT thing for the device deployed in the CST time zone was added to the cstdevices AWS IoT dynamic group by issuing the list-thing-groups-for-thing command
aws iot list-thing-groups-for-thing --thing-name vibrationdevicecsttimezone
The output from the command should look similar to the following:
{ "thingGroups": [ { "groupName": "cstdevices", "groupArn": "arn:aws:iot:us-east-1:<AWSACCOUNTID>:thinggroup/cstdevices" } ] }
Step 3: Create AWS IoT Jobs
To create your AWS IoT Jobs (console):
-
- Open the AWS IoT console
- On the left-hand navigation bar, under Manage, Remote Actions, choose Jobs
- Choose Create Job
- Select Create custom job
- Enter a job name of pstdevicesiotjob
- For Thing groups to run this job, choose pstdevices
- Choose the Job template you wish to use for this demonstration
- Choose Next
- For Job run Type, select Continuous
- Under Additional configurations, choose Scheduling configuration
- For Job start, enter today’s date and a time one hour ahead of your current time
- For Recurring maintenance window, select Weekly
- For On these weekdays, select Saturday
- For Maintenance window starts from, enter a time of 21:00 (9:00PM in 24-hour format)
- For Maintenance window duration, enter 9 hours and 0 minutes (ending job executions at 6:00AM)
Your console should be similar to the following screenshot: - Choose Submit
- Repeat steps 2 through 15 using the following:
- Set job name to cstdevicesiotjob
- Set Thing groups to run this job to cstdevices
- Choose the same Job template you used in step 7
- Choose Submit
- Under Status, ensure both jobs are showing as Scheduled
Your console should be similar to the following screenshot:
The status of each job will change to In progress after your configured Maintenance window starts from date and time passes. Each job will then start a job execution on every device within the targeted AWS IoT dynamic thing group at 9PM device local time on Saturday and end job executions at 6AM on Sunday. These job executions will occur on every current and newly added device until the Maintenance window duration time has passed. Once the Maintenance window duration time has passed, the status of each job will change back to Scheduled. This process will occur weekly until you cancel each job.
Cleaning Up
Be sure to remove the resources created in this blog to avoid charges.
From your AWS CloudShell environment issue the following commands:
-
aws iot delete-thing —thing-name "vibrationdevicepsttimezone"
-
aws iot delete-thing —thing-name "vibrationdevicecsttimezone"
-
aws iot delete-job --job-id "pstdevicesiotjob"
-
aws iot delete-job --job-id "cstdevicesiotjob"
-
aws iot delete-dynamic-thing-group --thing-group-name "pstdevices"
-
aws iot delete-dynamic-thing-group --thing-group-name "cstdevices"
-
aws iot update-indexing-configuration --thing-indexing-configuration thingIndexingMode=OFF
Conclusion
In this post you learned how to deploy an OTA update within an approved timeframe to a fleet of devices that are deployed across multiple time zones using AWS IoT Jobs, dynamic thing groups, and Device Shadows. Using this approach, you can remotely deploy updates to your fleet of devices, and get the peace of mind that each device will start the update at the correct time and apply it only during your sanctioned time window. For a more in depth look at AWS IoT Jobs, check out our Get Started with AWS IoT workshop and this blog post on how to Schedule remote operations using AWS IoT Device Management Jobs.
Author bio
Nicholas Switzer is an IoT Specialist Solutions Architect at Amazon Web Services. He joined AWS in 2022 and specializes in IoT and Edge Computing and the connected product space. He is based in the US and enjoys building smart products that improve everyday life.