与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请求进行签名和身份验证。这样就无需在 Greenggsc Contaings 核心设备上存储Amazon凭证。有关更多信息,请参阅Amazon IoT Core开发人员指南中的授权直接调用Amazon服务

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

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 日将 Amazon Support 添加到开发工具包中。Amazon您的组件必须使用在该日期或之后创建的Amazon开发工具包版本。有关更多信息,请参阅 Amazon Emazon Emaon Emaon E Amazon s dk。

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