

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

# Amazon VPC 原則範例
<a name="vpc-policy-examples"></a>

根據預設，IAM 角色不具備建立或修改 VPC 資源的許可。他們也無法使用 AWS 管理主控台 AWS CLI或 AWS API 執行任務。IAM 管理員建立的 IAM 政策必須授予角色在指定資源上執行特定 API 操作的許可。管理員接著必須將這些政策連接至需要這些許可的 IAM 角色。

若要了解如何使用這些範例 JSON 政策文件建立 IAM 身分型政策，請參閱《*IAM 使用者指南*》中的[建立 IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_create.html#access_policies_create-json-editor)。

**Topics**
+ [政策最佳實務](#security_iam_service-with-iam-policy-best-practices)
+ [使用 Amazon VPC 主控台](#security_iam_id-based-policy-examples-console)
+ [建立包含公有子網的 VPC](#vpc-public-subnet-iam)
+ [修改和刪除 VPC 資源](#modify-vpc-resources-iam)
+ [管理安全群組](#vpc-security-groups-iam)
+ [管理安全群組規則](#vpc-security-group-rules-iam)
+ [在特定子網中啟動執行個體](#subnet-sg-example-iam)
+ [在特定 VPC 中啟動執行個體](#subnet-ami-example-iam)
+ [封鎖對 VPC 和子網路的公開存取](#vpc-bpa-example-iam)
+ [其他 Amazon VPC 原則範例](#security-iam-additional-examples)

## 政策最佳實務
<a name="security_iam_service-with-iam-policy-best-practices"></a>

身分型政策會判斷您帳戶中的某個人員是否可以建立、存取或刪除 Amazon VPC 資源。這些動作可能會讓您的 AWS 帳戶產生費用。當您建立或編輯身分型政策時，請遵循下列準則及建議事項：
+ **開始使用 AWS 受管政策並邁向最低權限許可** – 若要開始將許可授予您的使用者和工作負載，請使用將許可授予許多常見使用案例的 *AWS 受管政策*。它們可在您的 中使用 AWS 帳戶。我們建議您定義特定於使用案例 AWS 的客戶受管政策，以進一步減少許可。如需更多資訊，請參閱《*IAM 使用者指南*》中的 [AWS 受管政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_managed-vs-inline.html#aws-managed-policies)或[任務職能的AWS 受管政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_job-functions.html)。
+ **套用最低權限許可** – 設定 IAM 政策的許可時，請僅授予執行任務所需的許可。為實現此目的，您可以定義在特定條件下可以對特定資源採取的動作，這也稱為*最低權限許可*。如需使用 IAM 套用許可的更多相關資訊，請參閱《*IAM 使用者指南*》中的 [IAM 中的政策和許可](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies.html)。
+ **使用 IAM 政策中的條件進一步限制存取權** – 您可以將條件新增至政策，以限制動作和資源的存取。例如，您可以撰寫政策條件，指定必須使用 SSL 傳送所有請求。如果透過特定 例如 使用服務動作 AWS 服務，您也可以使用條件來授予其存取權 CloudFormation。如需詳細資訊，請參閱《*IAM 使用者指南*》中的 [IAM JSON 政策元素：條件](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html)。
+ **使用 IAM Access Analyzer 驗證 IAM 政策，確保許可安全且可正常運作** – IAM Access Analyzer 驗證新政策和現有政策，確保這些政策遵從 IAM 政策語言 (JSON) 和 IAM 最佳實務。IAM Access Analyzer 提供 100 多項政策檢查及切實可行的建議，可協助您撰寫安全且實用的政策。如需詳細資訊，請參閱《*IAM 使用者指南*》中的[使用 IAM Access Analyzer 驗證政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/access-analyzer-policy-validation.html)。
+ **需要多重要素驗證 (MFA)** – 如果您的案例需要 IAM 使用者或 中的根使用者 AWS 帳戶，請開啟 MFA 以提高安全性。如需在呼叫 API 操作時請求 MFA，請將 MFA 條件新增至您的政策。如需詳細資訊，請參閱《*IAM 使用者指南*》中的[透過 MFA 的安全 API 存取](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa_configure-api-require.html)。

如需 IAM 中最佳實務的相關資訊，請參閱《*IAM 使用者指南*》中的 [IAM 安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)。

## 使用 Amazon VPC 主控台
<a name="security_iam_id-based-policy-examples-console"></a>

若要存取 Amazon VPC 主控台，您必須擁有最基本的一組許可。這些許可必須允許您列出和檢視 AWS 帳戶中 Amazon VPC 資源的詳細資訊。如果您建立比最基本必要許可更嚴格的身分型政策，則對於具有該政策的實體 (IAM 角色) 而言，主控台就無法如預期運作。

下列政策會授與角色在 VPC 主控台中列出資源的許可，但不會建立、更新或刪除這些資源。

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

****  

```
{
    "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
<a name="vpc-public-subnet-iam"></a>

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

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

****  

```
{
   "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 資源
<a name="modify-vpc-resources-iam"></a>

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

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

****  

```
{
    "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}}"
                }
            }
        }
    ]
}
```

------

## 管理安全群組
<a name="vpc-security-groups-iam"></a>

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

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

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

****  

```
{
    "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"
                },
                "ForAnyValue: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` 動作新增至您的政策。

## 管理安全群組規則
<a name="vpc-security-group-rules-iam"></a>

下列政策會授予角色許可，以允許角色檢視所有安全群組和安全群組規則、為特定 VPC 的安全群組新增和移除傳入和傳出規則，以及修改指定 VPC 的規則描述。第一個陳述式使用 `ec2:Vpc` 條件金鑰將許可範圍設定為特定 VPC。

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

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

****  

```
{
    "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:{{us-east-1}}:{{123456789012}}:security-group/*",
            "Condition": {
                "ArnEquals": {
                    "ec2:Vpc": "arn:aws:ec2:{{us-east-1}}:{{123456789012}}:vpc/{{vpc-1234567890abcdef0}}"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "ec2:DescribeSecurityGroups",
                "ec2:DescribeSecurityGroupRules",
                "ec2:DescribeTags"
            ],
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "ec2:ModifySecurityGroupRules"
            ],
            "Resource": "arn:aws:ec2:{{us-east-1}}:{{123456789012}}:security-group-rule/*"
        }
    ]
}
```

------

## 在特定子網中啟動執行個體
<a name="subnet-sg-example-iam"></a>

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

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

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "ec2:RunInstances",
            "Resource": [
                "arn:aws:ec2:{{us-east-1}}::image/ami-*",
                "arn:aws:ec2:{{us-east-1}}:{{123456789012}}:instance/*",
                "arn:aws:ec2:{{us-east-1}}:{{123456789012}}:subnet/{{subnet-1234567890abcdef0}}",
                "arn:aws:ec2:{{us-east-1}}:{{123456789012}}:network-interface/*",
                "arn:aws:ec2:{{us-east-1}}:{{123456789012}}:volume/*",
                "arn:aws:ec2:{{us-east-1}}:{{123456789012}}:key-pair/*",
                "arn:aws:ec2:{{us-east-1}}:{{123456789012}}:security-group/{{sg-0abcdef1234567890}}"
            ]
        }
    ]
}
```

------

## 在特定 VPC 中啟動執行個體
<a name="subnet-ami-example-iam"></a>

下列政策會授予角色許可，以允許角色在特定 VPC 的任何子網路中啟動執行個體。此政策執行這項作業的方式是將條件金鑰 (`ec2:Vpc`) 套用至子網路資源。

此政策也會授予角色許，以允許角色僅使用具有 "`department=dev`" 標籤的 AMI 啟動執行個體。

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

****  

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

------

## 封鎖對 VPC 和子網路的公開存取
<a name="vpc-bpa-example-iam"></a>

下列政策範例授予角色許可，以使用 [VPC 封鎖公開存取 (BPA) 功能](security-vpc-bpa.md)，以封鎖對 VPC 和子網路中資源的公開存取。

範例 1 – 允許唯讀存取 VPC BPA 全帳戶設定和 VPC BPA 排除。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "VPCBPAReadOnlyAccess",
      "Action": [
        "ec2:DescribeVpcBlockPublicAccessOptions",
        "ec2:DescribeVpcBlockPublicAccessExclusions"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
```

------

範例 2 – 允許完整讀取和寫入存取 VPC BPA 全帳戶設定和 VPC BPA 排除。

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

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "VPCBPAFullAccess",
      "Action": [
        "ec2:DescribeVpcBlockPublicAccessOptions",
        "ec2:DescribeVpcBlockPublicAccessExclusions",
        "ec2:ModifyVpcBlockPublicAccessOptions",
        "ec2:CreateVpcBlockPublicAccessExclusion",
        "ec2:ModifyVpcBlockPublicAccessExclusion",
        "ec2:DeleteVpcBlockPublicAccessExclusion"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
```

------

範例 3 – 允許存取所有 EC2 API，但修改 VPC BPA 設定和建立排除除外。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "EC2FullAccess",
            "Action": [
                "ec2:*"
            ],
            "Effect": "Allow",
            "Resource": "*"
        },
        {
            "Sid": "VPCBPAPartialAccess",
            "Action": [
                "ec2:ModifyVpcBlockPublicAccessOptions",
                "ec2:CreateVpcBlockPublicAccessExclusion"
            ],
            "Effect": "Deny",
            "Resource": "*"
        }
    ]
}
```

------

## 其他 Amazon VPC 原則範例
<a name="security-iam-additional-examples"></a>

您可以在下列文件中找到與 Amazon VPC 相關的其他 IAM 政策範例：
+ [受管理的字首清單](managed-prefix-lists.md#managed-prefix-lists-iam)
+ [流量鏡射](https://docs.aws.amazon.com/vpc/latest/mirroring/traffic-mirroring-security.html)
+ [傳輸閘道](https://docs.aws.amazon.com/vpc/latest/tgw/transit-gateway-authentication-access-control.html#tgw-example-iam-policies)
+ [VPC 端點與 VPC 端點服務 (AWS PrivateLink)](https://docs.aws.amazon.com/vpc/latest/privatelink/security_iam_id-based-policy-examples.html)
+ [VPC 對等互連](https://docs.aws.amazon.com/vpc/latest/peering/security-iam.html)