J ust-in-time 资源调配 - Amazon IoT Core
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

J ust-in-time 资源调配

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

JITP 概览

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

注意

您可以使用 Amazon IoT Core just-in-time 配置 (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::CommonNameAWS::IoT::Certificate::SerialNumberAWS::IoT::Certificate::CountryAWS::IoT::Certificate::Id 预调配参数的值。JITP 工作流随后将使用此模板执行以下操作:

  • 注册一个证书并将其状态设置为 PENDING_ACTIVE。

  • 创建一个事物资源。

  • 创建一个策略资源。

  • 将策略附加到证书。

  • 将证书附加到事物。

  • 将证书状态更新为 ACTIVE。

如果证书不具备Parameters部分中提及的所有属性,则设备配置将失败templateBody。例如,如果 AWS::IoT::Certificate::Country 包含在模板中,但证书没有 Country 属性,设备预调配操作将会失败。

您还可以使用 CloudTrail 对 JITP 模板的问题进行故障排除。有关 Amazon 中记录的指标的信息 CloudWatch,请参阅设备预配置指标。有关预调配模板的更多信息,请参阅预调配模板

注意

在配置过程中, just-in-time 配置 (JITP) 会调用其他 Amazon IoT 控制平面 API 操作。这些调用可能会超过为您账户设置的 Amazon IoT 节流配额,导致调用受到限制。如有必要,请联系 Amazon 客户支持来增大节流配额。

使用预调配模板注册 CA

要使用完整的预调配模板注册 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 对象,并且只能使用前述表中的值。您可以使用各种工具来创建所需的 JSON 输出,例如 json.dumps (Python) 或 JSON.stringify(节点)。roleARN 字段的值必须是附加有 AWSIoTThingsRegistration 的角色的 ARN。此外,您的模板还可以在示例中使用现有PolicyName,而不是内联 PolicyDocument

  2. 使用 RegisterCACertificate API 操作或 register-ca-certificate CLI 命令注册 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

    下面显示了如何使用 SNI_ONLY 在 Amazon 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 证书

  3. (可选)使用 UpdateCACertificate API 操作或 update-ca-certificate CLI 命令更新 CA 证书的设置。

    下面显示了如何使用 Amazon CLI 更新 CA 证书的示例:

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

使用预调配模板名称注册 CA

要使用预调配模板名称注册 CA,请执行以下步骤:

  1. 将预调配模板正文保存为 JSON 文件。您可以在示例模板正文中找到一个示例模板正文。

  2. 要创建配置模板,请使用 CreateProvisioningTemplateAPI 或 create-provisioning-templateCLI 命令:

    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时指定模板类型。有关模板类型的更多信息,请参阅 Amazon API 参考CreateProvisioningTemplate中的。

  3. 要使用模板名称注册 CA,请使用 RegisterCACertificate API 或register-ca-certificate 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