Amazon VPC 政策示例 - Amazon Virtual Private Cloud

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

Amazon VPC 政策示例

根據預設,IAM角色沒有建立或修改VPC資源的權限。他們也無法使用 AWS Management Console AWS CLI、或執行工作 AWS API。IAM系統管理員必須建立IAM原則,以授與角色權限,才能對所需的指定資源執行特定API作業。然後,系統管理員必須將這些原則附加至需要這些權限的IAM角色。

若要瞭解如何使用這些範例原則文件建立以IAM身分識別為基礎的JSON策略,請參閱使用指南中的IAM建立IAM策略。

政策最佳實務

以身分識別為基礎的政策決定某人是否可以在您的帳戶中建立、存取或刪除 Amazon VPC 資源。這些動作可能會讓您的 AWS 帳戶產生費用。當您建立或編輯身分型政策時,請遵循下列準則及建議事項:

  • 開始使用 AWS 受管原則並邁向最低權限權限 — 若要開始授與使用者和工作負載的權限,請使用可授與許多常見使用案例權限的AWS 受管理原則。它們可用在您的 AWS 帳戶. 建議您透過定義特定於您使用案例的 AWS 客戶管理政策,進一步降低使用權限。如需詳細資訊,請參閱AWS 《IAM使用指南》中針對工作職能的AWS 受管理策略或受管理的策略

  • 套用最低權限權限 — 當您使用原則設定權限時,IAM只授與執行工作所需的權限。為實現此目的,您可以定義在特定條件下可以對特定資源採取的動作,這也稱為最低權限許可。如需有關使用套用權限IAM的詳細資訊,請參閱《使用指南》IAM中的IAM《策略與權限

  • 使用IAM策略中的條件進一步限制存取 — 您可以在策略中新增條件,以限制對動作和資源的存取。例如,您可以撰寫政策條件,以指定必須使用傳送所有要求SSL。您也可以使用條件來授與對服務動作的存取權 (如透過特定) 使用這些動作 AWS 服務,例如 AWS CloudFormation。如需詳細資訊,請參閱《IAM使用指南》中的IAMJSON策略元素:條件

  • 使用 IAM Access Analyzer 驗證您的原IAM則,以確保安全和功能性的權限 — IAM Access Analyzer 會驗證新的和現有的原則,以便原則遵循IAM原則語言 (JSON) 和IAM最佳做法。IAMAccess Analyzer 提供超過 100 項原則檢查和可行的建議,協助您撰寫安全且功能正常的原則。如需詳細資訊,請參閱IAM使IAM用指南中的存取分析器原則驗證

  • 需要多因素驗證 (MFA) — 如果您的案例需要使IAM用者或 root 使用者 AWS 帳戶,請開啟以取得額外MFA的安全性。若要在呼叫API作業MFA時需要,請在原則中新增MFA條件。如需詳細資訊,請參閱《IAM使用指南》中的 < 設定MFA受保護的API存取 >。

如需中最佳作法的詳細資訊IAM,請參閱《IAM使用指南》IAM中的「安全性最佳作法」。

使用 Amazon VPC 控制台

若要存取 Amazon VPC 主控台,您必須擁有最少一組許可。這些許可必須允許您列出和檢視 AWS 帳戶中 Amazon VPC 資源的詳細資訊。如果您建立的以身分識別為基礎的原則,其限制性高於最低所需權限,則控制台將無法如預期用於具有該原則的實體 (IAM角色) 運作。

下列原則授與角色列出VPC主控台中資源的權限,但無法建立、更新或刪除資源。

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ec2:DescribeAccountAttributes", "ec2:DescribeAddresses", "ec2:DescribeAvailabilityZones", "ec2:DescribeClassicLinkInstances", "ec2:DescribeClientVpnEndpoints", "ec2:DescribeCustomerGateways", "ec2:DescribeDhcpOptions", "ec2:DescribeEgressOnlyInternetGateways", "ec2:DescribeFlowLogs", "ec2:DescribeInternetGateways", "ec2:DescribeManagedPrefixLists", "ec2:DescribeMovingAddresses", "ec2:DescribeNatGateways", "ec2:DescribeNetworkAcls", "ec2:DescribeNetworkInterfaceAttribute", "ec2:DescribeNetworkInterfacePermissions", "ec2:DescribeNetworkInterfaces", "ec2:DescribePrefixLists", "ec2:DescribeRouteTables", "ec2:DescribeSecurityGroupReferences", "ec2:DescribeSecurityGroups", "ec2:DescribeSecurityGroupRules", "ec2:DescribeStaleSecurityGroups", "ec2:DescribeSubnets", "ec2:DescribeTags", "ec2:DescribeTrafficMirrorFilters", "ec2:DescribeTrafficMirrorSessions", "ec2:DescribeTrafficMirrorTargets", "ec2:DescribeTransitGateways", "ec2:DescribeTransitGatewayVpcAttachments", "ec2:DescribeTransitGatewayRouteTables", "ec2:DescribeVpcAttribute", "ec2:DescribeVpcClassicLink", "ec2:DescribeVpcClassicLinkDnsSupport", "ec2:DescribeVpcEndpoints", "ec2:DescribeVpcEndpointConnectionNotifications", "ec2:DescribeVpcEndpointConnections", "ec2:DescribeVpcEndpointServiceConfigurations", "ec2:DescribeVpcEndpointServicePermissions", "ec2:DescribeVpcEndpointServices", "ec2:DescribeVpcPeeringConnections", "ec2:DescribeVpcs", "ec2:DescribeVpnConnections", "ec2:DescribeVpnGateways", "ec2:GetManagedPrefixListAssociations", "ec2:GetManagedPrefixListEntries" ], "Resource": "*" } ] }

