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:

  1. An AWS account
  2. Access to an AWS IoT Core supported region
  3. Access to an AWS IoT Device Management supported region
  4. 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.

A diagram that shows two devices being added to two separate AWS IoT dynamic thing groups, with two separate AWS IoT Jobs running on each device according to the timezone they're in.

Step 1: Create AWS IoT dynamic thing groups

To create your AWS IoT dynamic thing groups (AWS CloudShell, AWS CLI):

  1. 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
  2. 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"
  3. 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):

  1. 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"
  2. 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"
  3. 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"
  4. 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"
  5. 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"
            }
        ]
    }
  6. 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):

    1. Open the AWS IoT console
    2. On the left-hand navigation bar, under Manage, Remote Actions, choose Jobs
    3. Choose Create Job
    4. Select Create custom job
    5. Enter a job name of pstdevicesiotjob
    6. For Thing groups to run this job, choose pstdevices
    7. Choose the Job template you wish to use for this demonstration
    8. Choose Next
    9. For Job run Type, select Continuous
    10. Under Additional configurations, choose Scheduling configuration
    11. For Job start, enter today’s date and a time one hour ahead of your current time
    12. For Recurring maintenance window, select Weekly
    13. For On these weekdays, select Saturday
    14. For Maintenance window starts from, enter a time of 21:00 (9:00PM in 24-hour format)
    15. 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:A screenshot of the AWS IoT Console where Scheduling configuration parameter Job start is set to today's date, one hour ahead, Recurring maintenance window is set to Weekly, On these weekdays is set to Saturday, Maintenance window starts from is set to 21:00, and Maintenance window duration is set to 9 hours and 0 minutes.
    16. Choose Submit
    17. 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
    18. Under Status, ensure both jobs are showing as Scheduled
      Your console should be similar to the following screenshot:

A screenshot of the AWS IoT Console where the AWS IoT Job is showing a status of Scheduled.

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:

  1. aws iot delete-thing —thing-name "vibrationdevicepsttimezone"
  2. aws iot delete-thing —thing-name "vibrationdevicecsttimezone"
  3. aws iot delete-job --job-id "pstdevicesiotjob"
  4. aws iot delete-job --job-id "cstdevicesiotjob"
  5. aws iot delete-dynamic-thing-group --thing-group-name "pstdevices"
  6. aws iot delete-dynamic-thing-group --thing-group-name "cstdevices"
  7. 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.