通过使用 Amazon CLI 创建自定义任务模板 - Amazon IoT Core
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

通过使用 Amazon CLI 创建自定义任务模板

本主题介绍如何使用 Amazon CLI 创建、删除任务模板以及检索有关详细信息。

从头开始创建任务模板

以下 Amazon CLI 命令说明了如何使用存储在 S3 存储桶 (S3DownloadRole) 中的任务文档 (job-document.json)) 和具有从 Amazon S3 下载文件的权限的角色创建任务。

aws iot create-job-template \ --job-template-id 010 \ --document-source https://s3.amazonaws.com/my-s3-bucket/job-document.json \ --timeout-config inProgressTimeoutInMinutes=100 \ --job-executions-rollout-config "{ \"exponentialRate\": { \"baseRatePerMinute\": 50, \"incrementFactor\": 2, \"rateIncreaseCriteria\": { \"numberOfNotifiedThings\": 1000, \"numberOfSucceededThings\": 1000}}, \"maximumPerMinute\": 1000}" \ --abort-config "{ \"criteriaList\": [ { \"action\": \"CANCEL\", \"failureType\": \"FAILED\", \"minNumberOfExecutedThings\": 100, \"thresholdPercentage\": 20}, { \"action\": \"CANCEL\", \"failureType\": \"TIMED_OUT\", \"minNumberOfExecutedThings\": 200, \"thresholdPercentage\": 50}]}" \ --presigned-url-config "{\"roleArn\":\"arn:aws:iam::123456789012:role/S3DownloadRole\", \"expiresInSec\":3600}"

可选的 timeout-config 参数指定每个设备完成任务运行所具有的时间。计时器在任务执行状态设置为 IN_PROGRESS 时启动。如果任务执行状态未在时间到期之前设置为其它最终状态,则会设置为 TIMED_OUT

进行中的计时器无法更新,将应用到该任务的所有任务启动。只要任务启动保持在 IN_PROGRESS 状态的时间长度超过了此间隔,任务启动就会失败,并切换为最终 TIMED_OUT 状态。Amazon IoT 还将发布 MQTT 通知。

有关创建任务推出和中止相关配置的更多信息,请参阅任务推出和中止配置

注意

在您创建任务时,系统会检索指定为 Amazon S3 文件的任务文档。如果在创建任务后更改任务文档源 Amazon S3文件的内容,发送到任务目标的内容不会更改。

从现有任务创建任务模板

以下 Amazon CLI 命令通过指定某个现有任务的 Amazon Resource Name (ARN) 来创建任务模板。新任务模板将使用任务中指定的所有配置。或者,您可以使用任何可选参数更改现有任务中的任何配置。

aws iot create-job-template \ --job-arn arn:aws:iot:region:123456789012:job/job-name \ --timeout-config inProgressTimeoutInMinutes=100

获取有关任务模板的详细信息

以下 Amazon CLI 命令将获取有关指定任务模板的详细信息。

aws iot describe-job-template \ --job-template-id template-id

该命令将显示以下输出。

{ "abortConfig": { "criteriaList": [ { "action": "string", "failureType": "string", "minNumberOfExecutedThings": number, "thresholdPercentage": number } ] }, "createdAt": number, "description": "string", "document": "string", "documentSource": "string", "jobExecutionsRolloutConfig": { "exponentialRate": { "baseRatePerMinute": number, "incrementFactor": number, "rateIncreaseCriteria": { "numberOfNotifiedThings": number, "numberOfSucceededThings": number } }, "maximumPerMinute": number }, "jobTemplateArn": "string", "jobTemplateId": "string", "presignedUrlConfig": { "expiresInSec": number, "roleArn": "string" }, "timeoutConfig": { "inProgressTimeoutInMinutes": number } }

列出任务模板

以下 Amazon CLI 命令将列出您Amazon Web Services 账户中的所有任务模板。

aws iot list-job-templates

该命令将显示以下输出。

{ "jobTemplates": [ { "createdAt": number, "description": "string", "jobTemplateArn": "string", "jobTemplateId": "string" } ], "nextToken": "string" }

要检索其它结果页面,使用 nextToken 字段的值。

删除任务模板

以下 Amazon CLI 命令将删除指定的任务模板。

aws iot delete-job-template \ --job-template-id template-id

该命令不显示任何输出。

从自定义任务模板创建任务。

以下 Amazon CLI 命令将从自定义任务模板创建任务。它的目标是名为 thingOne 的设备,并将指定任务模板的 Amazon Resource Name (ARN),用作该任务的基础。您可以覆盖高级配置(如超时和取消配置),方法是传递 create-job 命令的关联参数。

警告

仅当从 Amazon 托管式模板创建任务时,document-parameters 对象才能与 create-job 命令一起使用。此对象不得与自定义任务模板一起使用。有关演示如何使用此参数创建任务的示例,请参阅使用托管模板创建任务

aws iot create-job \ --targets arn:aws:iot:region:123456789012:thing/thingOne \ --job-template-arn arn:aws:iot:region:123456789012:jobtemplate/template-id