

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# AWS IoT Device Client를 AWS IoT 사용하여에서 작업 생성 및 실행
<a name="iot-dc-runjobs-prepare-define"></a>

이 섹션의 절차에서는 작업 문서와 AWS IoT 작업 리소스를 생성합니다. 작업 리소스를 생성한 후는 작업 에이전트가 작업 문서를 디바이스 또는 클라이언트에 적용하는 지정된 작업 대상으로 작업 문서를 AWS IoT 보냅니다.

**Topics**
+ [IoT 작업의 작업 문서 생성 및 저장](#iot-dc-runjobs-prepare-define-jobdoc)
+ [하나의 IoT 디바이스에 AWS IoT 대해에서 작업 실행](#iot-dc-runjobs-prepare-define-job)

## IoT 작업의 작업 문서 생성 및 저장
<a name="iot-dc-runjobs-prepare-define-jobdoc"></a>

이 절차에서는 작업 리소스에 포함할 간단한 AWS IoT 작업 문서를 생성합니다. 이 작업 문서는 작업 대상에서 “Hello world\$1”를 표시합니다.

**작업 문서를 생성하고 저장하려면**

1. 작업 문서를 저장할 Amazon S3 버킷을 선택합니다. 이를 위해 사용할 기존 Amazon S3 버킷이 없는 경우에는 버킷을 생성해야 합니다. Amazon S3 버킷을 생성하는 방법에 대한 자세한 내용은 [Amazon S3 시작하기](https://docs.aws.amazon.com//AmazonS3/latest/userguide/GetStartedWithS3.html)의 주제를 참조하세요.

1. 이 작업에 대한 작업 문서 생성 및 저장

   1. 로컬 호스트 컴퓨터에서 텍스트 편집기를 엽니다.

   1. 이 텍스트를 복사하여 편집기에 붙여 넣습니다.

      ```
      {
          "operation": "echo",
          "args": ["Hello world!"]
      }
      ```

   1. 로컬 호스트 컴퓨터에서 편집기의 내용을 **hello-world-job.json**이라는 파일에 저장합니다.

   1. 파일이 올바르게 저장되었는지 확인합니다. 일부 텍스트 편집기는 텍스트 파일을 저장할 때 파일 이름에 자동으로 `.txt`를 추가합니다. 편집기에서 파일 이름에 `.txt`를 추가한 경우 계속하기 전에 파일 이름을 수정합니다.

1. *path\$1to\$1file*을 **hello-world-job.json**의 경로로 바꾸고(현재 디렉터리에 없는 경우) *s3\$1bucket\$1name*을 선택한 버킷에 대한 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
   ```

   Amazon S3에 저장한 작업 문서를 식별하는 작업 문서 URL은 다음 URL에서 *s3\$1bucket\$1name* 및 *AWS\$1region*을 바꿔서 결정됩니다. 나중에 *job\$1document\$1path*로 사용할 결과 URL을 기록합니다.

   ```
   https://s3_bucket_name.s3.AWS_Region.amazonaws.com/hello-world-job.json
   ```
**참고**  
AWS 보안은 AWS 계정예를 들어 브라우저를 사용하여 외부에서이 URL을 열 수 없도록 합니다. URL은 기본적으로 파일에 액세스할 수 있는 AWS IoT 작업 엔진에서 사용됩니다. 프로덕션 환경에서는 AWS IoT 서비스에 Amazon S3에 저장된 작업 문서에 대한 액세스 권한이 있는지 확인해야 합니다.

작업 문서의 URL을 저장한 후 [하나의 IoT 디바이스에 AWS IoT 대해에서 작업 실행](#iot-dc-runjobs-prepare-define-job)으로 진행합니다.

## 하나의 IoT 디바이스에 AWS IoT 대해에서 작업 실행
<a name="iot-dc-runjobs-prepare-define-job"></a>

이 섹션의 절차는 Raspberry Pi에서 AWS IoT Device Client를 시작하여 디바이스에서 작업 에이전트를 실행하고 작업이 실행될 때까지 기다립니다. 또한에서 작업 리소스를 생성 AWS IoT하여 IoT 디바이스로 작업을 전송하고 실행합니다.

**참고**  
이 절차는 단일 디바이스에서만 작업을 실행합니다.

**Raspberry Pi에서 작업 에이전트를 시작하려면**

1. Raspberry Pi에 연결된 로컬 호스트 컴퓨터의 터미널 창에서이 명령을 실행하여 AWS IoT Device Client를 시작합니다.

   ```
   cd ~/aws-iot-device-client/build
   ./aws-iot-device-client --config-file ~/dc-configs/dc-jobs-config.json
   ```

1. 터미널 창에서 AWS IoT Device 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
   ```

1. 터미널 창에서 이 메시지가 표시되면 다음 절차로 진행하고 작업 리소스를 생성합니다. 목록의 마지막 항목이 아닐 수도 있습니다.

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

**AWS IoT 작업 리소스를 생성하려면**

1. 로컬 호스트 컴퓨터에서

   1. *job\$1document\$1url*을 [IoT 작업의 작업 문서 생성 및 저장](#iot-dc-runjobs-prepare-define-jobdoc)의 작업 문서 URL로 바꿉니다.

   1. *thing\$1arn*을 디바이스에 대해 생성한 사물 리소스의 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"
      }
      ```

1. 터미널 창에 다음과 같이 AWS IoT Device 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
   ```

1.  AWS IoT Device Client가 실행 중이고 작업을 기다리는 동안 `job-id` 값을 변경하고 1단계**create-job**에서를 다시 실행하여 다른 작업을 제출할 수 있습니다.

작업 실행이 완료되면 터미널 창에서 ^C (control-C)를 입력하여 AWS IoT Device Client를 중지합니다.