您不需要為只對 AWS CLI 或進行呼叫的角色允許最低主控台權限 AWS API。而是只允許存取符合角色需要執行之API作業的動作。

使用公VPC用子網路建立

下列範例可讓角色建立VPCs子網路、路由表和網際網路閘道。角色也可以將網際網路閘道附加至網際網路閘道,VPC並在路由表中建立路由。此ec2:ModifyVpcAttribute動作可讓角色啟用的DNS主機DNS名稱VPC,以便每個啟動至某個執行個體都VPC會收到主機名稱。

{ "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Action": [ "ec2:CreateVpc", "ec2:CreateSubnet", "ec2:DescribeAvailabilityZones", "ec2:CreateRouteTable", "ec2:CreateRoute", "ec2:CreateInternetGateway", "ec2:AttachInternetGateway", "ec2:AssociateRouteTable", "ec2:ModifyVpcAttribute" ], "Resource": "*" } ] }

上述政策也可讓角色在 Amazon VPC 主控台VPC中建立。

修改和刪除VPC資源

您可能想要控制角色可以修改或刪除的VPC資源。例如,下列政策允許角色使用和刪除具有標籤 Purpose=Test 的路由表。此政策也會指定角色只能刪除具有標籤 Purpose=Test 的網際網路閘道。角色無法使用沒有此標籤的路由表或網際網路閘道。

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "ec2:DeleteInternetGateway", "Resource": "arn:aws:ec2:*:*:internet-gateway/*", "Condition": { "StringEquals": { "ec2:ResourceTag/Purpose": "Test" } } }, { "Effect": "Allow", "Action": [ "ec2:DeleteRouteTable", "ec2:CreateRoute", "ec2:ReplaceRoute", "ec2:DeleteRoute" ], "Resource": "arn:aws:ec2:*:*:route-table/*", "Condition": { "StringEquals": { "ec2:ResourceTag/Purpose": "Test" } } } ] }

管理安全群組

下列政策可讓角色管理安全群組。第一個陳述式允許角色刪除任何具有標籤 Stack=test 的安全群組並管理具有標籤 Stack=test 的任何安全群組的傳入和傳出規則。第二個陳述式要求角色使用標籤 Stack=Test 來標記每一個他們建立的安全群組。第三個陳述式允許角色在建立安全群組時建立標籤。第四個陳述式允許角色檢視任何安全群組和安全群組規則。第五個陳述式允許角色在VPC.

注意

AWS CloudFormation 服務無法使用此原則來建立具有必要標記的安全性群組。如果您移除需要標記之 ec2:CreateSecurityGroup 動作上的條件,則該政策會有效。

