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

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

与 Amazon 服务互动

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

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

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创建 Amazon SDK客户端时,客户端会检查此环境变量并连接到本地服务器以检索 Amazon 凭据,并使用这些凭据对API请求进行签名。这允许您使用 Amazon SDKs和其他工具来调用组件中的 Amazon 服务。有关更多信息,请参阅 令牌交换服务

重要

2016 年 7 月 13 日新增了 Amazon SDKs对以这种方式获取 Amazon 证书的支持。您的组件必须使用在该日期或之后创建的 Amazon SDK版本。有关更多信息,请参阅《Amazon 弹性容器服务开发者指南》 Amazon SDK中的 “使用支持的”。

要在自定义组件中获取 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)