与Amazon服务互动 - Amazon IoT Greengrass
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

与Amazon服务互动

Greengrass 核心设备使用 X.509 证书通过 TLS 双向身份验证协议进行连接。Amazon IoT Core这些证书允许设备在Amazon IoT没有Amazon凭证的情况下进行交互,证书通常包括访问密钥 ID 和私有访问密钥。其他Amazon服务需要Amazon凭据而不是 X.509 证书才能在服务端点调用 API 操作。 Amazon IoT Core有一个凭据提供程序,允许设备使用其 X.509 证书对请求进行身份验证。AmazonAmazon IoT凭证提供者使用 X.509 证书对设备进行身份验证,并以临时的有限Amazon权限安全令牌的形式颁发证书。设备可以使用此令牌对任何Amazon请求进行签名和身份验证。这样就无需在 Greengrass 核心设备上存储Amazon凭证。有关更多信息,请参阅《Amazon IoT Core开发人员指南》中的授权直接调用Amazon服务

要从 Amazon IoT Greengrass 获取证书,核心设备使用指向 IAM 角色Amazon IoT的角色别名。此 IAM 角色称为令牌交换角色。在安装 C Amazon IoT Greengrass ore 软件时,您可以创建角色别名和令牌交换角色。要指定核心设备使用的角色别名,请配置的iotRoleAlias参数Greengrass 核

Amazon IoT凭证提供商代表您扮演令牌交换角色,为核心设备提供Amazon凭证。您可以将适当的 IAM 策略附加到此角色,以允许您的核心设备访问您的Amazon资源,例如 S3 存储桶中的组件工件。有关如何配置令牌交换角色的更多信息,请参阅授权核心设备与Amazon服务

Greengrass 核心设备将凭据Amazon存储在内存中,默认情况下,凭证会在一小时后过期。如果 Amazon IoT Greengrass Core 软件重新启动,则必须重新获取凭据。您可以使用该UpdateRoleAlias操作来配置凭证的有效期限。

Amazon IoT Greengrass提供了一个公共组件,即令牌交换服务组件,您可以将其定义为自定义组件中的依赖项以与Amazon服务进行交互。令牌交换服务为您的组件提供了一个环境变量AWS_CONTAINER_CREDENTIALS_FULL_URI,该变量定义了提供Amazon凭据的本地服务器的 URI。创建 S Amazon DK 客户端时,客户端会检查此环境变量并连接到本地服务器以检索Amazon凭证,并使用这些凭证签署 API 请求。这样,您就可以使用 Amazon SDK 和其他工具来调用组件中的Amazon服务。有关更多信息,请参阅 代币兑换服务

重要

2016 年 7 月 13 日,AmazonSDK 中增加了以这种方式获取Amazon凭证的支持。您的组件必须使用在该日期或之后创建的 Amazon SDK 版本。有关更多信息,请参阅《亚马逊弹性容器服务开发者指南》中的使用支持的Amazon软件开发工具包。

要在自定义组件中获取Amazon凭据,请在组件配方中定义aws.greengrass.TokenExchangeService为依赖项。以下示例配方定义了一个组件,该组件用于安装 boto3 并运行一个 Python 脚本,该脚本使用来自令牌交换服务的Amazon证书列出 Amazon S3 存储桶。

注意

要运行此示例组件,您的设备必须具有s3:ListAllMyBuckets权限。有关更多信息,请参阅 授权核心设备与Amazon服务

JSON
{ "RecipeFormatVersion": "2020-01-25", "ComponentName": "com.example.ListS3Buckets", "ComponentVersion": "1.0.0", "ComponentDescription": "A component that uses the token exchange service to list S3 buckets.", "ComponentPublisher": "Amazon", "ComponentDependencies": { "aws.greengrass.TokenExchangeService": { "VersionRequirement": "^2.0.0", "DependencyType": "HARD" } }, "Manifests": [ { "Platform": { "os": "linux" }, "Lifecycle": { "install": "pip3 install --user boto3", "run": "python3 -u {artifacts:path}/list_s3_buckets.py" } }, { "Platform": { "os": "windows" }, "Lifecycle": { "install": "pip3 install --user boto3", "run": "py -3 -u {artifacts:path}/list_s3_buckets.py" } } ] }
YAML
--- RecipeFormatVersion: '2020-01-25' ComponentName: com.example.ListS3Buckets ComponentVersion: '1.0.0' ComponentDescription: A component that uses the token exchange service to list S3 buckets. ComponentPublisher: Amazon ComponentDependencies: aws.greengrass.TokenExchangeService: VersionRequirement: '^2.0.0' DependencyType: HARD Manifests: - Platform: os: linux Lifecycle: install: pip3 install --user boto3 run: |- python3 -u {artifacts:path}/list_s3_buckets.py - Platform: os: windows Lifecycle: install: pip3 install --user boto3 run: |- py -3 -u {artifacts:path}/list_s3_buckets.py

此示例组件运行以下 Python 脚本list_s3_buckets.py,其中列出了 Amazon S3 存储桶。

import boto3 import os try: print("Creating boto3 S3 client...") s3 = boto3.client('s3') print("Successfully created boto3 S3 client") except Exception as e: print("Failed to create boto3 s3 client. Error: " + str(e)) exit(1) try: print("Listing S3 buckets...") response = s3.list_buckets() for bucket in response['Buckets']: print(f'\t{bucket["Name"]}') print("Successfully listed S3 buckets") except Exception as e: print("Failed to list S3 buckets. Error: " + str(e)) exit(1)