即时预调配
您可以在设备首次尝试连接到 Amazon IoT 时使用即时预调配(JITP)对其进行预调配。要预调配设备,您必须启用自动注册,并将预调配模板与用于对设备证书进行签名的 CA 证书关联。预调配成功和错误记录均作为 设备预调配指标 录入 Amazon CloudWatch 中。
JITP 概览
当设备尝试使用由已注册 CA 证书签名的证书连接到 Amazon IoT 时,Amazon IoT 会从 CA 证书加载模板,并使用它来调用 RegisterThing。JITP 工作流首先将注册一个状态值为 PENDING_ACTIVATION 的证书。当设备预调配流程完成时,该证书的状态将更改为 ACTIVE。
Amazon 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。您可以使用 principal() 规则内的 Amazon IoT 函数获取此 ID 的值。
注意
您可以使用 Amazon IoT Core 即时预调配(JITP)特征,而无需将设备首次连接到 Amazon IoT Core 时发送整个信任链。可以选择出示 CA 证书,但当设备连接到 Amazon IoT Core 时,需要发送服务器名称指示(SNI)
示例模板正文
以下 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_ACTIVE。
-
创建一个事物资源。
-
创建一个策略资源。
-
将策略附加到证书。
-
将证书附加到事物。
-
将证书状态更新为 ACTIVE。
如果证书不具有 templateBody 的 Parameters 部分中提及的所有属性,则设备将会预调配失败。例如,如果 AWS::IoT::Certificate::Country 包含在模板中,但证书没有 Country 属性,设备预调配操作将会失败。
您还可以使用 CloudTrail 排除 JITP 模板的问题。有关 Amazon CloudWatch 中录入的指标的信息,请参阅 设备预调配指标。有关预调配模板的更多信息,请参阅预调配模板。
注意
在预调配过程中,即时预调配(JITP)调用其它 Amazon IoT 控制面板 API 操作。这些调用可能会超过为您账户设置的 Amazon IoT 节流配额,导致调用受到限制。如有必要,请联系 Amazon 客户支持
使用预调配模板注册 CA
要使用完整的预调配模板注册 CA,请执行以下步骤:
-
将预调配模板和角色 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 对象,并且只能使用前述表中的值。您可以使用各种工具来创建所需的 JSON 输出,例如json.dumps(Python)或JSON.stringify(节点)。roleARN字段的值必须是附加有AWSIoTThingsRegistration的角色的 ARN。此外,您的模板还可以在示例中使用现有PolicyName,而不是内联PolicyDocument。 -
使用 RegisterCACertificate API 操作或
register-ca-certificateCLI 命令注册 CA 证书。您将指定预调配模板的目录以及您在上一步中保存的角色 ARN 信息:下面显示了如何使用 Amazon 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下面显示了如何使用 Amazon CLI 在
SNI_ONLY模式中注册 CA 证书的示例:aws iot register-ca-certificate --ca-certificate file://your-ca-cert--certificate-modeSNI_ONLY--set-as-active --allow-auto-registration --registration-config file://your-template有关更多信息,请参阅注册 CA 证书。
-
(可选)使用 UpdateCACertificate API 操作或
update-ca-certificateCLI 命令更新 CA 证书的设置。下面显示了如何使用 Amazon CLI 更新 CA 证书的示例:
aws iot update-ca-certificate --certificate-idcaCertificateId--new-auto-registration-status ENABLE --registration-config file://your-template
使用预调配模板名称注册 CA
要使用预调配模板名称注册 CA,请执行以下步骤:
-
将预调配模板正文保存为 JSON 文件。您可以在示例模板正文中找到一个示例模板正文。
-
要创建预调配模板,请使用 CreateProvisioningTemplate API 或
create-provisioning-templateCLI 命令:aws iot create-provisioning-template --template-nameyour-template-name\ --template-body file://your-template-body.json--type JITP \ --provisioning-role-arnarn:aws:iam::123456789012:role/test注意
对于即时预调配(JITP),您必须在创建预调配模板时将模板类型指定为
JITP。有关模板类型的更多信息,请参阅《Amazon API 参考》中的 CreateProvisioningTemplate。 -
要使用模板名称注册 CA,请使用 RegisterCACertificate API 或
register-ca-certificateCLI 命令:aws iot register-ca-certificate --ca-certificate file://your-ca-cert--verification-certfile://your-verification-cert\ --set-as-active --allow-auto-registration --registration-config templateName=your-template-name