

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

# 使用標籤型 IAM 存取控制政策
<a name="tags-access-control"></a>

使用標籤可讓您撰寫包含 `Condition` 區塊的 IAM 政策，以根據資源的標籤來控制對資源的存取。本節包含工作群組和資料目錄資源的標籤政策範例。

## 工作群組的標籤政策範例
<a name="tag-policy-examples-workgroups"></a>

### 範例 – 基本標籤政策
<a name="tag-policy-examples-workgroups-basic"></a>

以下 IAM 政策可讓您執行查詢，並與名為 `workgroupA` 的工作群組的標籤互動：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
       {
            "Effect": "Allow",
            "Action": [
                "athena:ListWorkGroups",
                "athena:ListEngineVersions",
                "athena:ListDataCatalogs",
                "athena:ListDatabases",
                "athena:GetDatabase",
                "athena:ListTableMetadata",
                "athena:GetTableMetadata"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "athena:GetWorkGroup",
                "athena:TagResource",
                "athena:UntagResource",
                "athena:ListTagsForResource",
                "athena:StartQueryExecution",
                "athena:GetQueryExecution",
                "athena:BatchGetQueryExecution",
                "athena:ListQueryExecutions",
                "athena:StopQueryExecution",
                "athena:GetQueryResults",
                "athena:GetQueryResultsStream",
                "athena:CreateNamedQuery",
                "athena:GetNamedQuery",
                "athena:BatchGetNamedQuery",
                "athena:ListNamedQueries",
                "athena:DeleteNamedQuery",
                "athena:CreatePreparedStatement",
                "athena:GetPreparedStatement",
                "athena:ListPreparedStatements",
                "athena:UpdatePreparedStatement",
                "athena:DeletePreparedStatement"
            ],
            "Resource": "arn:aws:athena:us-east-1:123456789012:workgroup/workgroupA"
        }
    ]
}
```

------

### 範例 – 根據標籤索引鍵和標籤值配對在工作群組上拒絕動作的政策區塊
<a name="tag-policy-examples-workgroups-basic"></a>

與資源 (如工作群組) 相關聯的標籤稱為資源標籤。資源標籤可讓您編寫政策區塊，如以下項目在以鍵值組所標記的任何工作群組上拒絕列出的動作，例如 `stack`、`production`。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Deny",
            "Action": [
                "athena:GetWorkGroup",
                "athena:UpdateWorkGroup",
                "athena:DeleteWorkGroup",
                "athena:TagResource",
                "athena:UntagResource",
                "athena:ListTagsForResource",
                "athena:StartQueryExecution",
                "athena:GetQueryExecution",
                "athena:BatchGetQueryExecution",
                "athena:ListQueryExecutions",
                "athena:StopQueryExecution",
                "athena:GetQueryResults",
                "athena:GetQueryResultsStream",
                "athena:CreateNamedQuery",
                "athena:GetNamedQuery",
                "athena:BatchGetNamedQuery",
                "athena:ListNamedQueries",
                "athena:DeleteNamedQuery",
                "athena:CreatePreparedStatement",
                "athena:GetPreparedStatement",
                "athena:ListPreparedStatements",
                "athena:UpdatePreparedStatement",
                "athena:DeletePreparedStatement"
            ],
            "Resource": "arn:aws:athena:us-east-1:123456789012:workgroup/*",
            "Condition": {
                "StringEquals": {
                    "aws:ResourceTag/stack": "production"
                }
            }
        }
    ]
}
```

------

### 範例 – 限制對指定的標籤提出標籤變更動作請求的政策區塊
<a name="tag-policy-examples-workgroups-restricted-specific"></a>

做為參數傳入至變更標籤之操作的標籤 (例如帶有標籤的 `TagResource`、`UntagResource` 或 `CreateWorkGroup`) 稱為請求標籤。下列範例政策區塊只有在傳遞的其中一個標籤具有索引鍵 `costcenter` 和數值 `1`、`2` 或 `3` 時，才允許 `CreateWorkGroup` 操作。

