

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

# 使用功能组启用 Lake Formation
<a name="feature-store-lf-governance"></a>

当您在亚马逊 SageMaker 功能商店的功能组上启用 L AWS ake Formation 时，您可以对离线商店中的功能数据实施列级、行级和单元级安全。您可以使用 Lake Formation 授予和撤销权限模型来控制哪些用户和角色可以访问特定功能和记录，而不是通过单个 IAM 策略对 Amazon S3 和 AWS Glue 资源进行访问。有关 Lake Formation 的更多信息，请参阅 [AWS Lake Formation 开发者指南](https://docs.aws.amazon.com/lake-formation/latest/dg/what-is-lake-formation.html)。

**重要**  
Lake Formation 访问控制仅适用于线下商店。线下商店由亚马逊 S3 提供支持，并在由 Lake Form AWS Glue Data Catalog ation 管理的中注册。继续通过 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 注册亚马逊 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 F `CREATE_TABLE` ormation 权限。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`
+ Cross-account 配置（如果适用）。如果功能组的离线商店 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-only 模型的比较
<a name="feature-store-lf-permission-model"></a>

默认情况下，仅通过 IAM 策略控制对 AWS Glue Data Catalog 表（包括功能商店创建的表）的访问。启用 Lake Formation 时，访问需要 IAM 权限和 Lake Formation 权限。Lake Formation 使用授予和撤销模式，在这种模式下，您可以明确向 IAM 委托人授予特定数据库、表或列的权限，例如或。`SELECT` `DESCRIBE`

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

启用 Lake Formation 时，你可以选择使用混合访问模式还是湖泊 Formation-only 模式：
+ **混合访问模式** (`hybrid_access_mode_enabled=True`)：对 IAM 策略和 Lake Formation 权限进行评估。通过现有 IAM 策略进行访问的委托人继续拥有访问权限，您还可以通过 Lake Formation 授予细粒度的访问权限。这对于逐步迁移很有用。
+ **湖泊 Formation-only 模式** (`hybrid_access_mode_enabled=False`)：仅评估 Lake Formation 权限。对该 AWS Glue 表的现有 IAM-based 访问权限已被撤销。这提供了最强大的访问控制，但可能会破坏现有的工作负载。

**警告**  
设置后`hybrid_access_mode_enabled=False`，SDK 会撤消对 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 策略）的用户也可以通过直接读取底层的亚马逊 S3 对象来绕过 Lake Formation。要缩小这一差距，请应用 Amazon S3 存储桶策略，拒绝除了 Lake Formation 服务角色和功能商店执行角色之外的所有委托人直接访问离线商店前缀。

**重要**  
软件开发工具包不会自动应用 Amazon S3 拒绝策略。启用 Lake Formation 后，SDK 会将推荐的存储桶策略记录为警告消息。查看此政策并将其应用于您的 Amazon S3 存储桶，以实施端到端的访问控制。

## 配置参考
<a name="feature-store-lf-configuration"></a>

使用该`LakeFormationConfig`类来配置 Lake Formation。`FeatureGroupManager.create()`在创建新功能组时，您可以将此配置传递给，或者直接将各个参数与一起`enable_lake_formation()`用于现有功能组。


| 参数 | Type | 默认 | 必需 | 说明 | 
| --- | --- | --- | --- | --- | 
| enabled | 布尔 | False | 否 | 设置为可在功能组True的离线商店中激活 Lake Formation。 | 
| use\_service\_linked\_role | 布尔 | True | 否 | 是否使用 Lake Formation 服务相关角色来注册 S3 位置。False如果您使用自定义注册角色，则设置为。您不能使用服务相关角色进行跨账户访问或使用第三方查询引擎（例如 Apache Spark）。对于第三方引擎，您必须通过提供自己的注册角色registration\_role\_arn。 | 
| registration\_role\_arn | str | 无 | 有条件 | 用于向 Lake Formation 注册离线商店 S3 位置的自定义 IAM 角色的 ARN。何时use\_service\_linked\_role为必填项False。 | 
| hybrid\_access\_mode\_enabled | 布尔 | — | 是 | 是否IAMAllowedPrincipal从 AWS Glue 表中撤销。 False= 湖泊 Formation-only 权限。 True= 混合模式（包括 IAM 和 Lake Formation）。您必须明确选择。 | 
| acknowledge\_risk | 布尔 | — | 是 | 必须True继续。这是安全确认。设置为在执行任何操作RuntimeError之前False会引发 a。 | 

### 了解确认\_风险
<a name="feature-store-lf-acknowledge-risk"></a>

`acknowledge_risk`参数是安全门。将其设置为`True`，即表示您确认以下内容：
+ 如果`hybrid_access_mode_enabled=False`：查询此 AWS Glue 表的现有 IAM-based 作业、笔记本和管道会立即失去访问权限。您必须在启用之前或之后不久向这些委托人授予 Lake Formation 权限。
+ 如果 Lake Formation 权限配置不正确：在更正权限之前，所有用户都可能无法访问功能组中的数据。
+ 该操作会修改 AWS Glue 数据目录权限：这些更改会影响 AWS Glue 表的所有使用者，而不仅仅是 Feature Store 用户。从该表读取的任何 Athena 查询、Spark 任务或其他服务都会受到影响。

## 工作原理
<a name="feature-store-lf-how-it-works"></a>

当你启用 Lake Formation 时，SDK 会执行三个阶段的设置：

1. **注册 S3 位置。**SDK 使用与 Lake Formation 服务相关的角色或您指定的自定义注册角色向 Lake Formation 注册离线商店的 S3 路径。如果您使用第三方查询引擎（例如 Apache Spark），则必须提供自己的注册角色，因为服务相关角色不支持第三方引擎访问权限。

1. **授予 Lake Formation 权限。**SDK 向功能组的执行角色授予`SELECT``INSERT``DELETE``DESCRIBE`、、、和对 AWS Glue 数据目录表的`ALTER`权限。

1. **（可选）撤销 IAMAllowedPrincipal。**如果您进行了设置`hybrid_access_mode_enabled=False`，则 SDK 会撤消对 AWS Glue 表的`IAMAllowedPrincipal`授权。

所有阶段完成后，SDK 会记录推荐的 S3 存储桶拒绝策略。查看并应用此政策，防止绕过 Lake Formation 访问控制直接访问 Amazon S3。

**注意**  
如果安装在中途失败（例如，在注册 S3 位置之后但在授予权限之前），则可以放心地重新运行该方法。`enable_lake_formation()`SDK 是等效的，可以跳过已经成功完成的步骤。

## 用法示例
<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,
)
```

## 将 Lake Formation-enabled 功能组与 SageMaker AI 作业配合使用
<a name="feature-store-lf-sagemaker-jobs"></a>

您可以通过 SageMaker AI 训练和处理作业访问 Lake Formation-enabled 功能组。要读取 Lake 要 Formation-enabled 素数据，您可以通过 Athena 查询数据，也可以使用 Lake Formati [GetTemporaryGlueTableCredentials](https://docs.aws.amazon.com/lake-formation/latest/APIReference/API_GetTemporaryGlueTableCredentials.html)on API 向该表提供限定范围的临时 Amazon S3 证书。 AWS Glue 有关为计算角色配置 Lake Formation 权限的更多信息，请参阅 [Lake Formation 文档中的 Lake Formation 权限参考](https://docs.aws.amazon.com/lake-formation/latest/dg/lf-permissions-reference.html)。

## Cross-account 访问
<a name="feature-store-lf-cross-account"></a>

Lake Formation 支持跨平台共享要素组数据 AWS 账户。当创建者账户在功能组上启用 Lake Formation 时，它可以使用命名的资源方法或基于 Lake Formation 标签的访问控制 () LF-TBAC 向使用者账户授予跨账户访问权限。

Cross-account 共享需要进行以下设置：

1. 生产者账户向消费者账户、组织或 AWS 组织单位授予对 AWS Glue 数据目录表的跨账户权限。

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），则必须为共享表启用全表访问权限。 Third-party 引擎需要自动售卖全桌凭证，因为它们不支持 Lake Formation 的列级或单元级通过会话标签进行筛选。您还必须使用自定义注册角色而不是服务相关角色注册 Amazon S3 地点。有关更多信息，请参阅[第三方引擎的完整表访问权限](https://docs.aws.amazon.com/lake-formation/latest/dg/full-table-credential-vending.html)。

有关跨账户共享的先决条件、分步说明和最佳实践，请参阅 AWS Lake Formation 开发者指南中的以下主题：
+ [Cross-account 访问权限](https://docs.aws.amazon.com/lake-formation/latest/dg/cross-account-permissions.html) — 跨账户共享方法和 AWS RAM 集成概述。
+ [Cross-account 先决条件](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)-指定资源方法和 LF-TBAC 方法的 Step-by-step 说明。
+ [Tag-based 访问控制](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 开发者指南中的以下主题：
+ [授予和撤消数据目录权限](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)