1. 程式人生 > >Using Continuous Jobs with AWS IoT Device Management

Using Continuous Jobs with AWS IoT Device Management

In an earlier Using Over-the-Air Updates with AWS IoT Device Management blog post, we showed you how to create a simple AWS IoT snapshot job and track its progress. In this post, we will show you step-by-step how to configure and create an AWS IoT continuous job. A continuous job is a long running job that reacts to changes to your deployment targets by automatically deploy job executions. For example, a continuous job can be deployed to an initial group of 100 devices. When new devices are added to your deployment group, those new devices will automatically be notified about the continuous job.

A continuous job is useful when:

  • A factory reset is performed on a device that resets its software to v1. A continuous job can automatically update a device’s software to the latest version after it is reset to v1.
  • A device with an older firmware version is turned on after sitting in the warehouse for several months. A continuous job can update the device firmware to the latest version.
  • A device is recycled. A continuous job can remove all pending job executions on the device.

In the next few sections, we are going to create a continuous job continuous-job-V1-to-V2 to update devices’ firmware version from V1 to V2. Devices with firmware version V1 are grouped by thing group FirmwareV1Group

, and devices with firmware version V2 are grouped by thing group FirmwareV2Group. Once a device successfully updates to firmware V2, a pre-configured Lambda function will automatically remove this device from FirmwareV1Group and add to FirmwareV2Group. While continuous job continuous-job-V1-to-V2 is running, new devices that got added to FirmwareV1Group will also automatically get the V2 firmware update job. The steps are

  1. Create firmware deployment groups
  2. Add thing to the initial deployment group
  3. Configure and test a Lambda function to move device between deployment groups
  4. Create a continuous job for firmware V1 to V2 update

Configure Deployment Groups

First, we are going to create three thing groups, each with a different firmware version (V1, V2, V3).

$ aws iot create-thing-group --thing-group-name "FirmwareV1Group"
$ aws iot create-thing-group --thing-group-name "FirmwareV2Group"
$ aws iot create-thing-group --thing-group-name "FirmwareV3Group"

In this example, the device is registered with thing name MyRaspberryPi. Add MyRaspberryPi to thing group FirmwareV1Group.

$ aws iot add-thing-to-thing-group \
--thing-name "MyRaspberryPi" \
--thing-group-name "FirmwareV1Group"

In a real scenario when a device starts up, we recommend that you run a startup program to bring your device online and initialized its settings. In the startup program, you can add the device to corresponding deployment group. In addition, the program that resets the device to factory settings should also include a step to add the device to corresponding deployment group.

Configure a Lambda Function

We are going to write a Lambda function that is triggered whenever a device is successfully updated from firmware version V1 to V2. This Lambda function does the following:

  • Removes the thing from thing group FirmwareV1Group.
  • Adds the thing to ThingGroup FirmwareV2Group.

First, navigate to the IAM console to create a role, LambdaRoleForV1ToV2FirmwareUpdate, with the following policy. Replace AWS_REGION with your AWS region and <AWS_ACCOUNT_ID> with your AWS account ID.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Resource": "arn:aws:logs:*:*:*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "iot:RemoveThingFromThingGroup"
      ],
      "Resource": "arn:aws:iot:<AWS_REGION>:<AWS_ACCOUNT_ID>:thinggroup/FirmwareV1Group"
    },
    {
      "Effect": "Allow",
      "Action": [
        "iot:AddThingToThingGroup"
      ],
      "Resource": "arn:aws:iot:<AWS_REGION>:<AWS_ACCOUNT_ID>:thinggroup/FirmwareV2Group"
    }
  ]
}

Navigate to the AWS Lambda console. On the Create function page, select Blueprints. Under Blueprints, choose hello-world, and then choose Configure.


Under Basic information, type a name for this Lambda function. For Role, choose the role you created in the previous step (LambdaRoleForV1ToV2FirmwareUpdate), and then choose Create function.


On the next page, update the function code with the following code snippet, and then choose Save.

'use strict';

console.log('Loading function');

// Load the AWS SDK
var AWS = require("aws-sdk");

