

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

# Glue AWS 的資源型政策範例
<a name="security_iam_resource-based-policy-examples"></a>

本節包含以資源為基礎的範例政策，包括授予跨帳户存取權的政策。

這些範例使用 AWS Command Line Interface (AWS CLI) 與服務 AWS Glue API 操作互動。您可以在 AWS Glue 主控台或使用其中一個 AWS SDKs執行相同的操作。

**重要**  
變更 AWS Glue 資源政策時，您可能會不小心撤銷您帳戶中現有 AWS Glue 使用者的許可，並造成意外中斷。請僅在開發或測試帳戶中試用這些範例，並先確保您所做的變更不會破壞現有任務流程之後再變更。

**Topics**
+ [搭配 Glue AWS 使用資源型政策的考量事項](#security_iam_resource-based-policy-examples-considerations)
+ [使用資源政策來控制相同帳戶的存取](#glue-policy-resource-policies-example-same-account)

## 搭配 Glue AWS 使用資源型政策的考量事項
<a name="security_iam_resource-based-policy-examples-considerations"></a>

**注意**  
IAM 政策和 AWS Glue 資源政策都需要幾秒鐘的傳播時間。連接新政策之後到新政策在整個系統傳播之前，您可能會發現舊政策仍然有效。

您可以使用以 JSON 格式撰寫的政策文件來建立或修改資源政策。政策語法與以身分為基礎的 IAM 政策的政策語法相同 (請參閱 [IAM JSON 政策參考](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies.html))，但有下列例外狀況︰
+ 每個政策陳述式都需要 `"Principal"` 或 `"NotPrincipal"` 區塊。
+ `"Principal"` 或 `"NotPrincipal"` 必須識別有效的現有主體。不允許萬用字元模式 (例如 `arn:aws:iam::account-id:user/*`)。
+ 政策中的 `"Resource"` 區塊需要所有資源 ARN 符合下列規則表達式語法 (其中，第一個 `%s` 是 *region*，而第二個 `%s` 是 *account-id*)：

  ```
  *arn:aws:glue:%s:%s:(\*|[a-zA-Z\*]+\/?.*)
  ```

  例如，允許 `arn:aws:glue:us-west-2:account-id:*` 和 `arn:aws:glue:us-west-2:account-id:database/default`，但不允許 `*`。
+ 與以身分為基礎的政策不同，AWS Glue 資源政策只能包含屬於政策連接目錄之資源的 Amazon Resource Name (ARN)。這類 ARN 的開頭一律為 `arn:aws:glue:`。
+ 政策不能鎖定建立它的身分，使其無法執行進一步建立或修改。
+ 資源政策 JSON 文件的大小不能超過 10 KB。

## 使用資源政策來控制相同帳戶的存取
<a name="glue-policy-resource-policies-example-same-account"></a>

在此範例中，帳戶 A 的管理使用者建立了一個資源政策，以授予帳戶 A 中 IAM 使用者 `Alice` 對目錄的完整存取權。Alice 未連接任何 IAM 政策。

若要這樣做，管理員使用者會執行下列 AWS CLI 命令。

```
# Run as admin of Account A
$ aws glue put-resource-policy --profile administrator-name --region us-west-2 --policy-in-json '{
  "Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Principal": {
        "AWS": [
          "arn:aws:iam::account-A-id:user/Alice"
        ]
      },
      "Effect": "Allow",
      "Action": [
        "glue:*"
      ],
      "Resource": [
        "arn:aws:glue:us-west-2:account-A-id:*"
      ]
    }
  ]
}'
```

您可以儲存檔案中的政策文件，並在命令中 AWS CLI 參考 字首為 的檔案路徑，而不是輸入 JSON 政策文件做為 AWS CLI 命令的一部分`file://`。以下範例示範如何執行上述作業。

```
$ echo '{
  "Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Principal": {
        "AWS": [
          "arn:aws:iam::account-A-id:user/Alice"
        ]
      },
      "Effect": "Allow",
      "Action": [
        "glue:*"
      ],
      "Resource": [
        "arn:aws:glue:us-west-2:account-A-id:*"
      ]
    }
  ]
}' > /temp/policy.json

$ aws glue put-resource-policy --profile admin1 \
    --region us-west-2 --policy-in-json file:///temp/policy.json
```

傳播這項資源政策之後，Alice 即可存取帳戶 A 中的所有 AWS Glue 資源，如下所示。

```
# Run as user Alice
$ aws glue create-database --profile alice --region us-west-2 --database-input '{
    "Name": "new_database",
    "Description": "A new database created by Alice",
    "LocationUri": "s3://amzn-s3-demo-bucket"
}'

$ aws glue get-table --profile alice --region us-west-2 --database-name "default" --table-name "tbl1"}
```

AWS Glue 服務會傳回下列項目，以回應 Alice 的 `get-table` 呼叫。

```
{
  "Table": {
    "Name": "tbl1",
    "PartitionKeys": [],
    "StorageDescriptor": {
        ......
    },
    ......
  }
}
```