

# 将爬网程序配置为使用 Lake Formation 凭证
<a name="crawler-lf-integ"></a>

您可以将爬网程序配置为使用 AWS Lake Formation 凭证访问 Amazon S3 数据存储库或 Data Catalog 表的证书，该表包含相同 AWS 账户 或不同 AWS 账户 中的基础 Amazon S3 位置。如果爬网程序与 Data Catalog 表位于同一账户中，则可以将现有 Data Catalog 表配置为爬网程序的目标。使用 Data Catalog 表作为爬网程序的目标时，目前只允许具有单个目录表的单个目录目标。

**注意**  
将 Data Catalog 表定义为爬网程序目标时，请确保 Data Catalog 表的基础位置是 Amazon S3 位置。使用 Lake Formation 凭证的爬网程序仅支持具有基础 Amazon S3 位置的 Data Catalog 目标。

## 爬网程序与注册的 Amazon S3 位置或 Data Catalog 表位于同一账户（账户内爬取）时所需的设置
<a name="in-account-crawling"></a>

要允许爬网程序使用 Lake Formation 凭证访问数据存储或 Data Catalog 表，您需要向 Lake Formation 注册数据位置。此外，爬网程序的 IAM 角色必须有从 Amazon S3 桶的注册目标读取数据的权限。

您可以使用 AWS 管理控制台 或 AWS Command Line Interface（AWS CLI）完成以下配置步骤。

------
#### [ AWS 管理控制台 ]

