

# 使用 IAM 策略控制工作组访问
<a name="workgroups-iam-policy"></a>

要控制对工作组的访问，使用资源级 IAM 权限或基于身份的 IAM 策略。每当您使用 IAM 策略时，请确保遵循 IAM 最佳实践。有关更多信息，请参阅《[IAM 用户指南](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)》中的 *IAM 安全最佳实践*。

**注意**  
要访问启用了可信身份传播的工作组，必须将 IAM Identity Center 用户分配给由 Athena [GetWorkGroup](https://docs.aws.amazon.com/athena/latest/APIReference/API_GetWorkGroup.html) API 操作的响应返回的 `IdentityCenterApplicationArn`。

以下是 Athena 的特定过程。

有关 IAM 的特定信息，请参阅本节末尾列出的链接。有关示例 JSON 工作组策略的信息，请参阅[工作组策略示例](example-policies-workgroup.md)。

**要在 IAM 控制台中使用可视化编辑器创建工作组策略**

1. 登录 AWS 管理控制台，然后通过以下网址打开 IAM 控制台：[https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/)。

1. 在左侧的导航窗格中，选择 **Policies (策略)**，然后选择 **Create policy (创建策略)**。

1. 在**可视化编辑器**选项卡上，选择**选择服务**。然后，选择要添加到策略的 Athena。

1. 选择 **Select actions (选择操作)**，然后选择要添加到策略的操作。可视化编辑器会显示 Athena 中可用的操作。有关更多信息，请参阅《服务授权参考》**中的 [Amazon Athena 的操作、资源和条件键](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonathena.html)。

1. 选择 **add actions (添加操作)** 以键入特定操作，或使用通配符 (\$1) 指定多个操作。

   预设情况下，您创建的策略允许执行选择的操作。如果您在 Athena 中选择对 `workgroup` 资源执行一个或多个支持资源级权限的操作，则编辑器会列出 `workgroup` 资源。

1. 选择 **Resources (资源)**，为您的策略指定特定的工作组。有关示例 JSON 工作组策略，请参阅[工作组策略示例](example-policies-workgroup.md)。

1. 指定 `workgroup` 资源，如下所示：

   ```
   arn:aws:athena:<region>:<user-account>:workgroup/<workgroup-name>
   ```

1. 选择 **Review policy (查看策略)**，然后为您创建的策略键入 **Name (名称)** 和 **Description (描述)**（可选）。查看策略摘要以确保您已授予所需的权限。

1. 选择**创建策略**可保存您的新策略。

1. 将此基于身份的策略附加到用户、组或角色。

有关详细信息，请参阅*服务授权参考*和《*IAM 用户指南*》中的以下主题：
+  [Amazon Athena 的操作、资源和条件键](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonathena.html) 
+  [使用可视化编辑器创建策略](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html#access_policies_create-visual-editor) 
+  [添加和移除 IAM 策略](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage-attach-detach.html) 
+  [控制对资源的访问](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_controlling.html#access_controlling-resources) 

有关示例 JSON 工作组策略，请参阅[工作组策略示例](example-policies-workgroup.md)。

有关 Amazon Athena 操作的完整列表，请参阅 [Amazon Athena API 参考](https://docs.aws.amazon.com/athena/latest/APIReference/)中的 API 操作名称。

# 工作组策略示例
<a name="example-policies-workgroup"></a>

本节包含您可以用于启用对工作组执行各种操作的策略示例。每当您使用 IAM 策略时，请确保遵循 IAM 最佳实践。有关更多信息，请参阅《*IAM 用户指南*》中的 [IAM 安全最佳实践](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)。

工作组是由 Athena 管理的 IAM 资源。因此，如果您的工作组策略使用获取 `workgroup` 作为输入的操作，您必须如下所示指定工作组的 ARN：

```
"Resource": [arn:aws:athena:<region>:<user-account>:workgroup/<workgroup-name>]
```

其中，`<workgroup-name>` 是工作组的名称。例如，对于名为 `test_workgroup` 的工作组，指定它作为资源，如下所示：

```
"Resource": ["arn:aws:athena:us-east-1:123456789012:workgroup/test_workgroup"]
```

有关 Amazon Athena 操作的完整列表，请参阅 [Amazon Athena API 参考](https://docs.aws.amazon.com/athena/latest/APIReference/)中的 API 操作名称。有关 IAM policy 的更多信息，请参阅《IAM 用户指南》中的 [使用可视化编辑器创建策略](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html#access_policies_create-visual-editor)。有关为工作组创建 IAM 策略的更多信息，请参阅 [使用 IAM 策略控制工作组访问](workgroups-iam-policy.md)。
+  [Example policy for full access to all workgroups](#example1-full-access-all-wkgs) 
+  [Example policy for full access to a specified workgroup](#example2-full-access-this-wkg) 
+  [Example policy for running queries in a specified workgroup](#example3-user-access) 
+  [Example policy for running queries in the primary workgroup](#example4-run-in-primary-access) 
+  [Example policy for management operations on a specified workgroup](#example5-manage-wkgs-access) 
+  [Example policy for listing workgroups](#example6-list-all-wkgs-access) 
+  [Example policy for running and stopping queries in a specific workgroup](#example7-run-queries-access) 
+  [Example policy for working with named queries in a specific workgroup](#example8-named-queries-access) 
+  [Example policy for working with Spark notebooks](#example9-spark-workgroup) 

**Example 对所有工作组具有完全访问权限的策略示例**  
以下策略可实现对账户中可能存在的所有工作组资源具有完全访问权限。对于必须为其他所有用户监督和管理工作组的那些用户，我们建议您使用此策略。    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "athena:*"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}
```

**Example 对指定工作组具有完全访问权限的策略示例**  
以下策略可实现对单个名为 `workgroupA` 的特定工作组资源具有完全访问权限。对于对特定工作组具有完全控制权限的用户，您可以使用此策略。    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "athena:ListEngineVersions",
                "athena:ListWorkGroups",
                "athena:ListDataCatalogs",
                "athena:ListDatabases",
                "athena:GetDatabase",
                "athena:ListTableMetadata",
                "athena:GetTableMetadata"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "athena:BatchGetQueryExecution",
                "athena:GetQueryExecution",
                "athena:ListQueryExecutions",
                "athena:StartQueryExecution",
                "athena:StopQueryExecution",
                "athena:GetQueryResults",
                "athena:GetQueryResultsStream",
                "athena:CreateNamedQuery",
                "athena:GetNamedQuery",
                "athena:BatchGetNamedQuery",
                "athena:ListNamedQueries",
                "athena:DeleteNamedQuery",
                "athena:CreatePreparedStatement",
                "athena:GetPreparedStatement",
                "athena:ListPreparedStatements",
                "athena:UpdatePreparedStatement",
                "athena:DeletePreparedStatement"
            ],
            "Resource": [
                "arn:aws:athena:us-east-1:123456789012:workgroup/workgroupA"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "athena:DeleteWorkGroup",
                "athena:UpdateWorkGroup",
                "athena:GetWorkGroup",
                "athena:CreateWorkGroup"
            ],
            "Resource": [
                "arn:aws:athena:us-east-1:123456789012:workgroup/workgroupA"
            ]
        }
    ]
}
```

**Example 运行指定工作组中查询的策略示例**  
在以下策略中，允许用户运行指定 `workgroupA` 中的查询并查看它们。不允许用户对工作组本身执行管理任务，例如，更新或删除它。请注意，示例策略不会将用户仅限于此工作组或拒绝访问其他工作组。    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
       {
            "Effect": "Allow",
            "Action": [
                "athena:ListEngineVersions",
                "athena:ListWorkGroups",
                "athena:ListDataCatalogs",
                "athena:ListDatabases",
                "athena:GetDatabase",
                "athena:ListTableMetadata",
                "athena:GetTableMetadata"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "athena:GetWorkGroup", 
                "athena:BatchGetQueryExecution",
                "athena:GetQueryExecution",
                "athena:ListQueryExecutions",
                "athena:StartQueryExecution",
                "athena:StopQueryExecution",
                "athena:GetQueryResults",
                "athena:GetQueryResultsStream",
                "athena:CreateNamedQuery",
                "athena:GetNamedQuery",
                "athena:BatchGetNamedQuery",
                "athena:ListNamedQueries",
                "athena:DeleteNamedQuery",
                "athena:CreatePreparedStatement",
                "athena:GetPreparedStatement",
                "athena:ListPreparedStatements",
                "athena:UpdatePreparedStatement",
                "athena:DeletePreparedStatement"
            ],
            "Resource": [
                "arn:aws:athena:us-east-1:123456789012:workgroup/workgroupA"
            ]
        }
    ]
}
```

**Example 运行主工作组中查询的策略示例**  
您可以修改上述示例以允许特定用户在主工作组中运行查询。  
对于另行配置为运行指定工作组中查询的所有用户，我们建议您为其添加主工作组资源。在用户的指定工作组已被删除或禁用的情况下，在工作组用户策略中添加此策略资源有用。在这种情况下，他们可以继续运行主工作组中的查询。
要允许您账户中的用户运行主工作组中的查询，请将包含朱总做足 ARN 的行添加到 [Example policy for running queries in a specified workgroup](#example3-user-access) 的资源部分，如下例所示。  

```
arn:aws:athena:us-east-1:123456789012:workgroup/primary"
```

**Example 管理指定工作组操作的策略示例**  
在以下策略中，允许用户创建、删除、获取详细信息并更新工作组的 `test_workgroup`。    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "athena:ListEngineVersions"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "athena:CreateWorkGroup",
                "athena:GetWorkGroup",
                "athena:DeleteWorkGroup",
                "athena:UpdateWorkGroup"
            ],
            "Resource": [
                "arn:aws:athena:us-east-1:123456789012:workgroup/test_workgroup"
            ]
        }
    ]
}
```

**Example 列出工作组的策略示例**  
以下策略允许所有用户列出所有工作组：    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "athena:ListWorkGroups"
            ],
            "Resource": "*"
        }
    ]
}
```

**Example 运行和停止特定工作组中查询的策略示例**  
在此策略中，允许用户运行工作组中的查询：    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "athena:StartQueryExecution",
                "athena:StopQueryExecution"
            ],
            "Resource": [
                "arn:aws:athena:us-east-1:123456789012:workgroup/test_workgroup"
            ]
        }
    ]
}
```

**Example 使用特定工作组中的命名查询的策略示例**  
在以下策略中，用户具有在指定工作组中创建和删除命名查询以及获取其相关信息的权限：    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "athena:CreateNamedQuery",
                "athena:GetNamedQuery",
                "athena:DeleteNamedQuery"
            ],
            "Resource": [
                "arn:aws:athena:us-east-1:123456789012:workgroup/test_workgroup"            ]
        }
    ]
}
```

**Example 在 Athena 中使用 Spark 笔记本的示例策略**  
使用以下策略在 Athena 中使用 Spark 笔记本。    
****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowCreatingWorkGroupWithDefaults",
            "Action": [
                "athena:CreateWorkGroup",
                "s3:CreateBucket",
                "iam:CreateRole",
                "iam:CreatePolicy",
                "iam:AttachRolePolicy",
                "s3:GetBucketLocation",
                "athena:ImportNotebook"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:athena:us-east-1:123456789012:workgroup/Demo*",
                "arn:aws:s3:::123456789012-us-east-1-athena-results-bucket-*",
                "arn:aws:iam::123456789012:role/service-role/AWSAthenaSparkExecutionRole-*",
                "arn:aws:iam::123456789012:policy/service-role/AWSAthenaSparkRolePolicy-*"
            ]
        },
        {
            "Sid": "AllowRunningCalculations",
            "Action": [
                "athena:ListWorkGroups",
                "athena:GetWorkGroup",
                "athena:StartSession",
                "athena:CreateNotebook",
                "athena:ListNotebookMetadata",
                "athena:ListNotebookSessions",
                "athena:GetSessionStatus",
                "athena:GetSession",
                "athena:GetNotebookMetadata",
                "athena:CreatePresignedNotebookUrl"
            ],
            "Effect": "Allow",
            "Resource": "arn:aws:athena:us-east-1:123456789012:workgroup/Demo*"
        },
        {
            "Sid": "AllowListWorkGroupAndEngineVersions",
            "Action": [
                "athena:ListWorkGroups",
                "athena:ListEngineVersions"
            ],
            "Effect": "Allow",
            "Resource": "*"
        }
    ]
}
```