

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

# 授权核心设备与 Amazon 服务交互
<a name="device-service-role"></a>

Amazon IoT Greengrass 核心设备使用 Amazon IoT Core 凭证提供程序来授权对 Amazon 服务的调用。 Amazon IoT Core 凭据提供程序允许设备使用其 X.509 证书作为对请求进行身份验证的唯一设备身份。 Amazon 这样就无需在 Amazon IoT Greengrass 核心设备上存储 Amazon 访问密钥 ID 和私有访问密钥。有关更多信息，请参阅《*Amazon IoT Core 开发人员指南》*中的[授权直接调用 Amazon 服务](https://docs.amazonaws.cn/iot/latest/developerguide/authorizing-direct-aws.html)。

在运行 Amazon IoT Greengrass Core 软件时，您可以选择配置核心设备所需的 Amazon 资源。这包括您的核心设备通过 Amazon IoT Core 证书提供商承担的 Amazon Identity and Access Management (IAM) 角色。使用`--provision true`参数配置允许核心设备获取临时 Amazon 证书的角色和策略。此参数还配置指向此 IAM Amazon IoT 角色的角色别名。您可以指定要使用的 IAM 角色的名称和 Amazon IoT 角色别名。如果您不使用这些其他名称参数指定 `--provision true`，Greengrass 核心设备将创建并使用以下默认资源：
+ IAM 角色：`GreengrassV2TokenExchangeRole`

  此角色具有名为 `GreengrassV2TokenExchangeRoleAccess` 的策略，以及允许 `credentials.iot.amazonaws.com` 代入角色的信任关系。该策略包括核心设备的最低权限。
**重要**  
该策略不包括对 S3 存储桶中文件的访问权限。您必须向该角色添加权限，才能允许核心设备从 S3 存储桶中检索组件构件。有关更多信息，请参阅 [允许访问 S3 存储桶中的组件构件](#device-service-role-access-s3-bucket)。
+ Amazon IoT 角色别名：`GreengrassV2TokenExchangeRoleAlias`

  此角色别名指的是 IAM 角色。

有关更多信息，请参阅 [步骤 3：安装 Amazon IoT Greengrass 核心软件](install-greengrass-v2.md)。

您也可以为现有核心设备设置角色别名。为此，请配置 [Greengrass Nucleus 组件](greengrass-nucleus-component.md)的 `iotRoleAlias` 配置参数。

您可以为此 IAM 角色获取临时 Amazon 证书，以便在自定义组件中执行 Amazon 操作。有关更多信息，请参阅 [与 Amazon 服务交互](interact-with-aws-services.md)。

**Topics**
+ [核心设备的服务角色权限](#device-service-role-permissions)
+ [允许访问 S3 存储桶中的组件构件](#device-service-role-access-s3-bucket)

## 核心设备的服务角色权限
<a name="device-service-role-permissions"></a>

该角色允许以下服务代入角色：
+ `credentials.iot.amazonaws.com`

如果您使用 Amazon IoT Greengrass Core 软件创建此角色，它将使用以下权限策略来允许核心设备连接并向其发送日志 Amazon。策略的名称默认为以 `Access` 结尾的 IAM 角色的名称。例如，如果您使用默认 IAM 角色名称，则此策略的名称为 `GreengrassV2TokenExchangeRoleAccess`。

------
#### [ Greengrass nucleus v2.5.0 and later ]

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents",
        "logs:DescribeLogStreams",
        "s3:GetBucketLocation"
      ],
      "Resource": "*"
    }
  ]
}
```

------

------
#### [ v2.4.x ]

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "iot:DescribeCertificate",
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents",
        "logs:DescribeLogStreams",
        "s3:GetBucketLocation"
      ],
      "Resource": "*"
    }
  ]
}
```

------

------
#### [ Earlier than v2.4.0 ]

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "iot:DescribeCertificate",
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents",
        "logs:DescribeLogStreams",
        "iot:Connect",
        "iot:Publish",
        "iot:Subscribe",
        "iot:Receive",
        "s3:GetBucketLocation"
      ],
      "Resource": "*"
    }
  ]
}
```

------

------

## 允许访问 S3 存储桶中的组件构件
<a name="device-service-role-access-s3-bucket"></a>

默认核心设备角色不允许核心设备访问 S3 存储桶。要部署在 S3 存储桶中有构件的组件，必须添加 `s3:GetObject` 权限，以允许核心设备下载组件构件。您可以向核心设备角色添加新策略以授予此权限。

**添加允许访问 Amazon S3 中组件构件的策略**

1. 创建一个名为 `component-artifact-policy.json` 的文件，并将以下 JSON 复制到该文件中。此策略允许访问 S3 存储桶中的所有文件。将 amzn-s3-demo-bucket 替换为 S3 存储桶的名称，以允许核心设备访问。

------
#### [ JSON ]

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
       {
         "Effect": "Allow",
         "Action": [
           "s3:GetObject"
         ],
         "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*"
       }
     ]
   }
   ```

------

1. 运行以下命令，从 `component-artifact-policy.json` 中的策略文档创建策略。

------
#### [ Linux or Unix ]

   ```
   aws iam create-policy \
     --policy-name MyGreengrassV2ComponentArtifactPolicy \
     --policy-document file://component-artifact-policy.json
   ```

------
#### [ Windows Command Prompt (CMD) ]

   ```
   aws iam create-policy ^
     --policy-name MyGreengrassV2ComponentArtifactPolicy ^
     --policy-document file://component-artifact-policy.json
   ```

------
#### [ PowerShell ]

   ```
   aws iam create-policy `
     --policy-name MyGreengrassV2ComponentArtifactPolicy `
     --policy-document file://component-artifact-policy.json
   ```

------

   从输出中的策略元数据复制策略 Amazon 资源名称（ARN）。您将在下一步中使用此 ARN 来将此策略附加到核心设备角色。

1. 运行以下命令以将策略附加到核心设备角色。*GreengrassV2TokenExchangeRole*替换为您在运行 C Amazon IoT Greengrass ore 软件时指定的角色名称。然后，将 policy ARN 替换为上一步中的 ARN。

------
#### [ Linux or Unix ]

   ```
   aws iam attach-role-policy \
     --role-name GreengrassV2TokenExchangeRole \
     --policy-arn arn:aws:iam::123456789012:policy/MyGreengrassV2ComponentArtifactPolicy
   ```

------
#### [ Windows Command Prompt (CMD) ]

   ```
   aws iam attach-role-policy ^
     --role-name GreengrassV2TokenExchangeRole ^
     --policy-arn arn:aws:iam::123456789012:policy/MyGreengrassV2ComponentArtifactPolicy
   ```

------
#### [ PowerShell ]

   ```
   aws iam attach-role-policy `
     --role-name GreengrassV2TokenExchangeRole `
     --policy-arn arn:aws:iam::123456789012:policy/MyGreengrassV2ComponentArtifactPolicy
   ```

------

   如果命令没有输出，则表示命令成功，且您的核心设备可以访问您上传到此 S3 存储桶的构件。