本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
第 3 步:将组件发布到 Amazon IoT Greengrass 服务
在本节中,您将 Hello World 组件发布到 Amazon IoT Greengrass 云服务。Amazon IoT Greengrass 云服务推出组件后,您可以将其部署到核心设备。您可以使用 GDK CLI 将组件从开发计算机发布到 Amazon IoT Greengrass 云服务。GDK CLI 会上传组件的配方和构件。
将 Hello World 组件发布到 Amazon IoT Greengrass 服务
-
运行以下命令,使用 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'.
-
运行以下命令,将组件发布到 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'.
-
从输出中复制 S3 存储桶名称。您稍后会使用桶名称来允许核心设备从该桶下载组件构件。
-
(可选)在 Amazon IoT Greengrass 控制台中查看组件以验证其是否已成功上传。执行以下操作:
-
在 Amazon IoT Greengrass 控制台
导航菜单中,选择组件。 -
在组件页面上,选择我的组件选项卡,然后选择 com.example.BatteryAwareHelloWorld。
在此页面上,您可以看到组件的配方以及有关该组件的其他信息。
-
-
允许核心设备访问 S3 存储桶中的组件构件。
每台核心设备都有一个核心设备 IAM 角色,允许其与 Amazon IoT 进行交互并将日志发送到 Amazon 云。默认情况下,此设备角色不允许访问 S3 存储桶,因此您必须创建并附加一个允许核心设备从 S3 存储桶检索组件构件的策略。
如果设备的角色已允许访问 S3 存储桶,则可跳过此步骤。否则,请创建允许访问的 IAM 策略并将其附加到该角色,如下所示:
-
在 IAM 控制台
的导航菜单中,选择策略,然后选择创建策略。 -
在 JSON 选项卡中,将占位符内容替换为以下策略。将
greengrass-component-artifacts-us-west-2-123456789012
替换为 GDK CLI 向其中上传组件构件的 S3 存储桶的名称。例如,如果您在 GDK CLI 配置文件中指定了
greengrass-component-artifacts
和us-west-2
,而您的 Amazon Web Services 账户 ID 为123456789012
,则 GDK CLI 将使用名为greengrass-component-artifacts-us-west-2-123456789012
的 S3 存储桶。{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject" ], "Resource": "arn:aws:s3:::
greengrass-component-artifacts-us-west-2-123456789012
/*" } ] } -
选择下一步。
-
在策略详细信息部分,对于名称,请输入
MyGreengrassV2ComponentArtifactPolicy
。 -
选择创建策略。
-
在 IAM 控制台
导航菜单中,选择角色,然后选择核心设备的角色名称。您在安装 Amazon IoT Greengrass Core 软件时指定了此角色名称。如果您未指定名称,则默认使用 GreengrassV2TokenExchangeRole
。 -
在权限下,选择添加权限,然后选择附加策略。
-
在添加权限页面,选中与您创建的策略
MyGreengrassV2ComponentArtifactPolicy
对应的复选框,然后选择添加权限。
-