{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ec2:RevokeSecurityGroupIngress", "ec2:AuthorizeSecurityGroupEgress", "ec2:AuthorizeSecurityGroupIngress", "ec2:UpdateSecurityGroupRuleDescriptionsEgress", "ec2:RevokeSecurityGroupEgress", "ec2:DeleteSecurityGroup", "ec2:ModifySecurityGroupRules", "ec2:UpdateSecurityGroupRuleDescriptionsIngress" ], "Resource": "arn:aws:ec2:*:*:security-group/*", "Condition": { "StringEquals": { "ec2:ResourceTag/Stack": "test" } } }, { "Effect": "Allow", "Action": "ec2:CreateSecurityGroup", "Resource": "arn:aws:ec2:*:*:security-group/*", "Condition": { "StringEquals": { "aws:RequestTag/Stack": "test" }, "ForAllValues:StringEquals": { "aws:TagKeys": "Stack" } } }, { "Effect": "Allow", "Action": "ec2:CreateTags", "Resource": "arn:aws:ec2:*:*:security-group/*", "Condition": { "StringEquals": { "ec2:CreateAction": "CreateSecurityGroup" } } }, { "Effect": "Allow", "Action": [ "ec2:DescribeSecurityGroupRules", "ec2:DescribeVpcs", "ec2:DescribeSecurityGroups" ], "Resource": "*" }, { "Effect": "Allow", "Action": "ec2:CreateSecurityGroup", "Resource": "arn:aws:ec2:*:*:vpc/*" } ] }

若要允許角色變更與執行個體相關聯的安全群組,請將 ec2:ModifyInstanceAttribute 動作新增至您的政策。

若要允許角色變更網路介面的安全群組,請將 ec2:ModifyNetworkInterfaceAttribute 動作新增至您的政策。

管理安全群組規則

下列原則授與角色權限,以檢視所有安全群組和安全群組規則、新增和移除特定安全群組的輸入和輸出規則VPC,以及修改指定之規則描述的權限VPC。第一個陳述式會使用ec2:Vpc條件索引鍵,將權限限定為特定的範圍VPC。

第二個陳述式會授予角色許可,以允許角色描述所有安全群組、安全群組規則和標籤。這可讓角色檢視安全群組規則,以便進行修改。

{ "Version": "2012-10-17", "Statement":[{ "Effect":"Allow", "Action": [ "ec2:AuthorizeSecurityGroupIngress", "ec2:RevokeSecurityGroupIngress", "ec2:UpdateSecurityGroupRuleDescriptionsIngress", "ec2:AuthorizeSecurityGroupEgress", "ec2:RevokeSecurityGroupEgress", "ec2:UpdateSecurityGroupRuleDescriptionsEgress", "ec2:ModifySecurityGroupRules" ], "Resource": "arn:aws:ec2:region:account-id:security-group/*", "Condition": { "ArnEquals": { "ec2:Vpc": "arn:aws:ec2:region:account-id:vpc/vpc-id" } } }, { "Effect": "Allow", "Action": [ "ec2:DescribeSecurityGroups", "ec2:DescribeSecurityGroupRules", "ec2:DescribeTags" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "ec2:ModifySecurityGroupRules" ], "Resource": "arn:aws:ec2:region:account-id:security-group-rule/*" } ] }

在特定子網中啟動執行個體

下列政策會授予角色許可,以允許角色在特定子網路中啟動執行個體以及在請求中使用特定安全群組。原則會透過ARN為子網路和安全性群組指定。ARN如果角色嘗試在不同的子網路中啟動執行個體,或使用不同的安全群組來啟動執行個體,則請求會失敗 (除非另一個政策或陳述式授予了角色執行此作業的許可)。

此政策也會授予許可來使用網路界面資源。在子網路中啟動時,根據預設,RunInstances 請求會建立主要網路介面,因此角色需要在啟動執行個體時建立此資源的許可。

{ "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": [ "arn:aws:ec2:region::image/ami-*", "arn:aws:ec2:region:account:instance/*", "arn:aws:ec2:region:account:subnet/subnet-id", "arn:aws:ec2:region:account:network-interface/*", "arn:aws:ec2:region:account:volume/*", "arn:aws:ec2:region:account:key-pair/*", "arn:aws:ec2:region:account:security-group/sg-id" ] } ] }

將執行個體啟動到特定 VPC

下列原則授予角色權限,可將執行個體啟動至特定子網路中VPC。此政策執行這項作業的方式是將條件金鑰 (ec2:Vpc) 套用至子網路資源。

此政策也會授與角色權限,讓您僅使用標籤AMIs為「department=dev」的執行個體啟動。

{ "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": "arn:aws:ec2:region:account-id:subnet/*", "Condition": { "ArnEquals": { "ec2:Vpc": "arn:aws:ec2:region:account-id:vpc/vpc-id" } } }, { "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": "arn:aws:ec2:region::image/ami-*", "Condition": { "StringEquals": { "ec2:ResourceTag/department": "dev" } } }, { "Effect": "Allow", "Action": "ec2:RunInstances", "Resource": [ "arn:aws:ec2:region:account:instance/*", "arn:aws:ec2:region:account:volume/*", "arn:aws:ec2:region:account:network-interface/*", "arn:aws:ec2:region:account:key-pair/*", "arn:aws:ec2:region:account:security-group/*" ] } ] }

其他 Amazon VPC 政策示例

您可以在下列文件中找到與 Amazon 相關VPC的其他範例IAM政策: