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

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon 托管模板创建作业 Amazon CLI

使用获取 Amazon CLI 有关 Amazon 托管模板的信息,并使用这些模板创建作业。然后,您可以将任务另存为模板,并创建自己的自定义模板。

列出托管模板

list-managed-job-templates Amazon CLI 命令列出了您的所有作业模板 Amazon Web Services 账户。

aws iot list-managed-job-templates

默认情况下,运行此命令会显示所有可用的 Amazon 托管模板及其详细信息。

{ "managedJobTemplates": [ { "templateArn": "arn:aws:iot:region::jobtemplate/AWS-Reboot:1.0", "templateName": "AWS-Reboot", "description": "A managed job template for rebooting the device.", "environments": [ "LINUX" ], "templateVersion": "1.0" }, { "templateArn": "arn:aws:iot:region::jobtemplate/AWS-Remove-Application:1.0", "templateName": "AWS-Remove-Application", "description": "A managed job template for uninstalling one or more applications.", "environments": [ "LINUX" ], "templateVersion": "1.0" }, { "templateArn": "arn:aws:iot:region::jobtemplate/AWS-Stop-Application:1.0", "templateName": "AWS-Stop-Application", "description": "A managed job template for stopping one or more system services.", "environments": [ "LINUX" ], "templateVersion": "1.0" }, ... { "templateArn": "arn:aws:iot:us-east-1::jobtemplate/AWS-Restart-Application:1.0", "templateName": "AWS-Restart-Application", "description": "A managed job template for restarting one or more system services.", "environments": [ "LINUX" ], "templateVersion": "1.0" } ] }

有关更多信息,请参阅ListManagedJobTemplates

获取有关托管式模板的详细信息

describe-managed-job-template Amazon CLI 命令获取有关指定作业模板的详细信息。指定任务模板名称和可选的模板版本。如果未指定模板版本,则返回预定义的默认版本。以下示例显示运行命令以获取有关 Amazon-Download-File 模板的详细信息。

aws iot describe-managed-job-template \ --template-name Amazon-Download-File

该命令显示模板详细信息和 ARN、其任务文档以及 documentParameters 参数,此参数是模板输入参数的键/值对的列表。有关不同模板和输入参数的信息,请参阅托管模板远程操作和任务文档

注意

使用此 API 时返回的documentParameters对象只能在通过 Amazon 托管模板创建任务时使用。此对象不得用于自定义任务模板。有关演示如何使用此参数的示例,请参阅使用托管模板创建任务

{ "templateName": "Amazon-Download-File", "templateArn": "arn:aws:iot:region::jobtemplate/AWS-Download-File:1.0", "description": "A managed job template for downloading a file.", "templateVersion": "1.0", "environments": [ "LINUX" ], "documentParameters": [ { "key": "downloadUrl", "description": "URL of file to download.", "regex": "(.*?)", "example": "http://www.example.com/index.html", "optional": false }, { "key": "filePath", "description": "Path on the device where downloaded file is written.", "regex": "(.*?)", "example": "/path/to/file", "optional": false }, { "key": "runAsUser", "description": "Execute handler as another user. If not specified, then handler is executed as the same user as device client.", "regex": "(.){0,256}", "example": "user1", "optional": true }, { "key": "pathToHandler", "description": "Path to handler on the device. If not specified, then device client will use the current working directory.", "regex": "(.){0,4096}", "example": "/path/to/handler/script", "optional": true } ], "document": "{\"version\":\"1.0\",\"steps\":[{\"action\":{\"name\":\"Download-File\",\"type\":\"runHandler\",\"input\":{\"handler\":\"download-file.sh\",\"args\":[\"${aws:iot:parameter:downloadUrl}\",\"${aws:iot:parameter:filePath}\"],\"path\":\"${aws:iot:parameter:pathToHandler}\"},\"runAsUser\":\"${aws:iot:parameter:runAsUser}\"}}]}" }

有关更多信息,请参阅DescribeManagedJobTemplate

使用托管模板创建任务

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

该示例显示如何创建使用 AWS-Download-File 模板的任务。它还展示了如何使用 document-parameters 参数指定模板的输入参数。

注意

仅在 Amazon 托管模板中使用该document-parameters对象。此对象不得与自定义任务模板一起使用。

aws iot create-job \ --targets arn:aws:iot:region:account-id:thing/thingOne \ --job-id "new-managed-template-job" \ --job-template-arn arn:aws:iot:region::jobtemplate/AWS-Download-File:1.0 \ --document-parameters downloadUrl=https://example.com/index.html,filePath=path/to/file

其中:

  • 区域是 Amazon Web Services 区域.

  • account-id 唯一的 Amazon Web Services 账户 数字。

  • thingOne 是任务指向的 IoT 事物的名称。

  • Amazon-Download-File:1.0 是托管式模板的名称。

  • https://example.com/index.html 是从中下载文件的 URL。

  • https://pathto/file/index 是设备上用于存储下载文件的路径。

运行以下命令创建模板的任务:Amazon-Download-File

{ "jobArn": "arn:aws:iot:region:account-id:job/new-managed-template-job", "jobId": "new-managed-template-job", "description": "A managed job template for downloading a file." }

从托管模板创建自定义任务模板

  1. 使用上一节中介绍的托管模板创建任务。

  2. 使用您创建的任务的 ARN 创建自定义任务模板。有关更多信息,请参阅从现有任务创建任务模板