步骤 3:将组件发布到Amazon IoT Greengrass服务 - Amazon IoT Greengrass
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

步骤 3:将组件发布到Amazon IoT Greengrass服务

在本节中,你将 Hello World 组件发布到Amazon IoT Greengrass云服务。在Amazon IoT Greengrass云服务中提供组件后,您可以将其部署到核心设备。您可以使用 GDK CLI 将组件从开发计算机发布到Amazon IoT Greengrass云服务。GDK CLI 会为您上传组件的配方和工件。

将 Hello World 组件发布到该Amazon 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. 运行以下命令将组件发布到Amazon IoT Greengrass云服务。组件发布命令将组件的 ZIP 文件项目上传到 S3 存储桶。然后,它会在组件配方中更新 ZIP 文件的 S3 URI,并将配方上传到Amazon IoT Greengrass服务。在此过程中,GDK CLI 会检查Amazon 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. (可选)在Amazon IoT Greengrass控制台中查看组件以验证其是否成功上传。执行以下操作:

    1. Amazon IoT Greengrass控制台导航菜单中,选择组件

    2. 在 “组件” 页面上,选择 “我的组件” 选项卡,然后选择com.example.BatteryAwareHelloWorld

      在此页面上,您可以看到组件的配方以及有关该组件的其他信息。

  5. 允许核心设备访问 S3 存储桶中的组件工件。

    每台核心设备都有一个核心设备 IAM 角色,允许其与云进行交互Amazon IoT并将日志发送到Amazon云端。默认情况下,此设备角色不允许访问 S3 存储桶,因此您必须创建并附加允许核心设备从 S3 存储桶检索组件工件的策略。

    如果您的设备角色已允许访问 S3 存储桶,则可以跳过此步骤。否则,请创建允许访问的 IAM 策略并将其附加到该角色,如下所示:

    1. IAM 控制台导航菜单中,选择策略,然后选择创建策略

    2. JSON 选项卡中,将占位符内容替换为以下策略。将 greengrass-component-artifacts-us-west-2-123456789012 替换为 GDK CLI 上传组件工件的 S3 存储桶的名称。

      例如,如果您在 GDK CLI 配置文件us-west-2中指定了greengrass-component-artifacts和,而您的 Amazon Web Services 账户 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 控制台导航菜单中,选择角色,然后选择核心设备的角色名称。您在安装 C Amazon IoT Greengrass ore 软件时指定了此角色名称。如果您未指定名称,则默认为GreengrassV2TokenExchangeRole

    7. 权限下,选择添加权限,然后选择附加策略

    8. 添加权限页面上,选中您创建的MyGreengrassV2ComponentArtifactPolicy策略旁边的复选框,然后选择添加权限