

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

# Amazon EC2：根據標籤將 Amazon EBS 磁碟區與 EC2 執行個體連接或分開
<a name="reference_policies_examples_ec2_ebs-owner"></a>

此範例會示範如何建立身分型政策，允許 EBS 磁碟區擁有者將使用標籤 `VolumeUser` 定義的 EBS 磁碟區與標記為開發執行個體的 EC2 執行個體 (`Department=Development`) 連接或分開。此政策授予從 AWS API 或 以程式設計方式完成此動作所需的許可 AWS CLI。若要使用此政策，請將範例政策中的{{斜體預留位置文字}}取代為您自己的資訊。然後，遵循[建立政策](access_policies_create.md)或[編輯政策](access_policies_manage-edit.md)中的指示進行操作。

如需有關建立 IAM 政策以控制對 Amazon EC2 資源的存取的詳細資訊，請參閱 *Amazon EC2 User Guide* 中的 [Controlling Access to Amazon EC2 Resources](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/UsingIAM.html)。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "ec2:AttachVolume",
                "ec2:DetachVolume"
            ],
            "Resource": "arn:aws:ec2:*:*:instance/*",
            "Condition": {
                "StringEquals": {"aws:ResourceTag/{{Department}}": "{{Development}}"}
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "ec2:AttachVolume",
                "ec2:DetachVolume"
            ],
            "Resource": "arn:aws:ec2:*:*:volume/*",
            "Condition": {
                "StringEquals": {"aws:ResourceTag/{{VolumeUser}}": "${aws:username}"}
            }
        }
    ]
}
```

------