

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

# 單值內容索引鍵政策範例
<a name="reference_policies_condition_examples-single-valued-context-keys"></a>

下列一組政策範例示範如何使用單值內容索引鍵建立政策條件。

## 範例：具有單值內容索引鍵的多個條件區塊
<a name="reference_policies_condition_examples-single-valued-context-keys-1"></a>

當條件區塊具有多個條件時，每個條件都有單一內容索引鍵，所有內容索引鍵都必須解析為 true 才能調用所需的 `Allow` 或 `Deny` 效果。當您使用否定相符條件運算子時，會反轉條件值的評估邏輯。

下列範例可讓使用者建立 EC2 磁碟區，並在磁碟區建立期間將標籤套用至磁碟區。請求內容必須包含內容索引鍵 `aws:RequestTag/project` 的值，以及內容索引鍵 `aws:ResourceTag/environment` 的值可以是生產以外的任何值。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "ec2:CreateVolume",
      "Resource": "*"
    },
    {
      "Effect": "Allow",
      "Action": "ec2:CreateTags",
      "Resource": "arn:aws:ec2:us-east-1:123456789012:volume/*",
      "Condition": {
        "StringLike": {
          "aws:RequestTag/project": "*"
        }
      }
    },
    {
      "Effect": "Allow",
      "Action": "ec2:CreateTags",
      "Resource": "arn:aws:ec2:us-east-1:123456789012:*/*",
      "Condition": {
        "StringNotEquals": {
          "aws:ResourceTag/environment": "production"
        }
      }
    }
  ]
}
```

------

請求內容必須包含專案標籤值，且無法為生產資源建立以調用 `Allow` 效果。下列 EC2 磁碟區已成功建立，因為專案名稱為 `Feature3`，資源標籤為 `QA`。

```
aws ec2 create-volume \
    --availability-zone us-east-1a \
    --volume-type gp2 \
    --size 80 \
    --tag-specifications 'ResourceType=volume,Tags=[{Key=project,Value=Feature3},{Key=environment,Value=QA}]'
```

## 範例：具有多個單值內容索引鍵和值的一個條件區塊
<a name="reference_policies_condition_examples-single-valued-context-keys-2"></a>

當條件區塊包含多個內容索引鍵，且每個內容索引鍵都具有多個值時，每個內容索引鍵都必須解析為 true，以便至少一個金鑰值能夠調用所需的 `Allow` 或 `Deny` 效果。當您使用否定相符條件運算子時，會反轉內容索引鍵值的評估邏輯。

下列範例可讓使用者在 Amazon Elastic Container Service 叢集上啟動和執行任務。
+ 對於 `aws:RequestTag/environment` 內容索引鍵 **AND**，請求內容必須包含 `production` **OR** `prod-backup`。
+ `ecs:cluster` 內容索引鍵確保任務在 `default1` **OR** `default2` ARN ECS 叢集上執行。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ecs:RunTask",
        "ecs:StartTask"
      ],
      "Resource": [
        "*"
      ],
      "Condition": {
        "StringEquals": {
          "aws:RequestTag/environment": [
            "production",
            "prod-backup"
          ]
        },
        "ArnEquals": {
          "ecs:cluster": [
            "arn:aws:ecs:us-east-1:111122223333:cluster/default1",
            "arn:aws:ecs:us-east-1:111122223333:cluster/default2"
          ]
        }
      }
    }
  ]
}
```

------

下表顯示 如何根據請求中的條件索引鍵值來 AWS 評估此政策。


| 政策條件 | 請求內容 | 結果 | 
| --- | --- | --- | 
|  <pre>"StringEquals": {<br />  "aws:RequestTag/environment": [<br />    "production",<br />    "prod-backup"<br />  ]<br />},<br />"ArnEquals": {<br />  "ecs:cluster": [<br />    "arn:aws:ecs:us-east-1:111122223333:cluster/default1",<br />    "arn:aws:ecs:us-east-1:111122223333:cluster/default2"<br />  ]<br />}</pre>  | <pre>aws:RequestTag: environment:production<br />ecs:cluster:<br />  arn:aws:ecs:us-east-1:111122223333:cluster/default1</pre>  | 匹配 | 
| <pre>"StringEquals": {<br />  "aws:RequestTag/environment": [<br />    "production",<br />    "prod-backup"<br />  ]<br />},<br />"ArnEquals": {<br />  "ecs:cluster": [<br />    "arn:aws:ecs:us-east-1:111122223333:cluster/default1",<br />    "arn:aws:ecs:us-east-1:111122223333:cluster/default2"<br />  ]<br />}</pre>  | <pre>aws:RequestTag: environment:prod-backup<br />ecs:cluster:<br />  arn:aws:ecs:us-east-1:111122223333:cluster/default2</pre>  | 匹配 | 
| <pre>"StringEquals": {<br />  "aws:RequestTag/environment": [<br />    "production",<br />    "prod-backup"<br />  ]<br />},<br />"ArnEquals": {<br />  "ecs:cluster": [<br />    "arn:aws:ecs:us-east-1:111122223333:cluster/default1",<br />    "arn:aws:ecs:us-east-1:111122223333:cluster/default2"<br />  ]<br />}</pre>  | <pre>aws:RequestTag: webserver:production<br />ecs:cluster:<br />  arn:aws:ecs:us-east-1:111122223333:cluster/default2</pre>  | 無相符項目 | 
| <pre>"StringEquals": {<br />  "aws:RequestTag/environment": [<br />    "production",<br />    "prod-backup"<br />  ]<br />},<br />"ArnEquals": {<br />  "ecs:cluster": [<br />    "arn:aws:ecs:us-east-1:111122223333:cluster/default1",<br />    "arn:aws:ecs:us-east-1:111122223333:cluster/default2"<br />  ]<br />}</pre>  |  請求內容中沒有 `aws:RequestTag`。 <pre>ecs:cluster<br />  arn:aws:ecs:us-east-1:111122223333:cluster/default2</pre>  | 無相符項目 | 