**注意**  
如果您想要允許 IAM 角色在 `CreateWorkGroup` 操作中傳入標籤，請確定您授予角色進行 `TagResource` 和 `CreateWorkGroup` 動作的許可。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "athena:CreateWorkGroup",
                "athena:TagResource"
            ],
            "Resource": "arn:aws:athena:us-east-1:123456789012:workgroup/*",
            "Condition": {
                "StringEquals": {
                    "aws:RequestTag/costcenter": [
                        "1",
                        "2",
                        "3"
                    ]
                }
            }
        }
    ]
}
```

------

## 資料目錄的標籤政策範例
<a name="tag-policy-examples-data-catalogs"></a>

### 範例 – 基本標籤政策
<a name="tag-policy-examples-data-catalogs-basic"></a>

以下 IAM 政策可讓您與名為 `datacatalogA` 的資料目錄的標籤互動：

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "athena:ListWorkGroups",
                "athena:ListEngineVersions",
                "athena:ListDataCatalogs",
                "athena:ListDatabases",
                "athena:GetDatabase",
                "athena:ListTableMetadata",
                "athena:GetTableMetadata"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "athena:GetWorkGroup",
                "athena:TagResource",
                "athena:UntagResource",
                "athena:ListTagsForResource",
                "athena:StartQueryExecution",
                "athena:GetQueryExecution",
                "athena:BatchGetQueryExecution",
                "athena:ListQueryExecutions",
                "athena:StopQueryExecution",
                "athena:GetQueryResults",
                "athena:GetQueryResultsStream",
                "athena:CreateNamedQuery",
                "athena:GetNamedQuery",
                "athena:BatchGetNamedQuery",
                "athena:ListNamedQueries",
                "athena:DeleteNamedQuery"
            ],
            "Resource": [
                "arn:aws:athena:us-east-1:123456789012:workgroup/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "athena:CreateDataCatalog",
                "athena:GetDataCatalog",
                "athena:UpdateDataCatalog",
                "athena:DeleteDataCatalog",
                "athena:ListDatabases",
                "athena:GetDatabase",
                "athena:ListTableMetadata",
                "athena:GetTableMetadata",
                "athena:TagResource",
                "athena:UntagResource",
                "athena:ListTagsForResource"
            ],
            "Resource": "arn:aws:athena:us-east-1:123456789012:datacatalog/datacatalogA"
        }
    ]
}
```

------

### 範例 – 根據標籤索引鍵和標籤值組在資料目錄上拒絕動作的政策區塊
<a name="tag-policy-examples-data-catalogs-deny-actions"></a>

您可以使用資源標籤撰寫政策區塊，以拒絕針對以特定標籤鍵值組標記的資料目錄上的特定動作。下列範例政策會拒絕具有標籤鍵值組 `stack`、`production` 的資料目錄上的動作。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Deny",
            "Action": [
                "athena:CreateDataCatalog",
                "athena:GetDataCatalog",
                "athena:UpdateDataCatalog",
                "athena:DeleteDataCatalog",
                "athena:GetDatabase",
                "athena:ListDatabases",
                "athena:GetTableMetadata",
                "athena:ListTableMetadata",
                "athena:StartQueryExecution",
                "athena:TagResource",
                "athena:UntagResource",
                "athena:ListTagsForResource"
            ],
            "Resource": "arn:aws:athena:us-east-1:123456789012:datacatalog/*",
            "Condition": {
                "StringEquals": {
                    "aws:ResourceTag/stack": "production"
                }
            }
        }
    ]
}
```

------

### 範例 – 限制對指定的標籤提出標籤變更動作請求的政策區塊
<a name="tag-policy-examples-data-catalogs-action-specific-tags"></a>

做為參數傳入至變更標籤之操作的標籤 (例如帶有標籤的 `TagResource`、`UntagResource` 或 `CreateDataCatalog`) 稱為請求標籤。下列範例政策區塊只有在傳遞的其中一個標籤具有索引鍵 `costcenter` 和數值 `1`、`2` 或 `3` 時，才允許 `CreateDataCatalog` 操作。

**注意**  
如果您想要允許 IAM 角色在 `CreateDataCatalog` 操作中傳入標籤，請確定您授予角色進行 `TagResource` 和 `CreateDataCatalog` 動作的許可。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "athena:CreateDataCatalog",
                "athena:TagResource"
            ],
            "Resource": "arn:aws:athena:us-east-1:123456789012:datacatalog/*",
            "Condition": {
                "StringEquals": {
                    "aws:RequestTag/costcenter": [
                        "1",
                        "2",
                        "3"
                    ]
                }
            }
        }
    ]
}
```

------