

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# 例: コマンドを使用して車両のステアリングモードを制御する (AWS CLI)
<a name="remote-command-tutorial"></a>

**重要**  
現在、特定の AWS IoT FleetWise 機能へのアクセスはゲートされています。詳細については、「[AWS AWS IoT FleetWise でのリージョンと機能の可用性](fleetwise-regions.md)」を参照してください。

次の例は、 を使用して コマンド機能を使用する方法を示しています AWS CLI。この例では、 AWS IoT FleetWise 車両をターゲットデバイスとして使用して、ステアリングモードをリモートで制御するコマンドを送信する方法を示します。

**Topics**
+ [車両ステアリングモードの例の概要](#iot-remote-command-tutorial-overview)
+ [前提条件](#iot-remote-command-tutorial-prereq)
+ [リモートコマンドを使用するための IAM ポリシー](#remote-command-policy)
+ [AWS IoT コマンドを実行する (AWS CLI)](#iot-remote-command-tutorial-run)
+ [クリーンアップ](#remote-command-tutorial-clean)

## 車両ステアリングモードの例の概要
<a name="iot-remote-command-tutorial-overview"></a>

この例では、次の操作を行います。

1. を使用して オペレーションのコマンドリソースを作成し`create-command` AWS CLI 、車両のステアリングモードを変更します。

1. を使用して、コマンドが作成された時刻や最後に更新された時刻など、コマンドに関する情報を取得します`get-command` AWS CLI。

1. `start-command-execution` AWS CLI ステアリングモードを必須パラメータとする を使用して、 コマンドを車両に送信します。このパラメータはデバイスで実行されます。

1. を使用してコマンド実行の結果を取得します`get-command-execution` AWS CLI。実行が完了した日時を確認し、実行結果やコマンドの実行完了にかかった時間などの追加の詳細を取得できます。

1. 使用しなくなったコマンドとコマンド実行を削除して、クリーンアップアクティビティを実行します。

## 前提条件
<a name="iot-remote-command-tutorial-prereq"></a>

この例を実行する前に:
+ 車両を AWS IoT レジストリの AWS IoT モノ AWS IoT FleetWise としてプロビジョニングします。また、モノに証明書を追加してアクティブ化し、モノにポリシーをアタッチする必要があります。その後、デバイスはクラウドに接続し、コマンドを実行できます。詳細については、[「車両のプロビジョニング](https://docs.aws.amazon.com/iot-fleetwise/latest/developerguide/provision-vehicles.html)」を参照してください。
+ 「」に示すように、 コマンドを使用して API オペレーションを実行するアクセス許可を付与する IAM ユーザーと IAM ポリシーを作成します[リモートコマンドを使用するための IAM ポリシー](#remote-command-policy)。

## リモートコマンドを使用するための IAM ポリシー
<a name="remote-command-policy"></a>

次の表は、 コマンド機能のすべてのコントロールプレーンおよびデータプレーン API オペレーションへのアクセスを許可する IAM ポリシーの例を示しています。アプリケーションのユーザーは、表に示すように、すべてのリモートコマンド API オペレーションを実行するアクセス許可を持ちます。


**API オペレーション:**  

| API アクション | コントロール/データプレーン | プロトコル | 説明 | [リソース]  | 
| --- | --- | --- | --- | --- | 
| CreateCommand | コントロールプレーン | HTTP | コマンドリソースを作成します。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| GetCommand | コントロールプレーン | HTTP | コマンドに関する情報を取得します。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| UpdateCommand | コントロールプレーン | HTTP | コマンドまたは に関する情報を更新して非推奨にします |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| ListCommands | コントロールプレーン | HTTP | アカウントのコマンドを一覧表示します |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| DeleteCommand | コントロールプレーン | HTTP | コマンドを削除します。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| StartCommandExecution | データプレーン | HTTP | コマンドの実行を開始します |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| UpdateCommandExecution | データプレーン | MQTT | コマンド実行を更新する |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| GetCommandExecution | コントロールプレーン | HTTP | コマンド実行に関する情報を取得します。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| ListCommandExecutions | コントロールプレーン | HTTP | アカウントのコマンド実行を一覧表示します |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 
| DeleteCommandExecution | コントロールプレーン | HTTP | コマンド実行を削除します。 |  [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/ja_jp/iot-fleetwise/latest/developerguide/remote-command-tutorial.html)  | 

この例では、次のように置き換えます。
+ `us-east-1` など AWS リージョン、 で を使用します`ap-south-1`。
+ `111122223333` を `57EXAMPLE833` などの AWS アカウント 番号に置き換えます。
+ `command-id`、`command-id1`、および `command-id2`で、 `LockDoor`や などの一意のコマンド識別子を指定します`TurnOffAC`。
+ `thing-name` などの AWS IoT モノの名前を指定します`my_car`。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Action": [
                "iot:CreateCommand",
                "iot:GetCommand",
                "iot:ListCommands",
                "iot:UpdateCommand",
                "iot:DeleteCommand"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:iot:us-east-1:111122223333:command/command-id1",
                "arn:aws:iot:us-east-1:111122223333:command/command-id2"
            ]
        },
        {
            "Action": [
                "iot:GetCommandExecution",
                "iot:ListCommandExecutions",
                "iot:DeleteCommandExecution"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:iot:us-east-1:111122223333:command/command-id",
                "arn:aws:iot:us-east-1:111122223333:thing/thing-name"
            ]
        },
        {
            "Action": "iot:StartCommandExecution",
            "Effect": "Allow",
            "Resource": [
                "arn:aws:iot:us-east-1:111122223333:command/command-id",
                "arn:aws:iot:us-east-1:111122223333:thing/thing-name"
            ]
        }
    ]
}
```

------

## AWS IoT コマンドを実行する (AWS CLI)
<a name="iot-remote-command-tutorial-run"></a>

以下は、 を使用してコマンドオペレーション AWS CLI を実行し、車両のステアリングモードを変更する方法を示しています。

1. 

**ステアリングモードオペレーションのコマンドリソースを作成する**

   `create-command` CLI を使用して、デバイスに送信するコマンドを作成します。この例では、以下を指定します。
   + *`TurnOffSteeringMode`* としての `command-id`
   + `role-arn` として `"arn:aws:iam:accountId:role/FwCommandExecutionRole"` を指定`role-arn`する必要があります。これは、車両でコマンドを作成および実行するアクセス許可を付与する IAM ロールであるためです。詳細については、「[を使用してコマンドのペイロードを生成する AWS IoT Device Management アクセス許可を付与する AWS IoT FleetWise](controlling-access.md#generate-command-payload)」を参照してください。
   + `display-name` *`Turn off steering mode`*「」として
   + `namespace` は である必要があります `AWS-IoT-FleetWise`
   + `mandatory-parameters` を「\$1actuatorPath.Vehicle.Chassis.SteeringWheel.TurnOffSteeringMode`name`」とし、defaultValue を「\$1actuatorPath.Vehicle.Chassis.SteeringWheel.TurnOffSteeringMode」とする名前と値のペア*actuatorPath.Vehicle.Chassis.SteeringWheel.TurnOffSteeringMode* `{ "S": "true" }`
**注記**  
必須パラメータを指定せずにコマンドを作成することもできます。次に、`start-command-execution`CLI を使用してコマンドを実行するときに使用するパラメータを指定する必要があります。例については、[コマンドの使用シナリオ](remote-command-use-cases.md)を参照してください。
**重要**  
`AWS-IoT-FleetWise` 名前空間を使用する場合は、 の一部として指定された `Name`フィールドが `$actuatorPath.` プレフィックス`mandatory-parameters`を使用し、 `Value`フィールドが文字列データ型を使用していることを確認する必要があります。

   ```
   aws iot create-command \ 
       --command-id TurnOffSteeringMode \ 
       --role-arn "arn:aws:iam:accountId:role/FwCommandExecutionRole" \
       --display-name "Turn off steering mode" \ 
       --namespace AWS-IoT-FleetWise \
       --mandatory-parameters '[
         {
           "name": "$actuatorPath.Vehicle.Chassis.SteeringWheel.TurnOffSteeringMode",
           "defaultValue": { "S": "true" }
         }
       ]'
   ```

   次の出力は、 CLI からのレスポンスの例を示しています。ここで、 `ap-south-1` と `123456789012`は AWS リージョン および AWS アカウント ID の例です。

   ```
   {
       "commandId": "TurnOffSteeringMode",
       "commandArn": "arn:aws:iot:ap-south-1:123456789012:command/TurnOffSteeringMode"
   }
   ```

   このコマンドを使用するその他の例については、「」を参照してください[コマンドリソースを作成する](create-manage-remote-command-cli.md#create-remote-command-cli)。

1. 

**コマンドに関する情報を取得する**

   次のコマンドを実行して、コマンドに関する情報を取得します。ここで、 `command-id`は、上記の `create-command`オペレーションの出力のコマンド ID です。
**注記**  
複数のコマンドを作成する場合は、 `ListCommands` API を使用してアカウント内のすべてのコマンドを一覧表示し、 `GetCommand` API を使用して特定のコマンドに関する追加情報を取得できます。詳細については、「[アカウントのコマンドを一覧表示する](create-manage-remote-command-cli.md#list-remote-command-cli)」を参照してください。

   ```
   aws iot get-command --command-id TurnOffSteeringMode
   ```

   このコマンドを実行すると、次のレスポンスが生成されます。コマンドが作成された時刻と最後に更新された時刻、指定したパラメータ、およびコマンドをデバイスで実行できるかどうかが表示されます。

   ```
   {
       "commandId": "TurnOffSteeringMode",
       "commandArn": "arn:aws:iot:ap-south-1:123456789012:command/TurnOffSteeringMode",
       "namespace": "AWS-IoT-FleetWise",
       "mandatoryParameters":[
           {
               "name": "$actuatorPath.Vehicle.Chassis.SteeringWheel.TurnOffSteeringMode",
               "defaultValue": {"S": "true" }
           }
       ],
       "createdAt": "2024-03-23T00:50:10.095000-07:00",
       "lastUpdatedAt": "2024-03-23T00:50:10.095000-07:00",
       "deprecated": false
   }
   ```

   このコマンドの使用に関するその他の例については、「」を参照してください[コマンドに関する情報を取得する](create-manage-remote-command-cli.md#get-remote-command-cli)。

1. 

**コマンド実行を開始する**

   次のコマンドを実行してコマンドの実行を開始します。ここで、 `command-arn`は、上記の `get-command`オペレーションの出力のコマンド ARN です。`target-arn` は、 コマンドを実行するターゲットデバイスの ARN です。たとえば、 です*`myVehicle`*。

   この例では、コマンドの作成時にパラメータにデフォルト値を指定しているため、CLI `start-command-execution` はコマンドの実行時にこれらの値を使用できます。CLI の使用時にパラメータに別の値を指定することで、デフォルト値を上書きすることもできます。

   ```
   aws iot-data start-command-execution \    
       --command-arn arn:aws:iot:ap-south-1:123456789012:command/TurnOffSteeringMode \
       --target-arn arn:aws:iot:ap-south-1:123456789012:thing/myVehicle
   ```

   このコマンドを実行すると、コマンド実行 ID が返されます。この ID を使用して、コマンド実行ステータス、詳細、およびコマンド実行履歴をクエリすることができます。

   ```
   {
       "executionId": "07e4b780-7eca-4ffd-b772-b76358da5542"
   }
   ```

   CLI の使用に関するその他の例については、「」を参照してください[コマンドを送信する (AWS CLI)](send-monitor-remote-command-cli.md#send-remote-command-cli)。

1. 

**コマンド実行に関する情報を取得する**

   次のコマンドを実行して、ターゲットデバイスで実行したコマンドに関する情報を取得します。上から`start-command-execution`操作の出力として`execution-id`取得した と、ターゲットとするデバイスの ARN `target-arn`である を指定します。
**注記**  
最新のステータス情報を取得するには、デバイスが MQTT API を使用するコマンドの `UpdateCommandExecution` MQTT 予約レスポンストピックに更新されたステータス情報を公開している必要があります。詳細については、「[コマンド実行結果を更新する](send-monitor-remote-command-cli.md#update-remote-command-execution-cli)」を参照してください。
複数のコマンド実行を開始する場合は、 `ListCommandExecutions` API を使用してアカウント内のすべてのコマンド実行を一覧表示し、 `GetCommandExecution` API を使用して特定の実行に関する追加情報を取得できます。詳細については、「[アカウントのコマンド実行を一覧表示する](send-monitor-remote-command-cli.md#list-remote-command-execution-cli)」を参照してください。

   ```
   aws iot get-command-execution \    
       --execution-id <"07e4b780-7eca-4ffd-b772-b76358da5542"> \ 
       --target-arn arn:aws:iot:us-east-1:<account>:thing/myVehicle
   ```

   このコマンドを実行すると、コマンドの実行、実行ステータス、実行を開始した時刻、完了時刻に関する情報が返されます。たとえば、次のレスポンスは、ターゲットデバイスでコマンドの実行が成功し、ステアリングモードがオフになったことを示しています。

   ```
   {
       "executionId": "07e4b780-7eca-4ffd-b772-b76358da5542",
       "commandArn": "arn:aws:iot:ap-south-1:123456789012:command/TurnOffSteeringMode",
       "targetArn": "arn:aws:iot:ap-south-1:123456789012:thing/myVehicle",
       "result": "SUCCEEDED",
        "statusReason": {
           "reasonCode": "65536",
           "reasonDescription": "SUCCESS"
       },
       "result": {
           "KeyName": {
               "S": "",
               "B": true,
               "BIN": null
           }
       },
       "createdAt": "2024-03-23T00:50:10.095000-07:00",
       "completedAt": "2024-03-23T00:50:10.095000-07:00",
       "parameters": '{
            "$actuatorPath.Vehicle.Chassis.SteeringWheel.TurnOffSteeringMode":
            { "S": "true" }
       }' 
   }
   ```

## クリーンアップ
<a name="remote-command-tutorial-clean"></a>

コマンドを作成してデバイスで実行したので、このコマンドを使用する予定がない場合は、削除できます。進行中の保留中のコマンド実行は、削除リクエストの影響を受けずに引き続き実行されます。

**注記**  
または、コマンドが古く、後でターゲットデバイスで実行する必要がある場合は、そのコマンドを非推奨にすることもできます。

1. 

**(オプション) コマンドリソースを非推奨にする**

   次のコマンドを実行してコマンドを非推奨にします。ここで、 `command-id`は上記の `get-command`オペレーションの出力のコマンド ID です。

   ```
   aws iot update-command \    
      --command-id TurnOffSteeringMode \    
      --deprecated
   ```

   このコマンドを実行すると、コマンドが廃止されたことを示す出力が返されます。CLI を使用してコマンドを復元することもできます。
**注記**  
`update-command` CLI を使用して、コマンドの表示名と説明を更新することもできます。詳細については、「[コマンドリソースを更新または廃止する](create-manage-remote-command-cli.md#update-remote-command-cli)」を参照してください。

   ```
   {
       "commandId": "TurnOffSteeringMode",
       "deprecated": true,
       "lastUpdatedAt": "2024-05-09T23:16:51.370000-07:00"
   }
   ```

1. 

**コマンドを削除する**

   次のコマンドを実行して、 で指定された コマンドを削除します`command-id`。
**注記**  
削除アクションは永続的であり、元に戻すことはできません。

   ```
   aws iot delete-command --command-id TurnOffSteeringMode
   ```

   削除リクエストが成功すると、コマンドを非推奨にしたか、いつ非推奨にしたかに応じて、HTTP `statusCode`が 202 または 204 になります。詳細と例については、「[コマンドリソースを削除する](create-manage-remote-command-cli.md#delete-remote-command-cli)」を参照してください。

   `get-command` CLI を使用して、コマンドがアカウントから削除されたことを確認できます。

1. 

**(オプション) コマンド実行を削除する**

   デフォルトでは、すべてのコマンド実行は、作成日から 6 か月以内に削除されます。この情報は、 `GetCommandExecution` API の `timeToLive`パラメータを使用して表示できます。

   または、実行ステータスが 、、または のいずれかである場合など`SUCCEEDED``FAILED`、コマンド実行がターミナルになった場合は`REJECTED`、コマンド実行を削除できます。次のコマンドを実行して実行を削除します。ここで、 `execution-id`は上記の `get-command-execution`オペレーションの出力にある実行 ID です。

   ```
   aws iot delete-command-execution \ 
               --execution-id "07e4b780-7eca-4ffd-b772-b76358da5542"
   ```

   `get-command-execution` CLI を使用して、コマンド実行がアカウントから削除されたことを確認できます。