

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

# 爬蟲程式和註冊的 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 路徑。如需詳細資訊，請參閱 [Registering Amazon S3 location](https://docs.aws.amazon.com/lake-formation/latest/dg/register-location.html) (註冊 Amazon S3 位置)。

   1.  將 **Data location** (資料位置) 許可授與爬蟲程式執行所在的帳戶 (帳戶 A)。如需詳細資訊，請參閱 [Grant data location permissions](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) 共用的資料庫。如需詳細資訊，請參閱[接受來自 的資源共用邀請 AWS Resource Access Manager](https://docs.aws.amazon.com/lake-formation/latest/dg/accepting-ram-invite.html)。

   1.  建立爬蟲程式的 IAM 角色。將 `lakeformation:GetDataAccess` 政策新增至該角色。

   1.  在 Lake Formation 主控台 ([https://console.aws.amazon.com/lakeformation/](https://console.aws.amazon.com/lakeformation/)) 中，將目標 Amazon S3 位置的 **Data location** (資料位置) 許可授與用於爬蟲程式執行的 IAM 角色，以便爬蟲程式可以從 Lake Formation 中的目的地讀取資料。如需詳細資訊，請參閱 [Granting data location permissions](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/)：//) 中，在設定爬蟲程式時，選取**使用 Lake Formation 登入資料來爬取 Amazon S3 資料來源**的選項。

      對於跨帳戶爬取，請指定向 Lake Formation 註冊目標 Amazon S3 位置的 AWS 帳戶 ID。對於帳戶內網路爬取，accountId 為選填欄位。  
![Lake Formation 組態區段，其中帳戶 ID 欄位顯示 111111111111。](http://docs.aws.amazon.com/zh_tw/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 和資料目錄目標才支援使用 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)。
目前對於資料目錄目標爬蟲程式而言，僅允許具有單一目錄資料表的單一目錄目標。