// Set up the code to call when the Lambda function is invoked
exports.handler = (event, context, callback) => {
    // Log a message to the console, you can view this text in the Monitoring tab
    // in the Lambda console or in the CloudWatch Logs console
    console.log("Received event:", event);
    console.log("Thing Arn is:", event.thingArn);
    
    var iot = new AWS.Iot();
    
    // Remove the thing from V1 group
    var removeParams = {
        thingGroupName: "FirmwareV1Group",
        thingArn: event.thingArn
    };
    iot.removeThingFromThingGroup(removeParams, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else console.log(data);               // successful response
    });
    
    // Add the thing to V2 Group
    var addParams = {
        thingGroupName: "FirmwareV2Group",
        thingArn: event.thingArn
    };
    iot.addThingToThingGroup(addParams, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else console.log(data);               // successful response
    });

};

To configure the trigger for this Lambda function, under Designer, choose AWS IoT. In the Configure triggers section, we will create a custom IoT rule to filter succeeded job executions events for a continuous job with the job ID continuous-job-V1-to-V2. To learn more about Job Events, see Job Events documentation in the AWS IoT Developer Guide. Create a new rule named “SucceededJobExecutionsForV1toV2Update”, and input the following SQL statement for the Rule query statement:

SELECT * FROM 'aws/events/jobExecution/continuous-job-V1-to-V2/succeeded'

After you have configured the trigger, click Add and then click Save.

Test the Lambda Function

To test the Lambda function, configure a test event in the AWS Lambda console. From Select a test event, choose Configure test events.


Name the test event SucceededJobExecution, and replace the test event payload with the following job execution event payload. Replace AWS_REGION with your AWS region and <AWS_ACCOUNT_ID> with your AWS account ID. Choose Create to save your changes.

{
  "eventType": "JOB_EXECUTION",
  "eventId": "ba44f124-61fd-40c8-b747-f7f00a50515c",
  "timestamp": 1519870643,
  "operation": "succeeded",
  "jobId": "continuous-job-V1-to-V2",
  "thingArn": "arn:aws:iot:<AWS_REGION>:<AWS_ACCOUNT_ID>:thing/MyRaspberryPi",
  "status": "SUCCEEDED"
}

After you have created the test event, you are directed back to the AWS Lambda console. Select the SucceededJobExecution test event, and choose Test. You should see the thing MyRaspberryPi is removed from FirmwareV1Group and add to FirmwareV2Group from AWS IoT console. For information about creating a Lambda function and how to monitor and debug a Lambda function with CloudWatch, see Create a Simple Lambda Function in the AWS Lambda Developer Guide.

Create a Continuous Job to Update Firmware

Now create a continuous job, continuous-job-V1-to-V2, on thing group FirmwareV1Group.

$ aws iot create-job \
--job-id "continuous-job-V1-to-V2" \
--targets "arn:aws:iot:<AWS_REGION>:<AWS_ACCOUNT_ID>:thinggroup/FirmwareV1Group" \
--document file://<DIRECTORY_TO_JOB_DOCUMENT>/FirmwareUpdateV1ToV2.json \
--description "Continuous job to update firmware from V1 to V2" \
--target-selection CONTINUOUS

When the continuous-job-V1-to-V2 job is in progress, it will notify all target devices that belong to the FirmwareV1Group thing group about the job. Each device executes the job and reports its job execution status. For each successful job execution, the Jobs service sends a job execution event. The job execution event triggers the Lambda function, which removes devices from FirmwareV1Group and adds them to FirmwareV2Group.

Configure Continuous Deployments

We’ve walked through how to create a continuous job on deployment group FirmwareV1Group to update the firmware version on devices from V1 to V2. We’ve shown how to remove the device from FirmwareV1Group and add it to FirmwareV2Group after the device has successfully executed the job. So how about updating V2 devices to V3?

To bring device firmware up to date, you can create another continuous job, continuous-job-V2-to-V3, on the FirmwareV2Group deployment group, and configure a similar Lambda function to remove a device from FirmwareV2Group and add it to FirmwareV3Group.

Continuous jobs do not have to be incremental. Whether your update model is an incremental update or a direct update to a specific version, it is easy to create a continuous job to bring your devices to the desired state. For example, say you have 10 different firmware versions available: V1 — V10. Due to a compatibility issue between some versions, V1, V2, and V3 can only be directly updated to V7, not to V10. All other versions can be directly updated to V10. With continuous jobs, you can easily set up one job for V1, V2, and V3 deployment groups and update devices to V7. You can set up another job for V4 — V9 deployment groups and update devices to V10. By the time a newer version, V11, is released, you can easily create a third continuous job on the V10 deployment group and update devices to V11.

Conclusion

Leave your feedback in the comments. If you have questions or issues implementing this solution, open a thread on the AWS IoT forum.

相關推薦

Using Continuous Jobs with AWS IoT Device Management

