

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

# Just-in-time 资源调配
<a name="jit-provisioning"></a>

当您的设备首次尝试连接时，您可以使用 just-in-time配置 (JITP) 对其进行配置。 AWS IoT要预调配设备，您必须启用自动注册，并将预调配模板与用于对设备证书进行签名的 CA 证书关联。配置成功和错误的记录与[设备预调配指标](metrics_dimensions.md#provisioning-metrics)在 Amazon 中相同 CloudWatch。

**Topics**
+ [JITP 概览](#jit-provisioning-overview)
+ [使用预调配模板注册 CA](#jit-provisioning-registerCA-template)
+ [使用预调配模板名称注册 CA](#jit-provisioning-registerCA-templateName)

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

当设备尝试使用 AWS IoT 由已注册的 CA 证书签名的证书进行连接时，会从 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。您可以使用 AWS IoT 规则中的`principal()`函数获取此 ID 的值。

**注意**  
您可以使用 AWS IoT Core just-in-time 配置 (JITP) 功能配置设备，而不必在设备首次连接时将整个信任链发送到 AWS IoT Core。可以选择出示 CA 证书，但当设备连接到 AWS IoT Core时，需要发送[服务器名称指示（SNI）](https://datatracker.ietf.org/doc/html/rfc3546#section-3.1)扩展。

### 示例模板正文
<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\"] }] }"
         }
      }
   }
}
```

此示例模板声明了从证书中提取并在 `Resources` 部分中使用的 `AWS::IoT::Certificate::CommonName`、`AWS::IoT::Certificate::SerialNumber`、`AWS::IoT::Certificate::Country` 和 `AWS::IoT::Certificate::Id` 预调配参数的值。JITP 工作流随后将使用此模板执行以下操作：
+ 注册一个证书并将其状态设置为 PENDING\$1ACTIVE。
+ 创建一个事物资源。
+ 创建一个策略资源。
+ 将策略附加到证书。
+ 将证书附加到 事物。
+ 将证书状态更新为 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 输出，例如 `json.dumps`（Python）或 `JSON.stringify`（节点）。`roleARN` 字段的值必须是附加有 `AWSIoTThingsRegistration` 的角色的 ARN。此外，您的模板还可以在示例中使用现有 `PolicyName`，而不是内联 `PolicyDocument`。

1. 使用注册 CACertificate API 操作或 C [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)LI 命令[注册](https://docs.aws.amazon.com/iot/latest/apireference/API_RegisterCACertificate.html) 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
   ```

   下面显示了如何使用 `SNI_ONLY` 在 AWS CLI模式中注册 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
   ```

   有关更多信息，请参阅[注册 CA 证书](https://docs.aws.amazon.com//iot/latest/developerguide/register-CA-cert.html)。

1.  （可选）使用更新 CACertificate API 操作或 C [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)LI 命令[更新](https://docs.aws.amazon.com/iot/latest/apireference/API_UpdateCACertificate.html) 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
   ```
**注意**  
对于 just-in-time配置 (JITP)，您必须在创建置备模板`JITP`时指定模板类型。有关模板类型的更多信息，请参阅 *AWS API 参考[CreateProvisioningTemplate](https://docs.aws.amazon.com/iot/latest/apireference/API_CreateProvisioningTemplate.html)*中的。

1. 要使用模板名称注册 CA，请使用[注册 CACertificate](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
   ```