

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

# 使用 Lake Formation 標籤型存取控制管理資料湖
<a name="managing-dl-tutorial"></a>

成千上萬的客戶正在建置 PB 級資料湖 AWS。其中許多客戶使用 AWS Lake Formation 在整個組織中輕鬆建置和共用其資料湖。隨著資料表和使用者數量的增加，資料管理員和管理員正在尋找可以輕鬆大規模管理資料湖許可的方法。Lake Formation 標籤型存取控制 (LF-TBAC) 允許資料管理員建立 *LF 標籤* （根據其資料分類和本體），然後連接到資源，以解決此問題。

LF-TBAC 是一種授權策略，可根據屬性定義許可。在 Lake Formation 中，這些屬性稱為 LF 標籤。您可以將 LF 標籤連接至 Data Catalog 資源和 Lake Formation 主體。資料湖管理員可以使用 LF 標籤來指派和撤銷 Lake Formation 資源的許可。如需詳細資訊，請參閱 [Lake Formation 標籤型存取控制](tag-based-access-control.md)。

 本教學課程示範如何使用 AWS 公有資料集建立 Lake Formation 標籤型存取控制政策。此外，它示範如何查詢具有與其相關聯的 Lake Formation 標籤型存取政策的資料表、資料庫和資料欄。

您可以針對下列使用案例使用 LF-TBAC：
+ 您有大量資料表和主體，資料湖管理員必須授予存取權
+ 您想要根據 目錄對資料進行分類，並根據分類授予許可
+ 資料湖管理員想要以鬆散耦合的方式動態指派許可

以下是使用 LF-TBAC 設定許可的高階步驟：

1. 資料管理員使用兩個 LF 標籤來定義標籤本體： `Confidential`和 `Sensitive`。使用 的資料`Confidential=True`具有更嚴格的存取控制。使用 的資料`Sensitive=True`需要分析師的特定分析。

1. 資料管理員會將不同的許可層級指派給資料工程師，以使用不同的 LF 標籤建置資料表。

1. 資料工程師會建置兩個資料庫： `tag_database`和 `col_tag_database`。中的所有資料表`tag_database`都使用 設定`Confidential=True`。中的所有資料表`col_tag_database`都使用 設定`Confidential=False`。中資料表的某些資料欄`col_tag_database`會以 標記，`Sensitive=True`以滿足特定分析需求。

1. 對於具有特定表達式條件 `Confidential=True`和 的資料表`Confidential=False`，資料工程師會授予分析師讀取許可`Sensitive=True`。

1. 透過此組態，資料分析師可以專注於使用正確的資料執行分析。