In an earlier Using Over-the-Air Updates with AWS IoT Device Management blog post, we showed you how to create a simple AWS IoT snapshot job and t

Using AWS IoT Device Management in a Retail Scenario to Process Order Requests

In this blog post, we will simulate a common business scenario to show you how to use the group policy feature in AWS IoT Device Management. Speci

Questions fréquentes (FAQ) sur AWS IoT Device Management

Q : Comment IoT Device Management contribue-t-il aux mises à jour des appareils ? IoT Device Management permet aux clients de transf

AWS IoT Device Management _物聯網裝置管理服務

AWS IoT Device Management 對於工業、消費和商業應用(如工業和聯網家庭)都至關重要。藉助 AWS IoT Device Management,客戶可以管理具有各種裝置的大型佇列,如運營技術系統、機器、攝像機、裝置、車輛等等。 工業應用

New- AWS IoT Device Management

AWS IoT and AWS Greengrass give you a solid foundation and programming environment for your IoT devices and applications. The nature of Io

AWS IoT Device Management Features

With AWS IoT Device Management, you can group your device fleet into a hierarchical structure based on function, security requirements, or any

Вопросы и ответы по AWS IoT Device Management 

Вопрос: Как IoT Device Management помогает обновлять устройства? IoT Device Management позволяет удаленно выполнять такие действия,

AWS IoT Device Management Pricing

AWS IoT Device Management’s free tier includes 50 remote actions per month. The AWS Free Tier is available to you for 12 months starting w

AWS IoT Device Management 定價

AWS IoT Device Management 的免費套餐每月包含 50 次遠端操作。 AWS 免費套餐的有效期為 12 個月,從您建立 AWS 賬戶之日算起。如果您的免費使用期結束,或者應用程式用量超出免費使用套餐的範圍,您則需要按上述費率付費。免費套餐支援所有 AWS

AWS IoT Device Management 常見問題

問:什麼是 AWS IoT Device Management? AWS IoT Device Management 是一項服務,讓您可以在 IoT 裝置的整個生命週期內輕鬆而安全地大規模註冊、組織、監控和遠端管理 IoT 裝置。您可以使用 IoT Device

AWS IoT Device Management 功能

您可以通過 AWS IoT Device Management 將軟體和韌體推送到現場中的裝置,以便修補安全漏洞並改進裝置功能。您可以執行批量更新、控制部署速度並設定持續任務來自動更新裝置軟體,使其始終執行最新軟體版本。您還可以遠端發起重啟裝置或恢復出廠設定等操作,以便修復裝置中的軟體

Containerize your IOT application with AWS IOT Analytics

Overview In an earlier blog post about IoT Analytics, we discussed how AWS IoT Analytics enables you to collect, visualize, process, query

AWS IoT Device Defender Features

AWS IoT Device Defender detects unusual device behaviors that may be indicative of a compromise by continuously monitoring high-value security

Configuring Cognito User Pools to Communicate with AWS IoT Core

AWS IoT Core supports certificate-based mutual authentication, custom authorizers, and Amazon Cognito Identity as way to authenticate requests to

AWS IoT Device Defender_IoT裝置安全防護

AWS IoT Device Defender 是一項完全託管的服務,可幫助您保護 IoT 裝置佇列的安全。AWS IoT Device Defender 會不斷稽核您的 IoT 配置,以確保配置始終遵循安全最佳實踐。配置是您設定的一組技術控制,有助於在裝置在與其他裝置和雲通訊時確保資訊保安

In the Works – AWS IoT Device Defender – Secure Your IoT Fleet

Scale takes on a whole new meaning when it comes to IoT. Last year I was lucky enough to tour a gigantic factory that had, on average, one environ

Setting Up Just-in-Time Provisioning with AWS IoT Core

In an earlier blog post about just-in-time registration of device certificates, we discussed how just-in-time registration (JITR) can be used to a

Collect, Structure, and Search Industrial IoT data with AWS IoT SiteWise

AWS IoT SiteWise is a managed service that makes it easy to collect and organize data from industrial equipment at scale. You can easily monitor

Segmenting brain tissue using Apache MXNet with Amazon SageMaker and AWS Greengrass ML Inference

In Part 1 of this blog post, we demonstrated how to train and deploy neural networks to automatically segment brain tissue from an MRI scan in a s

Using Relay with AWS AppSync

If you want to jump right into the example app, you can see the code here. Otherwise, read ahead for a complete walkthrough.BackgroundRelay is a popular cl