Help us improve the AWS re:Post Knowledge Center by sharing your feedback in a brief survey. Your input can influence how we create and update our content to better support your AWS journey.
我該如何建立 IAM 政策,明確授予在具有標籤的指定虛擬私有雲端 (VPC) 中建立及管理 EC2 執行個體的權限?
我想要建立 AWS Identity and Access Management (IAM) 政策,明確授予 IAM 身分管理虛擬私有雲端 (VPC) 中 Amazon Elastic Compute Cloud (Amazon EC2) 執行個體的權限。
簡短說明
IAM 政策必須限制 IAM 身分的權限,使其只能管理具有標籤的 VPC 中的執行個體。
Amazon EC2 提供部分資源層級權限,但您可以使用多個服務層級的動作、資源與條件。若要控制 EC2 資源的存取,請使用資源標籤。
解決方法
建立政策,授予在 VPC 中啟動執行個體的權限,但限制 IAM 身分管理執行個體的權限。若要限制執行個體的管理,請使用 ec2:ResourceTag 政策條件。
建立受管政策以附加至啟動執行個體的 IAM 身分
請完成以下步驟:
-
開啟 IAM console (IAM 主控台)。
-
選擇 Policies (政策),然後選擇 Create Policy (建立政策)。
-
選擇 JSON 索引標籤,然後輸入以下自訂政策:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "GrantIAMPassRoleOnlyForEC2", "Action": [ "iam:PassRole" ], "Effect": "Allow", "Resource": [ "arn:aws:iam::account_number:role/role_name", "arn:aws:iam::account_number:role/role_nameE" ], "Condition": { "StringEquals": { "iam:PassedToService": "ec2.amazonaws.com" } } }, { "Sid": "ReadOnlyEC2WithNonResource", "Action": [ "ec2:Describe*", "iam:ListInstanceProfiles" ], "Effect": "Allow", "Resource": "*" }, { "Sid": "ModifyingEC2WithNonResource", "Action": [ "ec2:CreateKeyPair", "ec2:CreateSecurityGroup" ], "Effect": "Allow", "Resource": "*" }, { "Sid": "RunInstancesWithTagRestrictions", "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": [ "arn:aws:ec2:region:a:instance/*", "arn:aws:ec2:region:account_number:volume/*" ], "Condition": { "StringEquals": { "aws:RequestTag/TAG-KEY": "tag_value" } } }, { "Sid": "RemainingRunInstancePermissionsNonResource", "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": [ "arn:aws:ec2:REGION::image/*", "arn:aws:ec2:REGION::snapshot/*", "arn:aws:ec2:region:account_number:network-interface/*", "arn:aws:ec2:region:account_number:key-pair/*", "arn:aws:ec2:region:account_number:security-group/*" ] }, { "Sid": "EC2RunInstancesVpcSubnet", "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": "arn:aws:ec2:region:account_number:subnet/*", "Condition": { "StringEquals": { "ec2:Vpc": "arn:aws:ec2:region:account_number:vpc/VPC-ID" } } }, { "Sid": "EC2VpcNonResourceSpecificActions", "Effect": "Allow", "Action": [ "ec2:DeleteNetworkAcl", "ec2:DeleteNetworkAclEntry", "ec2:DeleteRoute", "ec2:DeleteRouteTable", "ec2:AuthorizeSecurityGroupEgress", "ec2:AuthorizeSecurityGroupIngress", "ec2:RevokeSecurityGroupEgress", "ec2:RevokeSecurityGroupIngress", "ec2:DeleteSecurityGroup", "ec2:CreateNetworkInterfacePermission", "ec2:CreateRoute", "ec2:UpdateSecurityGroupRuleDescriptionsEgress", "ec2:UpdateSecurityGroupRuleDescriptionsIngress" ], "Resource": "*", "Condition": { "StringEquals": { "ec2:Vpc": "arn:aws:ec2:region:account_number:vpc/VPCID" } } }, { "Sid": "AllowInstanceActionsTagBased", "Effect": "Allow", "Action": [ "ec2:RebootInstances", "ec2:StopInstances", "ec2:TerminateInstances", "ec2:StartInstances", "ec2:AttachVolume", "ec2:DetachVolume", "ec2:AssociateIamInstanceProfile", "ec2:DisassociateIamInstanceProfile", "ec2:GetConsoleScreenshot", "ec2:ReplaceIamInstanceProfileAssociation" ], "Resource": [ "arn:aws:ec2:region:account_number:instance/*", "arn:aws:ec2:region:account_number:volume/*" ], "Condition": { "StringEquals": { "ec2:ResourceTag/TAG-KEY": "Tag_key" } } }, { "Sid": "AllowCreateTagsOnlyLaunching", "Effect": "Allow", "Action": [ "ec2:CreateTags" ], "Resource": [ "arn:aws:ec2:region:account_number:instance/*", "arn:aws:ec2:region:account_number:volume/*" ], "Condition": { "StringEquals": { "ec2:CreateAction": "RunInstances" } } } ] }**注意:**請將 account_number、region、tag_key、tag_value、VPC-ID 以及 role_name 參數替換為您的值。
-
選擇 Review policy (檢閱政策),然後輸入名稱。
-
選擇 Create policy (建立政策)。
**注意:**您必須將部分項目替換為您環境中的特定資源。如需詳細資訊,請參閱使用 Amazon Resource Names (ARNs) 識別 AWS 資源。
將政策附加至群組
請完成以下步驟:
- 開啟 IAM console (IAM 主控台)。
- 在導覽窗格中,選擇 User groups (使用者群組),然後選擇群組名稱。
**注意:**最佳實務是將政策附加至使用者群組,然後將使用者新增至適當的群組。 - 選擇 Add permissions (新增權限),然後選擇 Attach policies (附加政策)。
- 輸入政策名稱,然後選擇您的政策。
- 選擇 Attach Policy (附加政策)。
當您附加自訂政策後,IAM 身分可以存取 AWS 管理主控台。IAM 身分接著可以開啟 EC2 儀表板並啟動 EC2 執行個體。IAM 身分必須指定子網路、VPC 以及標籤,才能啟動 EC2 執行個體。
EC2 資源標籤條件會限制以下動作:
- 啟動執行個體。
- 停止執行個體。
- 重新啟動執行個體。
- 終止執行個體。
- 將磁碟區附加至執行個體。
- 從執行個體分離磁碟區。
- 取消將 IAM 執行個體設定檔與執行個體建立關聯。
- 建立執行個體設定檔關聯。
- 替換執行個體的 IAM 執行個體設定檔關聯。
- 取得執行個體的主控台螢幕擷取畫面。
此政策會限制針對 VPC 的以下動作:
- 刪除安全群組。
- 建立及刪除路由。
- 刪除路由表。
- 刪除網路存取控制清單 (網路 ACL)。
- 刪除網路存取控制清單項目。
- 授權或撤銷安全群組傳入與傳出規則。
- 建立網路介面權限。
- 更新安全群組傳入與傳出規則的描述。
相關資訊
PrincipalTag、ResourceTag、RequestTag 與 TagKeys 條件索引鍵如何在 IAM 政策中控制存取?
- 語言
- 中文 (繁體)

相關內容
- 已提問 2 年前
- 已提問 1 年前