**Topics**
+ [目標對象](#tut-manage-dl-roles)
+ [先決條件](#tut-manage-dl-prereqs)
+ [步驟 1：佈建您的 資源](#tut-manage-dl-provision-resources)
+ [步驟 2：註冊您的資料位置、建立 LF 標籤本體，以及授予許可](#tut-manage-dl-register-datalocation-lftag)
+ [步驟 3：建立 Lake Formation 資料庫](#tut-manage-dl-tbac-create-databases)
+ [步驟 4：授予資料表許可](#tut-manage-dl-grant-table-permissions)
+ [步驟 5：在 Amazon Athena 中執行查詢以驗證許可](#tut-manage-dl-tbac-run-query)
+ [步驟 6：清除 AWS 資源](#tut-manage-dl-tbac-clean-up-db)

## 目標對象
<a name="tut-manage-dl-roles"></a>



本教學課程適用於資料管理員、資料工程師和資料分析師。在 Lake Formation 中管理 AWS Glue Data Catalog 許可時，生產帳戶中的資料管理員會根據其支援的功能擁有功能所有權，並且可以授予各種取用者、外部組織和帳戶的存取權。

下表列出本教學課程中使用的角色：


| Role | Description | 
| --- | --- | 
| 資料管理員 （管理員） | lf-data-steward 使用者具有下列存取權：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/lake-formation/latest/dg/managing-dl-tutorial.html)  | 
| 資料工程師 |  `lf-data-engineer` 使用者具有下列存取權： [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/lake-formation/latest/dg/managing-dl-tutorial.html)  | 
| 資料分析 | lf-data-analyst 使用者具有下列存取權：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/lake-formation/latest/dg/managing-dl-tutorial.html)  | 

## 先決條件
<a name="tut-manage-dl-prereqs"></a>

開始本教學課程之前，您必須擁有可用於以具有正確許可的管理使用者身分登入 AWS 帳戶 的 。如需詳細資訊，請參閱[完成初始 AWS 組態任務](getting-started-setup.md#initial-aws-signup)。

本教學課程假設您熟悉 IAM。如需 IAM 的相關資訊，請參閱 [IAM 使用者指南](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html)。

## 步驟 1：佈建您的 資源
<a name="tut-manage-dl-provision-resources"></a>

本教學課程包含快速設定的 AWS CloudFormation 範本。您可以檢閱並自訂它，以符合您的需求。範本會建立三個不同的角色 （列於 [目標對象](#tut-manage-dl-roles)) 來執行此練習，並將 nyc-taxi-data 資料集複製到本機 Amazon S3 儲存貯體。
+ Amazon S3 儲存貯體
+ 適當的 Lake Formation 設定
+ 適當的 Amazon EC2 資源
+ 具有登入資料的三個 IAM 角色

**建立您的 資源**

1. 在美國東部 （維吉尼亞北部） 區域登入 AWS CloudFormation 主控台，網址為 https：//[https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/)。

1. 選擇[啟動堆疊](https://console.aws.amazon.com/cloudformation/home?region=us-east-1#/stacks/new?templateURL=https://aws-bigdata-blog.s3.amazonaws.com/artifacts/lakeformationtbac/cfn/tbac_permission.json)。

1.  選擇**下一步**。

1.  在**使用者組態**區段中，輸入三個角色的密碼： `DataStewardUserPassword``DataEngineerUserPassword`和 `DataAnalystUserPassword`。

1.  檢閱最終頁面上的詳細資訊，然後選取**我確認 AWS CloudFormation 可能會建立 IAM 資源**。

1.  選擇**建立**。

   堆疊建立最多可能需要五分鐘。

**注意**  
完成教學課程後，您可能想要在 中刪除堆疊 CloudFormation ，以避免繼續產生費用。確認資源在堆疊的事件狀態中已成功刪除。

## 步驟 2：註冊您的資料位置、建立 LF 標籤本體，以及授予許可
<a name="tut-manage-dl-register-datalocation-lftag"></a>

在此步驟中，資料管理員使用者使用兩個 LF 標籤定義標籤本體： `Confidential`和 `Sensitive`，並讓特定 IAM 主體能夠將新建立的 LF 標籤連接至 資源。

**註冊資料位置並定義 LF 標籤本體**

1. 以資料管理員使用者 (`lf-data-steward`) 身分執行第一個步驟，以驗證 Amazon S3 中的資料和 Lake Formation 中的資料目錄。

   1. 登入 Lake Formation 主控台，網址為 [https://console.aws.amazon.com/lakeformation/](https://console.aws.amazon.com/lakeformation/)：`lf-data-steward`// CloudFormation 。

   1. 在導覽窗格的 **Permissions**̧ 下，選擇**管理角色和任務**。

   1. 在**資料湖管理員**區段中選擇**新增**。

   1. 在**新增管理員**頁面上，針對 **IAM 使用者和角色**，選擇使用者 `lf-data-steward`。

   1. 選擇**儲存**以新增`lf-data-steward`為 Lake Formation 管理員。

1. 接著，更新 Data Catalog 設定以使用 Lake Formation 許可來控制目錄資源，而不是以 IAM 為基礎的存取控制。

   1. 在導覽窗格的**管理**下，選擇 **Data Catalog 設定**。

   1. 取消勾選 **僅對新資料庫使用 IAM 存取控制**。

   1. 取消核取 **僅對新資料庫中的新資料表使用 IAM 存取控制**。

   1. 按一下 **Save (儲存)**。

1. 接著，註冊資料湖的資料位置。

   1. 在導覽窗格的**管理**下，選擇**資料湖位置**。

   1. 選擇**註冊位置**。

   1. 在**註冊位置**頁面上，針對 **Amazon S3 路徑**，輸入 `s3://lf-tagbased-demo-Account-ID`。

   1. 對於 **IAM** 角色 ̧ 保持預設值`AWSServiceRoleForLakeFormationDataAccess`不變。

   1. 選擇 **Lake Formation** 作為許可模式。

   1. 選擇**註冊位置**。

1. 接著，定義 LF 標籤來建立 拓撲。

   1. 在導覽窗格中的**許可**下，選擇 **LF 標籤和許可。**

   1. 選擇**新增 LF 標籤**。

   1. 在 **Key** (索引鍵) 欄位，輸入 `Confidential`。

   1. 針對**值**，新增 `True`和 `False`。

   1. 選擇**新增 LF 標籤**。

   1. 重複這些步驟以建立值`Sensitive`為 的 **LF 標籤**`True`。

   您已為此練習建立所有必要的 LF 標籤。

**將許可授予 IAM 使用者**

1. 接著，讓特定 IAM 主體能夠將新建立的 LF 標籤連接到資源。

   1. 在導覽窗格中的**許可**下，選擇 **LF 標籤和許可**。

   1. 在 **LF-Tag 許可**區段中，選擇**授予許可**。

   1. 針對**許可類型**，選擇 **LF-Tag 鍵/值對許可**。

   1. 選取 **IAM 使用者和角色**。

   1. 對於 **IAM 使用者和角色**，搜尋並選擇`lf-data-engineer`角色。

   1. 在 **LF 標籤**區段中，新增`Confidential`具有值 `True`和 的金鑰`False`，以及`key``Sensitive`具有值 的 `True`。

   1. 在**許可**下，選取**描述**和**關聯****許可**和**可授予許可**。

   1. 選擇 **Grant** (授予)。

1. 接著，將許可授予 `lf-data-engineer`，以在 Data Catalog 和 建立的基礎 Amazon S3 儲存貯體上建立資料庫 AWS CloudFormation。

   1. 在導覽窗格中的**管理**下，選擇**管理角色和任務**。

   1.  在**資料庫建立者**區段中，選擇**授予**。

   1. 對於 **IAM 使用者和角色**，選擇`lf-data-engineer`角色。

   1. 針對**目錄許可**，選取**建立資料庫**。

   1. 選擇 **Grant** (授予)。

1. 接著，將 Amazon S3 儲存貯體的許可授予`(s3://lf-tagbased-demo-Account-ID)``lf-data-engineer`使用者。

   1. 在導覽窗格中的**許可**下，選擇**資料位置**。

   1. 選擇 **Grant** (授予)。

   1. 選取**我的帳戶**。

   1. 對於 **IAM 使用者和角色**，選擇`lf-data-engineer`角色。

   1. 對於**儲存位置**，輸入 CloudFormation 範本 建立的 Amazon S3 儲存貯體`(s3://lf-tagbased-demo-Account-ID)`。

   1. 選擇 **Grant** (授予)。

1. 接著，對與 **LF-Tag** 表達式 相關聯的資源授予可`lf-data-engineer`授予的許可`Confidential=True`。

   1. 在導覽窗格中的**許可**下，選擇**資料湖許可**。

   1. 選擇 **Grant** (授予)。

   1. 選取 **IAM 使用者和角色**。

   1.  選擇角色 `lf-data-engineer`。

   1. 在 **LF 標籤或目錄資源**區段中，選取**符合 LF 標籤的資源**。

   1. 選擇**新增 LF 標籤鍵/值對**。

   1.  新增`Confidential`值為 的金鑰`True`。

   1. 在**資料庫許可**區段中，選取**描述****資料庫許可**和**可授予許可**。

   1. 在**資料表許可**區段中，針對**資料表許可**和**可授予許可**，選取**描述**、**選取**和**修改**。

   1.  選擇 **Grant** (授予)。

1. 接著，對與 LF-Tag 表達式 相關聯的資源授予可`lf-data-engineer`授予的許可`Confidential=False`。

   1. 在導覽窗格中的**許可**下，選擇**資料湖許可**。

   1. 選擇 **Grant** (授予)。

   1. 選取 **IAM 使用者和角色**。

   1. 選擇角色 `lf-data-engineer`。

   1.  選取**符合 LF 標籤的資源**。

   1. 選擇**新增 LF 標籤**。

   1.  新增值`Confidential`為 的金鑰`False`。

   1. 在**資料庫許可**區段中，選取**描述****資料庫許可**和**可授予許可**。

   1. 在**資料表和資料欄許可**區段中，請勿選取任何項目。

   1. 選擇 **Grant** (授予)。

1. 接著，我們對與 **LF-Tag** 鍵/值對`Confidential=False`和 相關聯的資源授予可`lf-data-engineer`授予的許可`Sensitive=True`。

   1. 在導覽窗格中的**許可**下，選擇**資料許可**。

   1. 選擇 **Grant** (授予)。

   1. 選取 **IAM 使用者和角色**。

   1. 選擇角色 `lf-data-engineer`。

   1. 在 **LF 標籤或目錄資源**區段下，選取**符合 LF 標籤的資源**。

   1. 選擇**新增 LF 標籤**。

   1.  新增值`Confidential`為 的金鑰`False`。

   1. 選擇**新增 LF 標籤鍵/值對**。

   1. 新增值`Sensitive`為 的金鑰`True`。

   1. 在**資料庫許可**區段中，選取**描述****資料庫許可**和**可授予許可**。

   1. 在**資料表許可**區段中，針對**資料表許可**和**可授予許可**，選取**描述**、**選取**和**修改**。

   1. 選擇 **Grant** (授予)。

## 步驟 3：建立 Lake Formation 資料庫
<a name="tut-manage-dl-tbac-create-databases"></a>

在此步驟中，您會建立兩個資料庫，並將 LF 標籤連接至資料庫和特定資料欄以進行測試。

**建立資料庫和資料表以進行資料庫層級存取**

1. 首先，建立資料庫 `tag_database`、資料表 `source_data`，並連接適當的 LF 標籤。

   1. 在 Lake Formation 主控台 ([https://console.aws.amazon.com/lakeformation/](https://console.aws.amazon.com/lakeformation/)：//) **的資料目錄**下，選擇**資料庫**。

   1. 選擇**建立資料庫**。

   1. 對於**名稱**，輸入 `tag_database`。

   1. 針對**位置**，輸入 CloudFormation 範本 建立的 Amazon S3 位置`(s3://lf-tagbased-demo-Account-ID/tag_database/)`。

   1. 取消選取 **僅對此資料庫中的新資料表使用 IAM 存取控制**。

   1. 選擇**建立資料庫**。

1. 接著，在 中建立新的資料表`tag_database`。

   1. 在**資料庫**頁面上，選取資料庫 `tag_database`。

   1.  選擇**檢視資料表**，然後按一下**建立資料表**。

   1. 對於**名稱**，輸入 `source_data`。

   1. 在 **Database** (資料庫) 中，選擇 `tag_database` 資料庫。

   1. 針對**資料表格式**，選擇**標準 AWS Glue 資料表**。

   1. 對於**位於 的資料**，選取**我帳戶中的指定路徑**。

   1. 針對包含路徑，輸入 CloudFormation 範本 所`tag_database`建立的路徑`(s3://lf-tagbased-demoAccount-ID/tag_database/)`。

   1. 針對**資料格式**，選取 **CSV**。

   1. 在**上傳結構描述**下，輸入下列資料欄結構的 JSON 陣列來建立結構描述：

      ```
       [
                     {
                          "Name": "vendorid",
                          "Type": "string"
                     },
                     {
                          "Name": "lpep_pickup_datetime",
                          "Type": "string"                    
                     },
                     {
                          "Name": "lpep_dropoff_datetime",
                          "Type": "string"  
                    
                     },
                        {
                          "Name": "store_and_fwd_flag",
                          "Type": "string"                                
                     },
                        {
                          "Name": "ratecodeid",
                          "Type": "string"                   
                          
                     },
                        {
                          "Name": "pulocationid",
                          "Type": "string"                   
                          
                     },
                     {
                          "Name": "dolocationid",
                          "Type": "string"                   
                          
                     },
                        {
                          "Name": "passenger_count",
                          "Type": "string"                   
                          
                     },
                     {
                          "Name": "trip_distance",
                          "Type": "string"                    
                          
                     }, 
                        {
                          "Name": "fare_amount",
                          "Type": "string"                   
                          
                     },
                     {
                          "Name": "extra",
                          "Type": "string"                   
                          
                     },
                        {
                          "Name": "mta_tax",
                          "Type": "string"                    
                          
                     },
                     {
                          "Name": "tip_amount",
                          "Type": "string"                   
                          
                     },
                        {
                          "Name": "tolls_amount",
                          "Type": "string"                   
                          
                     },
                     {
                          "Name": "ehail_fee",
                          "Type": "string"                    
                          
                     }, 
                     {
                          "Name": "improvement_surcharge",
                          "Type": "string"                   
                          
                     },
                     {
                          "Name": "total_amount",
                          "Type": "string"                    
                          
                     },
                     {
                          "Name": "payment_type",
                          "Type": "string"                    
                          
                     }
       ]
      ```

   1. 選擇**上傳**。上傳結構描述後，資料表結構描述看起來應該如下螢幕擷取畫面：  
![\[Table schema with 18 columns showing column names and data types, all set to string.\]](http://docs.aws.amazon.com/zh_tw/lake-formation/latest/dg/images/tutorial-manage-dl-tbac1.jpg)

   1. 選擇**提交**。

1. 接著，在資料庫層級連接 LF 標籤。

   1. 在**資料庫**頁面上，尋找並選取 `tag_database`。

   1. 在**動作**功能表中，選擇**編輯 LF 標籤**。

   1. 選擇**指派新的 LF 標籤**。

   1. 針對**指派的 key**̧，選擇您先前建立的 `Confidential` LF 標籤。

   1. 針對**值**，選擇 `True`。

   1. 選擇**儲存**。

   如此即完成 tag\$1database 資料庫的 LF-Tag 指派。

**建立資料庫和資料表以進行資料欄層級存取**

重複下列步驟來建立資料庫`col_tag_database`和資料表 `source_data_col_lvl`，並在資料欄層級連接 LF 標籤。

1. 在**資料庫**頁面上，選擇**建立資料庫**。

1. 對於**名稱**，輸入 `col_tag_database`。

1. 針對**位置**，輸入 CloudFormation 範本 建立的 Amazon S3 位置`(s3://lf-tagbased-demo-Account-ID/col_tag_database/)`。

1. 取消選取 **僅對此資料庫中的新資料表使用 IAM 存取控制**。

1. 選擇**建立資料庫**。

1. 在**資料庫**頁面上，選取您的新資料庫 `(col_tag_database)`。

1. 選擇**檢視資料表**，然後按一下**建立資料表**。

1. 對於**名稱**，輸入 `source_data_col_lvl`。

1. 針對**資料庫**，選擇您的新資料庫 `(col_tag_database)`。

1. 針對**資料表格式**，選擇**標準 AWS Glue 資料表**。

1. 對於**位於 的資料**，選取**我帳戶中的指定路徑**。

1. 輸入 的 `col_tag_database` Amazon S3 路徑`(s3://lf-tagbased-demo-Account-ID/col_tag_database/)`。

1. 針對**資料格式**，選取 `CSV`。

1. 在 下`Upload schema`，輸入下列結構描述 JSON：

   ```
   [
                  {
                       "Name": "vendorid",
                       "Type": "string"
                       
                       
                  },
                  {
                       "Name": "lpep_pickup_datetime",
                       "Type": "string"
                       
                       
                  },
                  {
                       "Name": "lpep_dropoff_datetime",
                       "Type": "string"
                       
                       
                  },
                     {
                       "Name": "store_and_fwd_flag",
                       "Type": "string"
                       
                       
                  },
                     {
                       "Name": "ratecodeid",
                       "Type": "string"
                       
                       
                  },
                     {
                       "Name": "pulocationid",
                       "Type": "string"
                       
                       
                  },
                  {
                       "Name": "dolocationid",
                       "Type": "string"
                       
                       
                  },
                     {
                       "Name": "passenger_count",
                       "Type": "string"
                       
                       
                  },
                  {
                       "Name": "trip_distance",
                       "Type": "string"
                       
                       
                  }, 
                     {
                       "Name": "fare_amount",
                       "Type": "string"
                       
                       
                  },
                  {
                       "Name": "extra",
                       "Type": "string"
                       
                       
                  },
                     {
                       "Name": "mta_tax",
                       "Type": "string"
                       
                       
                  },
                  {
                       "Name": "tip_amount",
                       "Type": "string"
                       
                       
                  },
                     {
                       "Name": "tolls_amount",
                       "Type": "string"
                       
                       
                  },
                  {
                       "Name": "ehail_fee",
                       "Type": "string"
                       
                       
                  }, 
                  {
                       "Name": "improvement_surcharge",
                       "Type": "string"
                       
                       
                  },
                  {
                       "Name": "total_amount",
                       "Type": "string"
                       
                       
                  },
                  {
                       "Name": "payment_type",
                       "Type": "string"
                       
                       
                  }
   ]
   ```

1. 選擇 `Upload`。上傳結構描述後，資料表結構描述看起來應該如下螢幕擷取畫面。  
![\[Table schema with 18 columns showing column names and data types, all set to string.\]](http://docs.aws.amazon.com/zh_tw/lake-formation/latest/dg/images/tutorial-manage-dl-tbac2.jpg)

1. 選擇**提交**以完成資料表的建立。

1. 現在，將 `Sensitive=True` LF-Tag 與資料欄 `vendorid`和 建立關聯`fare_amount`。

   1. 在**資料表**頁面上，選取您建立的資料表`(source_data_col_lvl)`。

   1. 在**動作**功能表中，選擇**結構描述**。

   1. 選取資料欄`vendorid`，然後選擇**編輯 LF 標籤**。

   1. 針對**指派的金鑰**，選擇**敏感**。

   1.  針對**值**，選擇 **True**。

   1. 選擇**儲存**。

1. 接著，將 `Confidential=False` LF-Tag 與 建立關聯`col_tag_database`。這是必要的， `lf-data-analyst` 才能在登入`col_tag_database`時描述資料庫 Amazon Athena。

   1. 在**資料庫**頁面上，尋找並選取 `col_tag_database`。

   1. 在**動作**功能表中，選擇**編輯 LF 標籤**。

   1. 選擇**指派新的 LF 標籤**。

   1. 針對**指派的金鑰**，選擇您先前建立的 `Confidential` LF 標籤。

   1. 針對**值**，選擇 `False`。

   1. 選擇**儲存**。

## 步驟 4：授予資料表許可
<a name="tut-manage-dl-grant-table-permissions"></a>

`col_tag_database` 使用 LF 標籤`tag_database`和 授予資料分析師使用資料庫`Confidential`和資料表的許可`Sensitive`。

1. 請依照下列步驟，在與 LF-Tag `Confidential=True`(Database：tag\$1database) 相關聯的物件上將許可授予`lf-data-analyst`使用者，以取得資料表的`Describe`資料庫和`Select`許可。

   1. 以 身分登入 Lake Formation 主控台，網址為 https：//[https://console.aws.amazon.com/lakeformation/](https://console.aws.amazon.com/lakeformation/)`lf-data-engineer`。

   1. 在**許可**下，選取**資料湖許可**。

   1. 選擇 **Grant** (授予)。

   1. 在**主體**下，選取 **IAM 使用者和角色**。

   1. 針對 **IAM 使用者和角色**，選擇 `lf-data-analyst`。

   1. 在 **LF 標籤或目錄資源**下，選取**符合 LF 標籤的資源**。

   1. 選擇**新增 LF 標籤**。

   1. 針對**金鑰**，選擇 `Confidential`。

   1. 針對**值**，選擇 `True`。

   1. 針對**資料庫許可**，選取 `Describe`。

   1. 針對**資料表許可**，選擇**選取**和**描述**。

   1. 選擇 **Grant** (授予)。

1. 接著，重複這些步驟，將 LF-Tag 表達式的許可授予資料分析師`Confidential=False`。從 Amazon Athena 以 身分登入`source_data_col_lvl`時，此 **LF 標籤**用於描述 `lf-data-analyst` `col_tag_database`和 資料表。

   1. 以 身分登入 Lake Formation 主控台，網址為 https：//[https://console.aws.amazon.com/lakeformation/](https://console.aws.amazon.com/lakeformation/)`lf-data-engineer`。

   1. 在**資料庫**頁面上，選取資料庫 `col_tag_database`。

   1. 選擇**動作**和**授予**。

   1. 在**主體**下，選取 **IAM 使用者和角色**。

   1. 針對 **IAM 使用者和角色**，選擇 `lf-data-analyst`。

   1. 選取 **LF 標籤相符的資源**。

   1. 選擇**新增 LF 標籤**。

   1. 針對**金鑰**，選擇 `Confidential`。

   1.  針對 **Values**̧，選擇 `False`。

   1. 針對**資料庫許可**，選取 `Describe`。

   1. 對於**資料表許可**，請勿選取任何項目。

   1. 選擇 **Grant** (授予)。

1. 接著，重複這些步驟，將 `Confidential=False`和 的 LF-Tag 表達式許可授予資料分析師`Sensitive=True`。從 Amazon Athena 以 身分登入時，此 LF 標籤用於描述 `col_tag_database`和 資料表 `lf-data-analyst` `source_data_col_lvl`（資料欄層級）。

   1. 以 登入 Lake Formation 主控台，網址為 https：//[https://console.aws.amazon.com/lakeformation/](https://console.aws.amazon.com/lakeformation/)`lf-data-engineer`。

   1. 在資料庫頁面上，選取資料庫 `col_tag_database`。

   1. 選擇**動作**和**授予**。

   1. 在**主體**下，選取 **IAM 使用者和角色**。

   1.  針對 **IAM 使用者和角色**，選擇 `lf-data-analyst`。

   1. 選取 **LF 標籤相符的資源**。

   1. 選擇**新增 LF 標籤**。

   1. 針對**金鑰**，選擇 `Confidential`。

   1. 針對 **Values**̧，選擇 `False`。

   1. 選擇**新增 LF 標籤**。

   1. 針對**金鑰**，選擇 `Sensitive`。

   1. 針對 **Values**̧，選擇 `True`。

   1. 針對**資料庫許可**，選取 `Describe`。

   1. 針對**資料表許可**，選取 `Select`和 `Describe`。

   1. 選擇 **Grant** (授予)。

## 步驟 5：在 Amazon Athena 中執行查詢以驗證許可
<a name="tut-manage-dl-tbac-run-query"></a>

在此步驟中，使用 Amazon Athena 對兩個資料表 執行`SELECT`查詢`(source_data and source_data_col_lvl)`。使用 Amazon S3 路徑做為查詢結果位置 `(s3://lf-tagbased-demo-Account-ID/athena-results/)`。

1. 以 身分登入位於 https：//[https://console.aws.amazon.com/athena/](https://console.aws.amazon.com/athena/home) 的 Athena 主控台`lf-data-analyst`。

1. 在 Athena 查詢編輯器中，選擇左側面板`tag_database`中的 。

1. 選擇旁邊的其他功能表選項圖示 （三個垂直點）`source_data`，然後選擇**預覽資料表**。

1. 選擇 **Run query** (執行查詢)。

   查詢應該需要幾分鐘的時間來執行。查詢會顯示輸出中的所有資料欄，因為 LF 標籤在資料庫層級相關聯，且`source_data`資料表會自動`LF-tag`從資料庫繼承 `tag_database`。

1. 使用 `col_tag_database`和 執行另一個查詢`source_data_col_lvl`。

   第二個查詢會傳回標記為 `Non-Confidential`和 的兩個資料欄`Sensitive`。

1. 您也可以查看您沒有政策授予的資料欄上的 Lake Formation 標籤型存取政策行為。從資料表 選取未標記的資料欄時`source_data_col_lvl`，Athena 會傳回錯誤。例如，您可以執行下列查詢，以選擇未標記的資料欄 `geolocationid`：

   ```
   SELECT geolocationid FROM "col_tag_database"."source_data_col_lvl" limit 10;
   ```

## 步驟 6：清除 AWS 資源
<a name="tut-manage-dl-tbac-clean-up-db"></a>

若要避免不必要的費用 AWS 帳戶，您可以刪除用於本教學課程 AWS 的資源。

1. 以 身分登入 Lake Formation 主控台，`lf-data-engineer`並刪除資料庫 `tag_database`和 `col_tag_database`。

1. 接著，以 身分登入`lf-data-steward`並清除上述授予的所有 **LF 標籤許可**、**資料許可**和**資料位置許可**。 `lf-data-engineer` `lf-data-analyst.`

1. 使用您用來部署 CloudFormation 堆疊的 IAM 登入資料，以帳戶擁有者的身分登入 Amazon S3 主控台。

1. 刪除下列儲存貯體：
   + lf-tagbased-demo-accesslogs-*acct-id*
   + lf-tagbased-demo-*acct-id*

1. 在 https：//[https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/) 登入 CloudFormation 主控台，並刪除您建立的堆疊。等待堆疊狀態變更為 `DELETE_COMPLETE`。