

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 中設定資料庫和資料表的存取權 AWS Glue Data Catalog
<a name="fine-grained-access-to-glue-resources"></a>

如果您將 AWS Glue Data Catalog 與 Amazon Athena 搭配使用，您可以為 Athena 中使用的資料庫和資料表 Data Catalog 物件定義資源層級政策。

**注意**  
本主題討論了資料庫和資料表層級安全性。如需有關資料欄、資料列和儲存格層級安全性的資訊，請參閱 [Lake Formation 中的資料篩選和儲存格層級安全性](https://docs.aws.amazon.com/lake-formation/latest/dg/data-filtering.html)。

您可以定義 IAM 身分型政策中的資源層級許可。

**重要**  
本節討論 IAM 身分型政策中的資源層級許可。這些不同於資源型政策。如需有關相關差異的詳細資訊，請參閱《IAM 使用者指南》**中的[身分型政策和資源型政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_identity-vs-resource.html)。

請參閱以下主題以了解這些任務：


| 執行此任務 | 參閱以下主題 | 
| --- | --- | 
| 建立 IAM 政策來定義資源的存取權 | 《IAM 使用者指南》中的[建立 IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html)。 | 
| 了解 中使用的 IAM 身分型政策 AWS Glue | 《AWS Glue 開發人員指南》中的[身分型政策 (IAM 政策)](https://docs.aws.amazon.com/glue/latest/dg/using-identity-based-policies.html)。 | 

 **本節內容** 
+  [限制](#access-to-glue-resources-limitations) 
+  [根據 設定目錄和資料庫的 AWS Glue 存取權 AWS 區域](#full-access-to-default-db-per-region) 
+  [關於 中資料表分割區和版本的存取控制 AWS Glue](#access-to-glue-resources-table-partitions-and-versions) 
+  [資料庫和資料表層級許可的範例](#examples-fine-grained-table-database-policies) 

## 限制
<a name="access-to-glue-resources-limitations"></a>

當您使用 和 Athena 的資料庫 AWS Glue Data Catalog 和資料表層級存取控制時，請考慮下列限制：
+ 啟用 IAM Identity Center 的 Athena 工作群組需要將 Lake Formation 設定為使用 IAM Identity Center 身分。如需詳細資訊，請參閱《AWS Lake Formation 開發人員指南》**中的[整合 IAM Identity Center](https://docs.aws.amazon.com/lake-formation/latest/dg/identity-center-integration.html)。
+ 您可以限制只能存取資料庫和資料表。這些控制項適用於資料表層級。您不能限制存取資料表中個別分割區。如需詳細資訊，請參閱[關於 中資料表分割區和版本的存取控制 AWS Glue](#access-to-glue-resources-table-partitions-and-versions)。
+  AWS Glue Data Catalog 包含下列資源：`CATALOG`、`TABLE`、 `DATABASE`和 `FUNCTION`。
**注意**  
從此清單中，Athena 和 之間的常見資源 AWS Glue Data Catalog 是`CATALOG`每個帳戶的 `DATABASE`、 `TABLE`和 。 `Function` 是特定的 AWS Glue。對於 Athena 中的刪除動作，您必須包含 AWS Glue 動作的許可。請參閱 [資料庫和資料表層級許可的範例](#examples-fine-grained-table-database-policies)。

  其階層如下：`CATALOG` 是每個帳戶中所有 `DATABASES` 的上階，而每個 `DATABASE` 是它的所有 `TABLES` 和 `FUNCTIONS` 的上階。例如，在帳戶中目錄裡的 `db` 資料庫中名為 `table_test` 的資料表，它的上階是帳戶中的 `db` 和目錄。對 `db` 資料庫來說，它的上階是帳戶中的目錄，而它的子代是資料表和函數。如需有關資源階層架構的詳細資訊，請參閱《AWS Glue 開發人員指南》**中的[資料目錄中的 ARN 清單](https://docs.aws.amazon.com/glue/latest/dg/glue-specifying-resource-arns.html#data-catalog-resource-arns)。
+ 對於資源的任何非刪除 Athena 動作，例如 `CREATE DATABASE`、`CREATE TABLE`、`SHOW DATABASE`、`SHOW TABLE` 或 `ALTER TABLE`，您需要許可，才能對資源 (資料表或資料庫) 以及資料目錄中資源的所有上階呼叫此動作。例如，對於表格，其上階為其所屬的資料庫，以及帳戶的目錄。對於資料庫，其上階為帳戶的目錄。請參閱 [資料庫和資料表層級許可的範例](#examples-fine-grained-table-database-policies)。
+ 對於 Athena 中的刪除動作，例如 `DROP DATABASE` 或 `DROP TABLE`，您也需要許可，才能對資料型錄中資源的所有上階和下階呼叫刪除動作。例如，若要刪除資料庫，您需要資料庫，目錄 (即其上階)，以及所有表格和使用者定義函數 (即其下階) 的許可。表格沒有下階。若要執行 `DROP TABLE`，您需要許可，才能對表格、其所屬資料庫，以及目錄執行此動作。請參閱 [資料庫和資料表層級許可的範例](#examples-fine-grained-table-database-policies)。

## 根據 設定目錄和資料庫的 AWS Glue 存取權 AWS 區域
<a name="full-access-to-default-db-per-region"></a>

若要讓 Athena 使用 AWS Glue， AWS 區域 需要一個政策，授予 AWS Glue Data Catalog 您 帳戶中每個 的資料庫和 存取權。若要建立資料庫，還需要 `CreateDatabase` 許可。在下列範例政策中，將 AWS 區域、 AWS 帳戶 ID 和 資料庫名稱取代為您自己的名稱。

```
{
   "Sid": "DatabasePermissions",
   "Effect": "Allow",
   "Action": [
      "glue:GetDatabase", 
      "glue:GetDatabases",
      "glue:CreateDatabase"
   ],
   "Resource": [
     "arn:aws:glue:us-east-1:123456789012:catalog",
     "arn:aws:glue:us-east-1:123456789012:database/default"
   ]
}
```

## 關於 中資料表分割區和版本的存取控制 AWS Glue
<a name="access-to-glue-resources-table-partitions-and-versions"></a>

在 中 AWS Glue，資料表可以有分割區和版本。資料表版本和分割區不會被視為 中的獨立資源 AWS Glue。授予資料表和資料表上階資源的存取權，便可得到資料表版本和分割區的存取權。

基於存取控制之目的，以下存取許可適用：
+ 控制適用於資料表層級。您可以限制只能存取資料庫和資料表。例如，如果允許存取有分割區的資料表，則此存取權會套用到資料表中的所有分割區。您不能限制存取資料表中個別分割區。
**重要**  
若要在分割區 AWS Glue 上執行動作，目錄、資料庫和資料表層級需要分割區動作的許可。存取資料表內的分割區並不足夠。例如，若要在資料庫 `myDB` 中對資料表 `myTable` 執行 `GetPartitions`，您必須授予目錄、`myDB` 資料庫和 `myTable` 資源的 `glue:GetPartitions` 許可。
+ 存取控制不適用於資料表版本。與分割區一樣，透過存取資料表 AWS Glue 上 中的資料表版本 APIs 和資料表前代來授予對資料表舊版的存取權。

如需 AWS Glue 動作許可的相關資訊，請參閱《 *AWS Glue 開發人員指南*》中的 [AWS Glue API 許可：動作和資源參考](https://docs.aws.amazon.com/glue/latest/dg/api-permissions-reference.html)。

## 資料庫和資料表層級許可的範例
<a name="examples-fine-grained-table-database-policies"></a>

下列資料表列出了 IAM 身分型政策範例，這些政策允許對 Athena 中的資料庫和資料表進行存取。我們建議您從這些範例開始，並根據您的需要調整它們，來允許或拒絕對特定資料庫和資料表的特定動作。

這些範例包括存取資料庫和目錄，以便 Athena 和 AWS Glue 可以一起運作。對於多個 AWS 區域，請包含每個資料庫和目錄的類似政策，每個區域一行。

在此範例中，將 `example_db` 資料庫和 `test` 資料表名稱取代為您自己的資料庫和資料表名稱。


| DDL 陳述式 | IAM 存取政策授予資源存取權的範例 | 
| --- | --- | 
| ALTER DATABASE | 可讓您修改 example\$1db 資料庫的屬性。<pre>{<br />   "Effect": "Allow",<br />   "Action": [<br />      "glue:GetDatabase", <br />      "glue:UpdateDatabase"<br />   ],<br />   "Resource": [<br />     "arn:aws:glue:us-east-1:123456789012:catalog",<br />     "arn:aws:glue:us-east-1:123456789012:database/example_db"<br />   ]<br />}</pre> | 
| CREATE DATABASE | 可讓您建立名為 example\$1db 的資料庫。<pre>{<br />   "Effect": "Allow",<br />   "Action": [<br />      "glue:GetDatabase", <br />      "glue:CreateDatabase"<br />   ],<br />   "Resource": [<br />     "arn:aws:glue:us-east-1:123456789012:catalog",<br />     "arn:aws:glue:us-east-1:123456789012:database/example_db"<br />   ]<br />}<br /></pre> | 
| CREATE TABLE | 可讓您在 example\$1db 資料庫中建立名為 test 的資料表。<pre>{<br />   "Sid": "DatabasePermissions",<br />   "Effect": "Allow",<br />   "Action": [<br />      "glue:GetDatabase", <br />      "glue:GetDatabases"<br />   ],<br />   "Resource": [<br />     "arn:aws:glue:us-east-1:123456789012:catalog",<br />     "arn:aws:glue:us-east-1:123456789012:database/example_db"<br />   ]<br />},<br />{<br />   "Sid": "TablePermissions",<br />   "Effect": "Allow",<br />   "Action": [<br />      "glue:GetTables",<br />      "glue:GetTable",<br />      "glue:GetPartitions",<br />      "glue:CreateTable"<br />   ],<br />   "Resource": [<br />     "arn:aws:glue:us-east-1:123456789012:catalog",<br />     "arn:aws:glue:us-east-1:123456789012:database/example_db",<br />     "arn:aws:glue:us-east-1:123456789012:table/example_db/test"<br />   ]<br />}</pre> | 
| DROP DATABASE | 可讓您捨棄 example\$1db 資料庫，包括其中的所有資料表。<pre>{<br />   "Effect": "Allow",<br />   "Action": [<br />      "glue:GetDatabase",<br />      "glue:DeleteDatabase",<br />      "glue:GetTables", <br />      "glue:GetTable", <br />      "glue:DeleteTable" <br />   ],<br />   "Resource": [<br />     "arn:aws:glue:us-east-1:123456789012:catalog",<br />     "arn:aws:glue:us-east-1:123456789012:database/example_db", <br />     "arn:aws:glue:us-east-1:123456789012:table/example_db/*", <br />     "arn:aws:glue:us-east-1:123456789012:userDefinedFunction/example_db/*"<br />   ]<br /> }</pre> | 
| DROP TABLE | 可讓您捨棄 example\$1db 資料庫中名為 test 的分割資料表。如果您的資料表沒有分割區，請勿包含分割動作。<pre>{<br />   "Effect": "Allow",<br />   "Action": [<br />      "glue:GetDatabase",<br />      "glue:GetTable",<br />      "glue:DeleteTable", <br />      "glue:GetPartitions",<br />      "glue:GetPartition",<br />      "glue:DeletePartition" <br />   ],<br />   "Resource": [<br />     "arn:aws:glue:us-east-1:123456789012:catalog",<br />     "arn:aws:glue:us-east-1:123456789012:database/example_db", <br />     "arn:aws:glue:us-east-1:123456789012:table/example_db/test"<br />   ]<br /> }</pre> | 
| MSCK REPAIR TABLE | 允許您在將 Hive 相容分割區新增至 example\$1db 資料庫中名為 test 的資料表後更新目錄中繼資料。<pre>{<br />    "Effect": "Allow",<br />    "Action": [<br />        "glue:GetDatabase",<br />        "glue:CreateDatabase",<br />        "glue:GetTable",<br />        "glue:GetPartitions",<br />        "glue:GetPartition",<br />        "glue:BatchCreatePartition"<br />    ],<br />    "Resource": [<br />      "arn:aws:glue:us-east-1:123456789012:catalog",<br />      "arn:aws:glue:us-east-1:123456789012:database/example_db", <br />      "arn:aws:glue:us-east-1:123456789012:table/example_db/test"<br />    ]<br />}</pre> | 
| SHOW DATABASES | 可讓您列出 AWS Glue Data Catalog中的所有資料庫。<pre>{<br />   "Effect": "Allow",<br />   "Action": [<br />      "glue:GetDatabase",<br />      "glue:GetDatabases" <br />   ],<br />   "Resource": [<br />     "arn:aws:glue:us-east-1:123456789012:catalog",<br />     "arn:aws:glue:us-east-1:123456789012:database/*"<br />   ]<br /> }</pre> | 
| SHOW TABLES | 可讓您列出 example\$1db 資料庫中的所有資料表。<pre>{<br />   "Effect": "Allow",<br />   "Action": [<br />      "glue:GetDatabase",<br />      "glue:GetTables"    <br />   ],<br />   "Resource": [<br />     "arn:aws:glue:us-east-1:123456789012:catalog",<br />     "arn:aws:glue:us-east-1:123456789012:database/example_db",  <br />     "arn:aws:glue:us-east-1:123456789012:table/example_db/*"<br />   ]<br />}</pre> | 