

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

# 使用特徵群組啟用 Lake Formation
<a name="feature-store-lf-governance"></a>

當您在 Amazon SageMaker Feature Store 中的特徵群組上啟用 AWS Lake Formation 時，您可以對離線存放區中的特徵資料強制執行資料欄層級、資料列層級和儲存格層級的安全性。您可以使用 Lake Formation 授予和撤銷許可模型來控制哪些使用者和角色可以存取特定功能和記錄 AWS Glue ，而不是透過 Amazon S3 和資源上的個別 IAM 政策來管理存取權。如需 Lake Formation 的詳細資訊，請參閱 [AWS Lake Formation 開發人員指南](https://docs.aws.amazon.com/lake-formation/latest/dg/what-is-lake-formation.html)。

**重要**  
Lake Formation 存取控制僅適用於離線存放區。離線存放區由 Amazon S3 提供支援，並在 AWS Glue Data Catalog Lake Formation 管理的 中註冊。線上商店存取會繼續透過 IAM 政策控制。若要設定 Lake Formation，您可以使用 SageMaker AI Python SDK () 中的 `FeatureGroupManager`和 `LakeFormationConfig`類別`sagemaker.mlops.feature_store`。Lake Formation 支援[混合存取模式](https://docs.aws.amazon.com/lake-formation/latest/dg/hybrid-access-mode.html)，允許 IAM 政策和 Lake Formation 許可在逐步遷移期間共存。

## 先決條件
<a name="feature-store-lf-prerequisites"></a>

啟用 Lake Formation 之前，請確認您有下列項目：
+ 已設定離線存放區的 SageMaker AI 功能群組，或者您可以在設定過程中使用離線存放區建立新的功能群組。Lake Formation 需要離線存放區，因為它會透過離線存放區建立 AWS Glue 的資料目錄資料表來管理存取。
+ 具有適當許可的 IAM 執行角色。下列範例顯示所需的最低 IAM 政策。將預留位置值取代為您自己的值。

  ```
  {
      "Version": "2012-10-17", 		 	 	 
      "Statement": [
          {
              "Sid": "SageMakerFeatureGroupOperations",
              "Effect": "Allow",
              "Action": [
                  "sagemaker:CreateFeatureGroup",
                  "sagemaker:DescribeFeatureGroup"
              ],
              "Resource": "arn:aws:sagemaker:*:*:feature-group/*"
          },
          {
              "Sid": "LakeFormation",
              "Effect": "Allow",
              "Action": [
                  "lakeformation:RegisterResource",
                  "lakeformation:GrantPermissions",
                  "lakeformation:RevokePermissions",
                  "lakeformation:ListPermissions"
              ],
              "Resource": "*"
          },
          {
              "Sid": "GlueCatalogReadAccess",
              "Effect": "Allow",
              "Action": [
                  "glue:GetTable",
                  "glue:GetDatabase"
              ],
              "Resource": [
                  "arn:aws:glue:*:*:catalog",
                  "arn:aws:glue:*:*:database/sagemaker_featurestore",
                  "arn:aws:glue:*:*:table/sagemaker_featurestore/*"
              ]
          },
          {
              "Sid": "GlueCatalogTableCreate",
              "Effect": "Allow",
              "Action": [
                  "glue:CreateTable"
              ],
              "Resource": [
                  "arn:aws:glue:*:*:catalog",
                  "arn:aws:glue:*:*:database/sagemaker_featurestore",
                  "arn:aws:glue:*:*:table/sagemaker_featurestore/*"
              ]
          },
          {
              "Sid": "PassOfflineStoreRole",
              "Effect": "Allow",
              "Action": "iam:PassRole",
              "Resource": "arn:aws:iam::<account-id>:role/<offline-store-role-name>"
          },
          {
              "Sid": "S3FeatureStoreStorage",
              "Effect": "Allow",
              "Action": [
                  "s3:GetBucketAcl",
                  "s3:GetBucketLocation",
                  "s3:ListBucket"
              ],
              "Resource": "arn:aws:s3:::<offline-store-bucket-name>"
          },
          {
              "Sid": "LakeFormationRegistrationRole",
              "Effect": "Allow",
              "Action": [
                  "iam:GetRole",
                  "iam:GetRolePolicy"
              ],
              "Resource": "arn:aws:iam::<account-id>:role/<registration-role-name>"
          }
      ]
  }
  ```
**注意**  
`LakeFormationRegistrationRole` 陳述式授予許可，以讀取用於向 Lake Formation 註冊 Amazon S3 位置的角色。如果您使用 Lake Formation 服務連結角色 (`use_service_linked_role=True`)，請將資源設定為 `arn:aws:iam::<account-id>:role/aws-service-role/lakeformation.amazonaws.com/AWSServiceRoleForLakeFormationDataAccess`。如果您提供自己的註冊角色，請將其設定為該角色的 ARN。
**注意**  
如果`sagemaker_featurestore`資料庫已停用混合存取模式，呼叫者也必須擁有資料庫的 Lake Formation `CREATE_TABLE`許可。Lake Formation 管理員可以透過 Lake Formation 主控台或 API 授予此許可。
+ 您帳戶中設定的 Lake Formation 管理員。您必須指定至少一個 IAM 使用者或角色做為 Lake Formation 管理員。如需設定說明，請參閱 [Lake Formation 文件中的 Lake Formation 入門](https://docs.aws.amazon.com/lake-formation/latest/dg/getting-started-setup.html)。
+ SageMaker AI Python SDK 3.8.0 版或更新版本。安裝或升級`sagemaker`套件： `pip install --upgrade sagemaker>=3.8.0`
+ 跨帳戶組態 （如適用）。如果特徵群組的離線存放區 Amazon S3 儲存貯體位於不同的 AWS 帳戶，則需要額外的 Lake Formation 跨帳戶共用組態，包括 AWS Glue 資源政策、 AWS RAM 共用接受度、資源連結和取用者資料庫設定。如需詳細資訊，請參閱 Lake Formation 文件中的[跨帳戶共用資料](https://docs.aws.amazon.com/lake-formation/latest/dg/sharing-catalog-resources.html)。

## 重要概念
<a name="feature-store-lf-concepts"></a>

下列概念對於了解 Lake Formation 如何與 Feature Store 搭配使用非常重要。

### Lake Formation 許可模型與僅限 IAM 模型的比較
<a name="feature-store-lf-permission-model"></a>

根據預設，僅透過 IAM 政策控制對 AWS Glue Data Catalog 資料表的存取，包括特徵商店建立的資料表。當您啟用 Lake Formation 時，存取需要 IAM 許可和 Lake Formation 許可。Lake Formation 使用授予和撤銷模型，您在特定資料庫、資料表或資料欄上明確授予許可給 IAM 主體`DESCRIBE`，例如 `SELECT`或 。

### 混合存取模式
<a name="feature-store-lf-hybrid-access"></a>

當您啟用 Lake Formation 時，您可以選擇要使用混合存取模式還是僅限 Lake Formation 模式：
+ **混合存取模式** (`hybrid_access_mode_enabled=True`)：評估 IAM 政策和 Lake Formation 許可。可透過現有 IAM 政策存取的委託人仍可繼續存取，您也可以透過 Lake Formation 授予精細存取。這對於逐步遷移很有用。
+ **僅限 Lake Formation 模式** (`hybrid_access_mode_enabled=False`)：只會評估 Lake Formation 許可。 AWS Glue 資料表的現有 IAM 型存取已撤銷。這可提供最強大的存取控制，但可能會中斷現有的工作負載。

**警告**  
當您設定 時`hybrid_access_mode_enabled=False`，軟體開發套件會撤銷 AWS Glue 資料表上的`IAMAllowedPrincipal`授予。僅透過 IAM 許可存取此資料表的任何現有任務、筆記本或管道會立即失去存取權。在停用混合存取模式之前，請確認您已將必要的 Lake Formation 許可授予所有需要存取的主體。

**注意**  
當資料表格式為 Iceberg 時，您必須停用跨帳戶存取的混合存取模式。

### S3 拒絕政策
<a name="feature-store-lf-s3-deny-policy"></a>

即使在 AWS Glue Data Catalog 資料表上啟用 Lake Formation 之後，具有直接 Amazon S3 存取權 （透過 IAM 政策） 的使用者也可以直接讀取基礎 Amazon S3 物件來略過 Lake Formation。若要消除此差距，請套用 Amazon S3 儲存貯體政策，拒絕直接存取 Lake Formation 服務角色和特徵商店執行角色以外的所有主體的離線存放區字首。

**重要**  
軟體開發套件不會自動套用 Amazon S3 拒絕政策。啟用 Lake Formation 之後，開發套件會將建議的儲存貯體政策記錄為警告訊息。檢閱此政策並將其套用至 Amazon S3 儲存貯體，以強制執行end-to-end存取控制。

## 組態參考
<a name="feature-store-lf-configuration"></a>

使用 `LakeFormationConfig`類別來設定 Lake Formation。您可以在建立新功能群組`FeatureGroupManager.create()`時將此組態傳遞至 ，或`enable_lake_formation()`針對現有功能群組直接使用個別參數搭配 。


| 參數 | Type | 預設 | 必要 | 說明 | 
| --- | --- | --- | --- | --- | 
| enabled | bool | False | 否 | 設定為 True 以在特徵群組的離線存放區上啟用 Lake Formation。 | 
| use\_service\_linked\_role | bool | True | 否 | 是否使用 Lake Formation 服務連結角色註冊 S3 位置。False 如果您使用自訂註冊角色，請將 設定為 。您無法在跨帳戶存取或使用第三方查詢引擎 （例如 Apache Spark) 時使用服務連結角色。對於第三方引擎，您必須透過 提供自己的註冊角色registration\_role\_arn。 | 
| registration\_role\_arn | str | 無 | 有條件 | 自訂 IAM 角色的 ARN，用於向 Lake Formation 註冊離線存放區 S3 位置。當 use\_service\_linked\_role為 時為必要False。 | 
| hybrid\_access\_mode\_enabled | bool | — | 是 | 是否要IAMAllowedPrincipal撤銷 AWS Glue 資料表。 False= 僅限 Lake Formation 的許可。 True = 混合模式 (IAM 和 Lake Formation)。您必須明確選擇。 | 
| acknowledge\_risk | bool | — | 是 | 必須為 True才能繼續。這是安全確認。在執行任何操作RuntimeError之前，將 設定為 會False引發 。 | 

### 了解 acknowledge\_risk
<a name="feature-store-lf-acknowledge-risk"></a>

`acknowledge_risk` 參數是安全閘道。將其設定為 `True`，即表示您確認下列事項：
+ 如果 `hybrid_access_mode_enabled=False`：查詢此 AWS Glue 資料表的現有 IAM 型任務、筆記本和管道會立即失去存取權。您必須在啟用之前或之後不久將 Lake Formation 許可授予這些委託人。
+ 如果 Lake Formation 許可未正確設定：在更正許可之前，所有使用者都可能無法存取特徵群組中的資料。
+ 操作會修改 AWS Glue Data Catalog 許可：這些變更會影響 AWS Glue 資料表的所有取用者，而不只是特徵商店使用者。從此表格讀取的任何 Athena 查詢、Spark 任務或其他 服務都會受到影響。

## 運作方式
<a name="feature-store-lf-how-it-works"></a>

當您啟用 Lake Formation 時，軟體開發套件會執行三階段設定：

1. **註冊 S3 位置。**SDK 會使用 Lake Formation 服務連結角色或您指定的自訂註冊角色，向 Lake Formation 註冊離線存放區的 S3 路徑。如果您使用第三方查詢引擎 （例如 Apache Spark)，您必須提供自己的註冊角色，因為服務連結角色不支援第三方引擎存取。

1. **授予 Lake Formation 許可。**SDK 會將 AWS Glue Data Catalog 資料表上的 `SELECT`、`DESCRIBE`、、 `INSERT` `DELETE`和 `ALTER`許可授予特徵群組的執行角色。

1. **選擇性地撤銷 IAMAllowedPrincipal。**如果您設定 `hybrid_access_mode_enabled=False`，軟體開發套件會撤銷 AWS Glue 資料表上的`IAMAllowedPrincipal`授予。

在所有階段完成後，開發套件會記錄建議的 S3 儲存貯體拒絕政策。檢閱並套用此政策，以防止繞過 Lake Formation 存取控制的直接 Amazon S3 存取。

**注意**  
如果設定中途失敗 （例如，在註冊 S3 位置之後，但在授予許可之前），您可以安全地重新執行`enable_lake_formation()`方法。開發套件是等冪的，並略過已成功完成的步驟。

## 使用範例
<a name="feature-store-lf-examples"></a>

### 使用 Lake Formation 存取控制建立新的功能群組
<a name="feature-store-lf-example-new-fg"></a>

下列範例會使用離線存放區建立新的功能群組，並在單一操作中啟用 Lake Formation 存取控制。

```
from sagemaker.core.helper.session_helper import Session
from sagemaker.core.shapes import (
    FeatureDefinition,
    OfflineStoreConfig,
    OnlineStoreConfig,
    S3StorageConfig,
)
from sagemaker.mlops.feature_store import FeatureGroupManager, LakeFormationConfig

session = Session()
region = session.boto_region_name

role_arn = "arn:aws:iam::<account-id>:role/<execution-role-name>"
registration_role_arn = "arn:aws:iam::<account-id>:role/<registration-role-name>"
fg_name = "my-feature-group"
bucket = session.default_bucket()
offline_s3_uri = f"s3://{bucket}/feature-store/{fg_name}"

feature_definitions = [
    FeatureDefinition(feature_name="customer_id", feature_type="String"),
    FeatureDefinition(feature_name="event_time", feature_type="String"),
    FeatureDefinition(feature_name="age", feature_type="Integral"),
    FeatureDefinition(feature_name="total_purchases", feature_type="Integral"),
    FeatureDefinition(feature_name="avg_order_value", feature_type="Fractional"),
]

fg = FeatureGroupManager.create(
    feature_group_name=fg_name,
    record_identifier_feature_name="customer_id",
    event_time_feature_name="event_time",
    feature_definitions=feature_definitions,
    online_store_config=OnlineStoreConfig(enable_online_store=False),
    offline_store_config=OfflineStoreConfig(
        s3_storage_config=S3StorageConfig(s3_uri=offline_s3_uri),
        table_format="Iceberg",
    ),
    role_arn=role_arn,
    description="A feature group with Lake Formation-managed offline store",
    # Example configuration — adjust parameters for your use case.
    lake_formation_config=LakeFormationConfig(
        enabled=True,
        use_service_linked_role=False,
        registration_role_arn=registration_role_arn,
        # Set to True to keep existing IAM-based access (hybrid mode).
        hybrid_access_mode_enabled=False,
        acknowledge_risk=True,
    ),
    region=region,
)
```

### 在現有功能群組上啟用 Lake Formation
<a name="feature-store-lf-example-existing-fg"></a>

下列範例會在已存在的特徵群組上啟用 Lake Formation。

```
from sagemaker.mlops.feature_store import FeatureGroupManager

fg = FeatureGroupManager.get(feature_group_name="my-existing-feature-group")
result = fg.enable_lake_formation(
    use_service_linked_role=False,
    registration_role_arn="arn:aws:iam::<account-id>:role/<registration-role-name>",
    hybrid_access_mode_enabled=False,
    acknowledge_risk=True,
)
```

## 搭配 SageMaker AI 任務使用已啟用 Lake Formation 的功能群組
<a name="feature-store-lf-sagemaker-jobs"></a>

您可以從 SageMaker AI 訓練和處理任務存取已啟用 Lake Formation 的功能群組。若要讀取已啟用 Lake Formation 的功能資料，您可以透過 Athena 查詢資料，或使用 Lake Formation [GetTemporaryGlueTableCredentials](https://docs.aws.amazon.com/lake-formation/latest/APIReference/API_GetTemporaryGlueTableCredentials.html) API 來提供範圍為 AWS Glue 資料表的臨時 Amazon S3 憑證。如需為運算角色設定 Lake Formation 許可的詳細資訊，請參閱 [Lake Formation 文件中的 Lake Formation 許可參考](https://docs.aws.amazon.com/lake-formation/latest/dg/lf-permissions-reference.html)。

## 跨帳戶存取權
<a name="feature-store-lf-cross-account"></a>

Lake Formation 支援跨 共用特徵群組資料 AWS 帳戶。當生產者帳戶在特徵群組上啟用 Lake Formation 時，可以使用具名資源方法或 Lake Formation 標籤型存取控制 (LF-TBAC) 授予消費者帳戶的跨帳戶存取權。

跨帳戶共用需要下列設定：

1. 生產者帳戶會將 AWS Glue Data Catalog 資料表上的跨帳戶許可授予取用者帳戶、 AWS 組織或組織單位。

1. 如果帳戶不在相同的 AWS 組織，則取用者帳戶接受 AWS RAM 資源共享邀請。如需詳細資訊，請參閱[接受資源共享邀請](https://docs.aws.amazon.com/lake-formation/latest/dg/accepting-ram-invite.html)。

1. 取用者帳戶會建立本機資料庫中共用資料表的資源連結。Athena 和 Amazon Redshift Spectrum 等服務需要資源連結，才能查詢共用資源。如需詳細資訊，請參閱[關於資源連結](https://docs.aws.amazon.com/lake-formation/latest/dg/resource-links-about.html)。

1. 消費者帳戶中的 Lake Formation 管理員會將資源連結和基礎共用資料表的許可授予需要存取的 IAM 主體。

**重要**  
如果您使用第三方查詢引擎 （例如 Apache Spark)，您必須啟用共用資料表的完整資料表存取。第三方引擎需要完整的資料表憑證販賣，因為它們不支援 Lake Formation 透過工作階段標籤進行資料欄層級或儲存格層級篩選。您還必須使用自訂註冊角色註冊 Amazon S3 位置，而不是服務連結角色。如需詳細資訊，請參閱[第三方引擎的完整資料表存取](https://docs.aws.amazon.com/lake-formation/latest/dg/full-table-credential-vending.html)。

如需跨帳戶共用的先決條件、step-by-step說明和最佳實務，請參閱 AWS Lake Formation 開發人員指南中的下列主題：
+ [跨帳戶存取](https://docs.aws.amazon.com/lake-formation/latest/dg/cross-account-permissions.html) — 跨帳戶共用方法和 AWS RAM 整合的概觀。
+ [跨帳戶先決條件](https://docs.aws.amazon.com/lake-formation/latest/dg/cross-account-prereqs.html) — 生產者和消費者帳戶的必要組態。
+ [共用資料目錄資料表和資料庫](https://docs.aws.amazon.com/lake-formation/latest/dg/cross-account-data-share-steps.html) — Step-by-step說明。
+ [標籤型存取控制](https://docs.aws.amazon.com/lake-formation/latest/dg/tag-based-access-control.html) — 建議用於跨多個帳戶大規模管理許可。

## 設定後授予精細存取
<a name="feature-store-lf-fine-grained-access"></a>

啟用 Lake Formation 之後，Lake Formation 管理員可以使用 Lake Formation 主控台、API 或 CLI 將精細許可授予其他 IAM 主體。Lake Formation 支援資料欄層級、資料列層級和儲存格層級存取控制。

如需授予和管理 Lake Formation 許可的指示，請參閱 AWS Lake Formation 開發人員指南中的下列主題：
+ [授予和撤銷 Data Catalog 許可](https://docs.aws.amazon.com/lake-formation/latest/dg/granting-catalog-permissions.html)
+ [Lake Formation 中的資料篩選條件](https://docs.aws.amazon.com/lake-formation/latest/dg/data-filters-about.html)，用於資料列層級和儲存格層級篩選
+ [Lake Formation 許可參考](https://docs.aws.amazon.com/lake-formation/latest/dg/lf-permissions-reference.html)