1. 在配置爬网程序以访问爬网程序源之前，向 Lake Formation 注册数据存储或 Data Catalog 的数据位置。在 Lake Formation 控制台（[https://console.aws.amazon.com/lakeformation/](https://console.aws.amazon.com/lakeformation/)）中，将 Amazon S3 位置注册为定义爬网程序的 AWS 账户 中数据湖的根位置。有关更多信息，请参阅 [Registering an Amazon S3 location](https://docs.aws.amazon.com/lake-formation/latest/dg/register-location.html)（注册 Amazon S3 位置）。

1. 向用于爬网程序运行的 IAM 角色授予 **Data location**（数据位置）权限，以便爬网程序可以从 Lake Formation 中的目标读取数据。有关更多信息，请参阅 [Granting data location permissions (same account)](https://docs.aws.amazon.com/lake-formation/latest/dg/granting-location-permissions-local.html)（授予数据位置权限（同一账户））。

1. 授予爬网程序角色访问数据库的权限（`Create`），该数据库被指定为输出数据库。有关更多信息，请参阅 [Granting database permissions using the Lake Formation console and the named resource method](https://docs.aws.amazon.com/lake-formation/latest/dg/granting-database-permissions.html)（使用 Lake Formation 控制台和指定的资源方法授予数据库权限）。

1. 在 IAM 控制台（[https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/)）中，为爬网程序创建 IAM 角色。将 `lakeformation:GetDataAccess` 策略添加到该角色。

1. 在 AWS Glue 控制台（[https://console.aws.amazon.com/glue/](https://console.aws.amazon.com/glue/)）中配置爬网程序时，选择选项 **Use Lake Formation credentials for crawling Amazon S3 data source**（使用 Lake Formation 凭证爬取 Amazon S3 数据来源）。
**注意**  
accountId 字段对于账户内爬取是可选的。

------
#### [ AWS CLI ]

```
aws glue --profile demo create-crawler --debug --cli-input-json '{
    "Name": "prod-test-crawler",
    "Role": "arn:aws:iam::111122223333:role/service-role/AWSGlueServiceRole-prod-test-run-role",
    "DatabaseName": "prod-run-db",
    "Description": "",
    "Targets": {
    "S3Targets":[
                {
                 "Path": "s3://amzn-s3-demo-bucket"
                }
                ]
                },
   "SchemaChangePolicy": {
      "UpdateBehavior": "LOG",
      "DeleteBehavior": "LOG"
  },
  "RecrawlPolicy": {
    "RecrawlBehavior": "CRAWL_EVERYTHING"
  },
  "LineageConfiguration": {
    "CrawlerLineageSettings": "DISABLE"
  },
  "LakeFormationConfiguration": {
    "UseLakeFormationCredentials": true,
    "AccountId": "111122223333"
  },
  "Configuration": {
           "Version": 1.0,
           "CrawlerOutput": {
             "Partitions": { "AddOrUpdateBehavior": "InheritFromTable" },
             "Tables": {"AddOrUpdateBehavior": "MergeNewColumns" }
           },
           "Grouping": { "TableGroupingPolicy": "CombineCompatibleSchemas" }
         },
  "CrawlerSecurityConfiguration": "",
  "Tags": {
    "KeyName": ""
  }
}'
```

------

# 爬网程序与注册的 Amazon S3 位置位于不同账户（跨账户爬取）时所需的设置
<a name="cross-account-crawling"></a>

要允许爬网程序使用 Lake Formation 凭证访问不同账户中的数据存储，您必须先向 Lake Formation 注册 Amazon S3 数据位置。然后，通过执行以下步骤向爬网程序的账户授予数据位置权限。

您可以使用 AWS 管理控制台 或 AWS CLI 完成以下步骤。

------
#### [ AWS 管理控制台 ]

1. 在注册 Amazon S3 位置的账户（账户 B）中：

   1. 向 Lake Formation 注册 Amazon S3 路径。有关更多信息，请参阅[注册 Amazon S3 位置](https://docs.aws.amazon.com/lake-formation/latest/dg/register-location.html)。

   1.  向将运行爬网程序的账户（账户 A）授予 **Data location**（数据位置）权限。有关更多信息，请参阅[授予数据位置权限](https://docs.aws.amazon.com/lake-formation/latest/dg/granting-location-permissions-local.html)。

   1. 在 Lake Formation 中创建一个空数据库，将基础位置作为目标 Amazon S3 位置。有关更多信息，请参阅[创建数据库](https://docs.aws.amazon.com/lake-formation/latest/dg/creating-database.html)。

   1. 授予账户 A（将运行爬网程序的账户）访问您在上一步中创建的数据库的权限。有关更多信息，请参阅[授予数据库权限](https://docs.aws.amazon.com/lake-formation/latest/dg/granting-database-permissions.html)。

1. 在创建并将运行爬网程序的账户（账户 A）中：

   1.  使用 AWS RAM 控制台，接受从外部账户（账户 B）共享的数据库。有关更多信息，请参阅 [Accepting a resource share invitation from AWS Resource Access Manager](https://docs.aws.amazon.com/lake-formation/latest/dg/accepting-ram-invite.html)（接受来自 RAMlong 的资源共享邀请）。

   1.  为爬网程序创建 IAM 角色。将 `lakeformation:GetDataAccess` 策略添加到该角色。

   1.  在 Lake Formation 控制台（[https://console.aws.amazon.com/lakeformation/](https://console.aws.amazon.com/lakeformation/)）中，授予用于爬网程序运行的 IAM 角色目标 Amazon S3 位置上的 **Data location**（数据位置）权限，以使爬网程序能够从 Lake Formation 中的目标读取数据。有关更多信息，请参阅[授予数据位置权限](https://docs.aws.amazon.com/lake-formation/latest/dg/granting-location-permissions-local.html)。

   1.  在共享数据库上创建资源链接。有关更多信息，请参阅[创建资源链接](https://docs.aws.amazon.com/lake-formation/latest/dg/create-resource-link-database.html)。

   1.  授予爬网程序角色对共享数据库和（`Describe`）资源链接的访问权限（`Create`）。资源链接在爬网程序的输出中指定。

   1.  在 AWS Glue 控制台（[https://console.aws.amazon.com/glue/](https://console.aws.amazon.com/glue/)）中配置爬网程序时，选择选项 **Use Lake Formation credentials for crawling Amazon S3 data source**（使用 Lake Formation 凭证爬取 Amazon S3 数据来源）。

      对于跨账户爬取，请指定向 Lake Formation 注册的目标 Amazon S3 位置的 AWS 账户 ID。对于账户内爬取，accountId 字段是可选的。  
![\[IAM role selection and Lake Formation configuration options for AWS Glue 爬网程序 security settings.\]](http://docs.aws.amazon.com/zh_cn/glue/latest/dg/images/cross-account-crawler.png)

------
#### [ AWS CLI ]

```
aws glue --profile demo create-crawler --debug --cli-input-json '{
    "Name": "prod-test-crawler",
    "Role": "arn:aws:iam::111122223333:role/service-role/AWSGlueServiceRole-prod-test-run-role",
    "DatabaseName": "prod-run-db",
    "Description": "",
    "Targets": {
    "S3Targets":[
                {
                 "Path": "s3://amzn-s3-demo-bucket"
                }
                ]
                },
   "SchemaChangePolicy": {
      "UpdateBehavior": "LOG",
      "DeleteBehavior": "LOG"
  },
  "RecrawlPolicy": {
    "RecrawlBehavior": "CRAWL_EVERYTHING"
  },
  "LineageConfiguration": {
    "CrawlerLineageSettings": "DISABLE"
  },
  "LakeFormationConfiguration": {
    "UseLakeFormationCredentials": true,
    "AccountId": "111111111111"
  },
  "Configuration": {
           "Version": 1.0,
           "CrawlerOutput": {
             "Partitions": { "AddOrUpdateBehavior": "InheritFromTable" },
             "Tables": {"AddOrUpdateBehavior": "MergeNewColumns" }
           },
           "Grouping": { "TableGroupingPolicy": "CombineCompatibleSchemas" }
         },
  "CrawlerSecurityConfiguration": "",
  "Tags": {
    "KeyName": ""
  }
}'
```

------

**注意**  
只有 Amazon S3 和 Data Catalog 目标支持使用 Lake Formation 凭证的爬网程序。
对于使用 Lake Formation 凭证售卖的目标，基础 Amazon S3 位置必须属于同一个桶。例如，只要所有目标位置都在同一个存储桶 (amzn-s3-demo-bucket1) 下，客户就可以使用多个目标（s3://amzn-s3-demo-bucket1/folder1、s3://amzn-s3-demo-bucket1/folder2）。不允许指定不同的存储桶（s3://amzn-s3-demo-bucket1/folder1、s3://amzn-s3-demo-bucket2/folder2）。
目前，对于 Data Catalog 目标爬网程序，只允许具有单个目录表的单个目录目标。