J ust-in-time 佈建 - AWS IoT Core

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

J ust-in-time 佈建

您可以使用 just-in-time 佈建 (JITP) 在裝置第一次嘗試連線時佈建裝置。 AWS IoT若要佈建裝置,您必須啟用自動註冊,而且您在佈建裝置時,其裝置憑證簽署所用的憑證授權機構憑證,必須與佈建範本建立關聯。佈建成功和錯誤記錄在 Amazon 裝置佈建指標 CloudWatch 中。

JITP 概觀

當裝置嘗試使用已註冊 CA 憑證簽署的憑證進行連線時,會從 CA 憑證 AWS IoT 載入範本並使用該範本進行呼叫RegisterThing。 AWS IoT 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雖不要求顯示憑證授權機構憑證,但需要裝置在連線至 AWS 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。

如果憑證沒有ParameterstemplateBody. 例如,如果 AWS::IoT::Certificate::Country 併入在範本中,但憑證沒有 Country 屬性,裝置佈建會失敗。

您也可以使用 CloudTrail 來疑難排解 JITP 範本的問題。如需 Amazon 中記錄的指標的相關資訊 CloudWatch,請參閱裝置佈建指標。如需佈建範本的詳細資訊,請參閱佈建範本

注意

在佈建程序期間, just-in-time 佈建 (JITP) 會呼叫其他 AWS IoT 控制平面 API 作業。這些呼叫可能會超過針對您帳戶設定的 AWS IoT 調節配額,並導致調節呼叫。如有必要,請聯絡 AWS 客戶支援,以提高您的調節配額。

使用佈建範本註冊 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 資訊:

    以下範例展示如何使用 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

    如需詳細資訊,請參閱註冊 CA 憑證

  3. (選用) 使用 UpdateCACertificate API 操作或 update-ca-certificate CLI 命令來更新 CA 構憑證的設定。

    以下範例展示如何使用 AWS 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時指定要使用的範本類型。如需範本類型的詳細資訊,請參閱 AWS 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