步骤 2:在 Amazon IoT中创建并运行任务 - Amazon IoT Core
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

步骤 2:在 Amazon IoT中创建并运行任务

本节中的过程创建一个任务文档和 Amazon IoT任务资源。创建任务资源后,Amazon IoT 将任务文档发送到指定的任务目标,任务代理将任务文档应用于设备或客户端。

创建并存储任务的任务文档

此过程将创建一个简单的任务文档包括在 Amazon IoT任务资源内。这份任务文档显示“Hello world!” 在任务目标上。

要创建和存储任务文档:

  1. 选择要将任务文档保存到的 Amazon S3 存储桶。如果您没有现有 Amazon S3 存储桶可用,需要创建一个。有关如何创建 Amazon S3 存储桶的信息,请参阅 Amazon S3 入门中的主题。

  2. 为此任务创建并保存任务文档

    1. 在本地主机上,打开文本编辑器。

    2. 复制此文本并粘贴到编辑器中。

      { "operation": "echo", "args": ["Hello world!"] }
    3. 在本地主机上,将编辑器内容保存到名为 hello-world-job.json的文件中。

    4. 确认文件已正确保存。一些文本编辑器会自动追加 .txt用户保存文本文件时的文件名。如果您的编辑器附加 .txt在文件名中,在继续之前更正文件名。

  3. path_to_file 替换为到 hello-world-job.json的路径,如果不在当前目录中,将 s3_bucket_name 替换为所选存储桶的 Amazon S3 存储桶路径,然后运行此命令将您的任务文档放入 Amazon S3 存储桶中。

    aws s3api put-object \ --key hello-world-job.json \ --body path_to_file/hello-world-job.json --bucket s3_bucket_name

    通过替换以下 URL 中的 s3_bucket_name and AWS_region,可以确定标识存储在 Amazon S3 中的任务文档的任务文档 URL。记录生成的 URL 稍后用作job_ocument_path

    https://s3_bucket_name.s3.AWS_Region.amazonaws.com/hello-world-job.json
    注意

    Amazon安全防止您无法在 Amazon Web Services 账户外面打开此 URL,例如通过使用浏览器。预设情况下,具有文件访问权限的 Amazon IoT任务引擎使用 URL。在生产环境中,您需要确保 Amazon IoT服务有权访问存储在 Amazon S3 中的任务文档。

保存任务文档的 URL 后,继续 为一台 IoT 设备在 Amazon IoT中运行任务

为一台 IoT 设备在 Amazon IoT中运行任务

本节中的过程启动 Raspberry Pi 上的Amazon IoT Device Client,在设备上运行任务代理,等待任务运行。还在 Amazon IoT中创建了一个任务资源,将将任务发送到 IoT 设备并在物联网设备上运行。

注意

此过程仅在一台设备上运行任务。

要在 Raspberry Pi 上启动任务代理:

  1. 在连接到 Raspberry Pi 的本地主机上的终端窗口中,运行此命令启动 Amazon IoTDevice Client。

    cd ~/aws-iot-device-client/build ./aws-iot-device-client --config-file ~/dc-configs/dc-jobs-config.json
  2. 在终端窗口中,确认 Amazon IoTDevice Client 并显示这些消息

    2021-11-15T18:45:56.708Z [INFO] {Main.cpp}: Jobs is enabled . . . 2021-11-15T18:45:56.708Z [INFO] {Main.cpp}: Client base has been notified that Jobs has started 2021-11-15T18:45:56.708Z [INFO] {JobsFeature.cpp}: Running Jobs! 2021-11-15T18:45:56.708Z [DEBUG] {JobsFeature.cpp}: Attempting to subscribe to startNextPendingJobExecution accepted and rejected 2021-11-15T18:45:56.708Z [DEBUG] {JobsFeature.cpp}: Attempting to subscribe to nextJobChanged events 2021-11-15T18:45:56.708Z [DEBUG] {JobsFeature.cpp}: Attempting to subscribe to updateJobExecutionStatusAccepted for jobId + 2021-11-15T18:45:56.738Z [DEBUG] {JobsFeature.cpp}: Ack received for SubscribeToUpdateJobExecutionAccepted with code {0} 2021-11-15T18:45:56.739Z [DEBUG] {JobsFeature.cpp}: Attempting to subscribe to updateJobExecutionStatusRejected for jobId + 2021-11-15T18:45:56.753Z [DEBUG] {JobsFeature.cpp}: Ack received for SubscribeToNextJobChanged with code {0} 2021-11-15T18:45:56.760Z [DEBUG] {JobsFeature.cpp}: Ack received for SubscribeToStartNextJobRejected with code {0} 2021-11-15T18:45:56.776Z [DEBUG] {JobsFeature.cpp}: Ack received for SubscribeToStartNextJobAccepted with code {0} 2021-11-15T18:45:56.776Z [DEBUG] {JobsFeature.cpp}: Ack received for SubscribeToUpdateJobExecutionRejected with code {0} 2021-11-15T18:45:56.777Z [DEBUG] {JobsFeature.cpp}: Publishing startNextPendingJobExecutionRequest 2021-11-15T18:45:56.785Z [DEBUG] {JobsFeature.cpp}: Ack received for StartNextPendingJobPub with code {0} 2021-11-15T18:45:56.785Z [INFO] {JobsFeature.cpp}: No pending jobs are scheduled, waiting for the next incoming job
  3. 在终端窗口中,看到此消息后,继续下一步并创建任务资源。请注意,它可能不是列表中的最后一个条目。

    2021-11-15T18:45:56.785Z [INFO] {JobsFeature.cpp}: No pending jobs are scheduled, waiting for the next incoming job

