

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

# ジャストインタイムプロビジョニング
<a name="jit-provisioning"></a>

ジャストインタイムプロビジョニング (JITP) を使用して、デバイスが最初に AWS IoTに接続しようとするときにデバイスをプロビジョニングできます。デバイスをプロビジョニングするには、自動登録を有効にして、プロビジョニングテンプレートを、デバイス証明書に署名するために使用される CA 証明書に関連付ける必要があります。プロビジョニングの成功とエラーは、Amazon CloudWatch の [デバイスプロビジョニングのメトリクス](metrics_dimensions.md#provisioning-metrics) と同様にログに記録されます。

**Topics**
+ [JITP の概要](#jit-provisioning-overview)
+ [プロビジョニングテンプレートを使用して CA を登録する](#jit-provisioning-registerCA-template)
+ [プロビジョニングテンプレート名を使用して CA を登録する](#jit-provisioning-registerCA-templateName)

## JITP の概要
<a name="jit-provisioning-overview"></a>

登録された CA 証明書によって署名された証明書 AWS IoT を使用してデバイスが に接続しようとすると、 は CA 証明書からテンプレートを AWS IoT ロードし、それを使用して [RegisterThing](fleet-provision-api.md#register-thing) を呼び出します。JITP ワークフローは、最初に `PENDING_ACTIVATION` というステータス値で証明書を登録します。デバイスのプロビジョニングフローが完了すると、証明書のステータスは `ACTIVE` に変わります。

AWS IoT は、プロビジョニングテンプレートで宣言および参照できる以下のパラメータを定義します。
+ `AWS::IoT::Certificate::Country`
+ `AWS::IoT::Certificate::Organization`
+ `AWS::IoT::Certificate::OrganizationalUnit`
+ `AWS::IoT::Certificate::DistinguishedNameQualifier`
+ `AWS::IoT::Certificate::StateName`
+ `AWS::IoT::Certificate::CommonName`
+ `AWS::IoT::Certificate::SerialNumber`
+ `AWS::IoT::Certificate::Id`

これらのプロビジョニングテンプレートパラメータの値は、プロビジョニング対象のデバイスの証明書の件名フィールドから JITP が抽出できるものに限られます。証明書には、テンプレート本体のすべてのパラメータの値が含まれている必要があります。`AWS::IoT::Certificate::Id` パラメータは、証明書に含まれている ID ではなく、内部で生成された ID を参照します。この ID の値は、 AWS IoT ルール内の `principal()`関数を使用して取得できます。

**注記**  
デバイスの最初の接続で信頼チェーン全体を送信しなくても、ジャ AWS IoT Core just-in-timeプロビジョニング (JITP) 機能を使用してデバイスをプロビジョニングできます AWS IoT Core。CA 証明書の提示はオプションですが、デバイスが AWS IoT Coreに接続するときに [[Server Name Indication (SNI)]](https://datatracker.ietf.org/doc/html/rfc3546#section-3.1) (サーバーネームインディケーション (SNI)) エクステンションを送信する必要があります。

### テンプレート本文の例
<a name="jit-provisioning-example-templatebody"></a>

次の JSON ファイルは、完全な JITP テンプレートのテンプレート本文の例です。

```
{
   "Parameters":{
      "AWS::IoT::Certificate::CommonName":{
         "Type":"String"
      },
      "AWS::IoT::Certificate::SerialNumber":{
         "Type":"String"
      },
      "AWS::IoT::Certificate::Country":{
         "Type":"String"
      },
      "AWS::IoT::Certificate::Id":{
         "Type":"String"
      }
   },
   "Resources":{
      "thing":{
         "Type":"AWS::IoT::Thing",
         "Properties":{
            "ThingName":{
               "Ref":"AWS::IoT::Certificate::CommonName"
            },
            "AttributePayload":{
               "version":"v1",
               "serialNumber":{
                  "Ref":"AWS::IoT::Certificate::SerialNumber"
               }
            },
            "ThingTypeName":"lightBulb-versionA",
            "ThingGroups":[
               "v1-lightbulbs",
               {
                  "Ref":"AWS::IoT::Certificate::Country"
               }
            ]
         },
         "OverrideSettings":{
            "AttributePayload":"MERGE",
            "ThingTypeName":"REPLACE",
            "ThingGroups":"DO_NOTHING"
         }
      },
      "certificate":{
         "Type":"AWS::IoT::Certificate",
         "Properties":{
            "CertificateId":{
               "Ref":"AWS::IoT::Certificate::Id"
            },
            "Status":"ACTIVE"
         }
      },
      "policy":{
         "Type":"AWS::IoT::Policy",
         "Properties":{
            "PolicyDocument":"{ \"Version\": \"2012-10-17\",		 	 	  \"Statement\": [{ \"Effect\": \"Allow\", \"Action\":[\"iot:Publish\"], \"Resource\": [\"arn:aws:iot:us-east-1:123456789012:topic/foo/bar\"] }] }"
         }
      }
   }
}
```

このサンプルテンプレートでは、証明書から抽出され、`AWS::IoT::Certificate::CommonName` セクションで使用されている `AWS::IoT::Certificate::SerialNumber`、`AWS::IoT::Certificate::Country`、`AWS::IoT::Certificate::Id` および `Resources` プロビジョニングパラメータの値を宣言します。次に、JITP ワークフローはこのテンプレートを使用して次のアクションを実行します。
+ 証明書を登録し、そのステータスを PENDING\$1ACTIVE に設定します。
+ 1 つのモノのリソースを作成します。
+ 1 つのポリシーのリソースを作成します。
+ ポリシーを証明書にアタッチします。
+ 証明書をモノにアタッチします。
+ 証明書のステータスを ACTIVE に更新します。

`templateBody` の `Parameters` セクションに記載されているすべてのプロパティが証明書に含まれていない場合、デバイスのプロビジョニングは失敗します。例えば、`AWS::IoT::Certificate::Country` がテンプレートに含まれていても、証明書に `Country` プロパティがない場合、デバイスのプロビジョニングは失敗します。

CloudTrail を使用して、JITP テンプレートに関する問題をトラブルシューティングすることもできます。Amazon CloudWatch にログ記録されるメトリクスについては、「[デバイスプロビジョニングのメトリクス](metrics_dimensions.md#provisioning-metrics)」を参照してください。プロビジョニングテンプレートの詳細については、「[プロビジョニングテンプレート](provision-template.md)」を参照してください。

**注記**  
プロビジョニングプロセス中に、just-in-timeプロビジョニング (JITP) は他の AWS IoT コントロールプレーン API オペレーションを呼び出します。これらの呼び出しは、アカウントに設定された[AWS IoT スロットリングクォータ](https://docs.aws.amazon.com/general/latest/gr/iot-core.html#throttling-limits)を超過し、スロットリングされた呼び出しが発生する可能性があります。必要に応じて、[AWS カスタマーサポート](https://console.aws.amazon.com/support/home)に連絡して、スロットリングのクォータを引き上げてください。

## プロビジョニングテンプレートを使用して CA を登録する
<a name="jit-provisioning-registerCA-template"></a>

完全なプロビジョニングテンプレートを使用して CA を登録するには、次の手順に従います。

1. 次の例に示すようなプロビジョニングテンプレートとロール ARN 情報を JSON ファイルとして保存します。

   ```
   { 
        "templateBody" : "{\r\n    \"Parameters\" : {\r\n        \"AWS::IoT::Certificate::CommonName\": {\r\n            \"Type\": \"String\"\r\n        },\r\n        \"AWS::IoT::Certificate::SerialNumber\": {\r\n            \"Type\": \"String\"\r\n        },\r\n        \"AWS::IoT::Certificate::Country\": {\r\n            \"Type\": \"String\"\r\n        },\r\n        \"AWS::IoT::Certificate::Id\": {\r\n            \"Type\": \"String\"\r\n        }\r\n    },\r\n    \"Resources\": {\r\n        \"thing\": {\r\n            \"Type\": \"AWS::IoT::Thing\",\r\n            \"Properties\": {\r\n                \"ThingName\": {\r\n                    \"Ref\": \"AWS::IoT::Certificate::CommonName\"\r\n                },\r\n                \"AttributePayload\": {\r\n                    \"version\": \"v1\",\r\n                    \"serialNumber\": {\r\n                        \"Ref\": \"AWS::IoT::Certificate::SerialNumber\"\r\n                    }\r\n                },\r\n                \"ThingTypeName\": \"lightBulb-versionA\",\r\n                \"ThingGroups\": [\r\n                    \"v1-lightbulbs\",\r\n                    {\r\n                        \"Ref\": \"AWS::IoT::Certificate::Country\"\r\n                    }\r\n                ]\r\n            },\r\n            \"OverrideSettings\": {\r\n                \"AttributePayload\": \"MERGE\",\r\n                \"ThingTypeName\": \"REPLACE\",\r\n                \"ThingGroups\": \"DO_NOTHING\"\r\n            }\r\n        },\r\n        \"certificate\": {\r\n            \"Type\": \"AWS::IoT::Certificate\",\r\n            \"Properties\": {\r\n                \"CertificateId\": {\r\n                    \"Ref\": \"AWS::IoT::Certificate::Id\"\r\n                },\r\n                \"Status\": \"ACTIVE\"\r\n            },\r\n            \"OverrideSettings\": {\r\n                \"Status\": \"DO_NOTHING\"\r\n            }\r\n        },\r\n        \"policy\": {\r\n            \"Type\": \"AWS::IoT::Policy\",\r\n            \"Properties\": {\r\n                \"PolicyDocument\": \"{ \\\"Version\\\": \\\"2012-10-17		 	 	 \\\", \\\"Statement\\\": [{ \\\"Effect\\\": \\\"Allow\\\", \\\"Action\\\":[\\\"iot:Publish\\\"], \\\"Resource\\\": [\\\"arn:aws:iot:us-east-1:123456789012:topic\/foo\/bar\\\"] }] }\"\r\n            }\r\n        }\r\n    }\r\n}",
        "roleArn" : "arn:aws:iam::123456789012:role/JITPRole"
   }
   ```

   この例で、`templateBody` フィールドの値は、エスケープ文字列として指定された JSON オブジェクトである必要があり、[前のリスト](#jit-provisioning-overview)に示した値のみを使用できます。`json.dumps` (Python) や `JSON.stringify` （ノード）など、必要な JSON 出力を作成するには、さまざまなツールを使用できます。`roleARN` フィールドの値は、`AWSIoTThingsRegistration` がアタッチされたロールの ARN である必要があります。また、テンプレートでは例のインライン `PolicyName` の代わりに、既存の `PolicyDocument` を使用できます 

1. [RegisterCACertificate](https://docs.aws.amazon.com/iot/latest/apireference/API_RegisterCACertificate.html) API オペレーションまたは [https://docs.aws.amazon.com//cli/latest/reference/iot/register-ca-certificate.html](https://docs.aws.amazon.com//cli/latest/reference/iot/register-ca-certificate.html) CLI コマンドを使用して CA 証明書を登録します。前のステップで保存したプロビジョニングテンプレートとロール ARN 情報のディレクトリを指定します。

   以下に、 AWS CLIを使用して `DEFAULT` モードで CA 証明書を登録する方法の例を示します。

   ```
   aws iot register-ca-certificate --ca-certificate file://your-ca-cert --verification-cert file://your-verification-cert 
                   --set-as-active --allow-auto-registration --registration-config file://your-template
   ```

   以下に、 AWS CLIを使用して `SNI_ONLY` モードで CA 証明書を登録する方法の例を示します。

   ```
   aws iot register-ca-certificate --ca-certificate file://your-ca-cert --certificate-mode SNI_ONLY
                    --set-as-active --allow-auto-registration --registration-config file://your-template
   ```

   詳細については､「[Register your CA Certificates](https://docs.aws.amazon.com//iot/latest/developerguide/register-CA-cert.html)」(CA 証明書の登録) を参照してください。

1.  (オプション) [UpdateCACertificate](https://docs.aws.amazon.com/iot/latest/apireference/API_UpdateCACertificate.html) API オペレーションまたは [https://docs.aws.amazon.com//cli/latest/reference/iot/update-ca-certificate.html](https://docs.aws.amazon.com//cli/latest/reference/iot/update-ca-certificate.html) CLI コマンドを使用して、CA 証明書の設定を更新します。

    AWS CLIを使用して CA 証明書を更新する方法の例を次に示します。

   ```
   aws iot update-ca-certificate --certificate-id caCertificateId
                   --new-auto-registration-status ENABLE --registration-config file://your-template
   ```

## プロビジョニングテンプレート名を使用して CA を登録する
<a name="jit-provisioning-registerCA-templateName"></a>

プロビジョニングテンプレート名を使用して CA を登録するには、次の手順に従います。

1. プロビジョニングテンプレート本文を JSON ファイルとして保存します。テンプレート本文の例は「[テンプレート本文の例](#jit-provisioning-example-templatebody)」にあります。

1. プロビジョニングテンプレートを作成するには、[CreateProvisioningTemplate](https://docs.aws.amazon.com/iot/latest/apireference/API_CreateProvisioningTemplate.html) API または [https://docs.aws.amazon.com//cli/latest/reference/iot/create-provisioning-template.html](https://docs.aws.amazon.com//cli/latest/reference/iot/create-provisioning-template.html)CLI コマンドを使用します。

   ```
   aws iot create-provisioning-template --template-name your-template-name \
           --template-body file://your-template-body.json --type JITP \
           --provisioning-role-arn arn:aws:iam::123456789012:role/test
   ```
**注記**  
ジャストインタイムプロビジョニング (JITP) の場合は、プロビジョニングテンプレートを作成するときにテンプレートタイプとして `JITP` を指定する必要があります｡ テンプレートタイプの詳細については、「*AWS API リファレンス*」の「[CreateProvisioningTemplate](https://docs.aws.amazon.com/iot/latest/apireference/API_CreateProvisioningTemplate.html)」を参照してください。

1. CA をテンプレート名で登録するには、[RegisterCACertificate](https://docs.aws.amazon.com/iot/latest/apireference/API_RegisterCACertificate.html) API または [https://docs.aws.amazon.com//cli/latest/reference/iot/register-ca-certificate.html](https://docs.aws.amazon.com//cli/latest/reference/iot/register-ca-certificate.html) CLI コマンドを使用します。

   ```
   aws iot register-ca-certificate --ca-certificate file://your-ca-cert --verification-cert file://your-verification-cert \
           --set-as-active --allow-auto-registration --registration-config templateName=your-template-name
   ```