步驟 3:將元件發佈至 AWS IoT Greengrass 服務 - AWS IoT Greengrass

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

步驟 3:將元件發佈至 AWS IoT Greengrass 服務

在本節中,您將 Hello World 元件發佈至 AWS IoT Greengrass 雲端服務。在 AWS IoT Greengrass 雲端服務中提供元件之後,您可以將其部署到核心裝置。您可以使用 GDK CLI 將元件從開發電腦發佈至 AWS IoT Greengrass 雲端服務。GDK CLI 會為您上傳元件的配方和成品。

將 Hello World 元件發佈至 AWS IoT Greengrass 服務
  1. 執行下列命令,使用 GDK CLI 建置元件。元件建置命令會根據 GDK CLI 組態檔案建立配方和成品。在此程序中,GDK CLI 會建立包含元件原始碼的 ZIP 檔案。

    gdk component build

    您應該會看到類似下列範例的訊息。

    [2022-04-28 11:20:16] INFO - Getting project configuration from gdk-config.json [2022-04-28 11:20:16] INFO - Found component recipe file 'recipe.yaml' in the project directory. [2022-04-28 11:20:16] INFO - Building the component 'com.example.BatteryAwareHelloWorld' with the given project configuration. [2022-04-28 11:20:16] INFO - Using 'zip' build system to build the component. [2022-04-28 11:20:16] WARNING - This component is identified as using 'zip' build system. If this is incorrect, please exit and specify custom build command in the 'gdk-config.json'. [2022-04-28 11:20:16] INFO - Zipping source code files of the component. [2022-04-28 11:20:16] INFO - Copying over the build artifacts to the greengrass component artifacts build folder. [2022-04-28 11:20:16] INFO - Updating artifact URIs in the recipe. [2022-04-28 11:20:16] INFO - Creating component recipe in 'C:\Users\finthomp\greengrassv2\com.example.BatteryAwareHelloWorld\greengrass-build\recipes'.
  2. 執行下列命令,將元件發佈至 AWS IoT Greengrass 雲端服務。元件發佈命令會將元件的 ZIP 檔案成品上傳至 S3 儲存貯體。然後,它會在元件配方中更新 ZIP 檔案的 S3 URI,並將配方上傳至服務 AWS IoT Greengrass 。在此程序中,GDK CLI 會檢查 AWS IoT Greengrass 雲端服務中已有可用的 Hello World 元件版本,因此可以選擇該版本之後的下一個修補程式版本。如果元件尚不存在,GDK CLI 會使用版本 1.0.0

    gdk component publish

    您應該會看到類似下列範例的訊息。輸出會告訴您 GDK CLI 建立的元件版本。

    [2022-04-28 11:20:29] INFO - Getting project configuration from gdk-config.json [2022-04-28 11:20:29] INFO - Found component recipe file 'recipe.yaml' in the project directory. [2022-04-28 11:20:29] INFO - Found credentials in shared credentials file: ~/.aws/credentials [2022-04-28 11:20:30] INFO - No private version of the component 'com.example.BatteryAwareHelloWorld' exist in the account. Using '1.0.0' as the next version to create. [2022-04-28 11:20:30] INFO - Publishing the component 'com.example.BatteryAwareHelloWorld' with the given project configuration. [2022-04-28 11:20:30] INFO - Uploading the component built artifacts to s3 bucket. [2022-04-28 11:20:30] INFO - Uploading component artifacts to S3 bucket: greengrass-component-artifacts-us-west-2-123456789012. If this is your first time using this bucket, add the 's3:GetObject' permission to each core device's token exchange role to allow it to download the component artifacts. For more information, see https://docs.aws.amazon.com/greengrass/v2/developerguide/device-service-role.html. [2022-04-28 11:20:30] INFO - Not creating an artifacts bucket as it already exists. [2022-04-28 11:20:30] INFO - Updating the component recipe com.example.BatteryAwareHelloWorld-1.0.0. [2022-04-28 11:20:31] INFO - Creating a new greengrass component com.example.BatteryAwareHelloWorld-1.0.0 [2022-04-28 11:20:31] INFO - Created private version '1.0.0' of the component in the account.'com.example.BatteryAwareHelloWorld'.
  3. 從輸出複製 S3 儲存貯體名稱。您稍後會使用儲存貯體名稱,以允許核心裝置從此儲存貯體下載元件成品。

  4. (選用) 在 AWS IoT Greengrass 主控台中檢視元件,以確認其已成功上傳。請執行下列操作:

    1. AWS IoT Greengrass 主控台導覽功能表中,選擇元件

    2. 元件頁面上,選擇我的元件索引標籤,然後選擇 com.example.BatteryAwareHelloWorld

      在此頁面上,您可以查看元件的配方和有關元件的其他資訊。

  5. 允許核心裝置存取 S3 儲存貯體中的元件成品。

    每個核心裝置都有一個核心裝置 IAM 角色,允許它與 互動 AWS IoT 並將日誌傳送到 AWS 雲端。根據預設,此裝置角色不允許存取 S3 儲存貯體,因此您必須建立並連接政策,以允許核心裝置從 S3 儲存貯體擷取元件成品。

    如果您裝置的角色已允許存取 S3 儲存貯體,您可以略過此步驟。否則,請建立允許存取的 IAM 政策,並將其連接至角色,如下所示:

    1. IAM 主控台導覽功能表中,選擇政策,然後選擇建立政策

    2. JSON 標籤上,用以下政策取代預留位置內容。將 greengrass-component-artifacts-us-west-2-123456789012 取代為 GDK CLI 上傳元件成品的 S3 儲存貯體名稱。

      例如,如果您us-west-2在 GDK CLI 組態檔案中指定 greengrass-component-artifacts和 ,且 AWS 帳戶 ID 為 123456789012,則 GDK CLI 會使用名為 的 S3 儲存貯體greengrass-component-artifacts-us-west-2-123456789012

      { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject" ], "Resource": "arn:aws:s3:::greengrass-component-artifacts-us-west-2-123456789012/*" } ] }
    3. 選擇 Next (下一步)

    4. 政策詳細資訊區段中,針對名稱輸入 MyGreengrassV2ComponentArtifactPolicy

    5. 選擇 建立政策

    6. IAM 主控台導覽功能表中,選擇角色,然後選擇核心裝置的角色名稱。您在安裝 AWS IoT Greengrass Core 軟體時指定了此角色名稱。如果您未指定名稱,則預設為 GreengrassV2TokenExchangeRole

    7. 許可下,選擇新增許可,然後選擇連接政策

    8. 新增許可頁面上,選取您建立MyGreengrassV2ComponentArtifactPolicy的政策旁的核取方塊,然後選擇新增許可