要创建 Amazon IoT任务资源

  1. 在本地主机上:

    1. job_ocument_url 替换为 创建并存储任务的任务文档的任务文档 URL。

    2. thing_arn 替换为您为设备创建的事物资源的 ARN,然后运行此命令。

      aws iot create-job \ --job-id hello-world-job-1 \ --document-source "job_document_url" \ --targets "thing_arn" \ --target-selection SNAPSHOT

      如果成功,该命令将返回类似此结果。

      { "jobArn": "arn:aws:iot:us-west-2:57EXAMPLE833:job/hello-world-job-1", "jobId": "hello-world-job-1" }
  2. 在终端窗口中,您应看到 Amazon IoTDevice Client 的输出 。

    2021-11-15T18:02:26.688Z [INFO] {JobsFeature.cpp}: No pending jobs are scheduled, waiting for the next incoming job 2021-11-15T18:10:24.890Z [DEBUG] {JobsFeature.cpp}: Job ids differ 2021-11-15T18:10:24.890Z [INFO] {JobsFeature.cpp}: Executing job: hello-world-job-1 2021-11-15T18:10:24.890Z [DEBUG] {JobsFeature.cpp}: Attempting to update job execution status! 2021-11-15T18:10:24.890Z [DEBUG] {JobsFeature.cpp}: Not including stdout with the status details 2021-11-15T18:10:24.890Z [DEBUG] {JobsFeature.cpp}: Not including stderr with the status details 2021-11-15T18:10:24.890Z [DEBUG] {JobsFeature.cpp}: Assuming executable is in PATH 2021-11-15T18:10:24.890Z [INFO] {JobsFeature.cpp}: About to execute: echo Hello world! 2021-11-15T18:10:24.890Z [DEBUG] {Retry.cpp}: Retryable function starting, it will retry until success 2021-11-15T18:10:24.890Z [DEBUG] {JobsFeature.cpp}: Created EphermalPromise for ClientToken 3TEWba9Xj6 in the updateJobExecution promises map 2021-11-15T18:10:24.890Z [DEBUG] {JobEngine.cpp}: Child process now running 2021-11-15T18:10:24.890Z [DEBUG] {JobEngine.cpp}: Child process about to call execvp 2021-11-15T18:10:24.890Z [DEBUG] {JobEngine.cpp}: Parent process now running, child PID is 16737 2021-11-15T18:10:24.891Z [DEBUG] {16737}: Hello world! 2021-11-15T18:10:24.891Z [DEBUG] {JobEngine.cpp}: JobEngine finished waiting for child process, returning 0 2021-11-15T18:10:24.891Z [INFO] {JobsFeature.cpp}: Job exited with status: 0 2021-11-15T18:10:24.891Z [INFO] {JobsFeature.cpp}: Job executed successfully! 2021-11-15T18:10:24.891Z [DEBUG] {JobsFeature.cpp}: Attempting to update job execution status! 2021-11-15T18:10:24.891Z [DEBUG] {JobsFeature.cpp}: Not including stdout with the status details 2021-11-15T18:10:24.891Z [DEBUG] {JobsFeature.cpp}: Not including stderr with the status details 2021-11-15T18:10:24.892Z [DEBUG] {Retry.cpp}: Retryable function starting, it will retry until success 2021-11-15T18:10:24.892Z [DEBUG] {JobsFeature.cpp}: Created EphermalPromise for ClientToken GmQ0HTzWGg in the updateJobExecution promises map 2021-11-15T18:10:24.905Z [DEBUG] {JobsFeature.cpp}: Ack received for PublishUpdateJobExecutionStatus with code {0} 2021-11-15T18:10:24.905Z [DEBUG] {JobsFeature.cpp}: Removing ClientToken 3TEWba9Xj6 from the updateJobExecution promises map 2021-11-15T18:10:24.905Z [DEBUG] {JobsFeature.cpp}: Success response after UpdateJobExecution for job hello-world-job-1 2021-11-15T18:10:24.917Z [DEBUG] {JobsFeature.cpp}: Ack received for PublishUpdateJobExecutionStatus with code {0} 2021-11-15T18:10:24.918Z [DEBUG] {JobsFeature.cpp}: Removing ClientToken GmQ0HTzWGg from the updateJobExecution promises map 2021-11-15T18:10:24.918Z [DEBUG] {JobsFeature.cpp}: Success response after UpdateJobExecution for job hello-world-job-1 2021-11-15T18:10:25.861Z [INFO] {JobsFeature.cpp}: No pending jobs are scheduled, waiting for the next incoming job
  3. 尽管 Amazon IoTDevice Client 正在运行并等待任务,您可以通过更改 job-id值并从步骤 1 开始重新运行 create-job

完成运行任务后,在终端窗口中输入 ^C(control-C)来停止 Amazon IoTDevice Client。