

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

# 在 API Gateway 中使用私有 API 的 VPC 端點政策
<a name="apigateway-vpc-endpoint-policies"></a>

若要提高私有 API 的安全性，您可以建立 VPC 端點政策。VPC 端點政策為 IAM 資源政策，您可將其連接至 VPC 端點。如需詳細資訊，請參閱[使用 VPC 端點控制服務的存取](https://docs.aws.amazon.com/vpc/latest/privatelink/vpc-endpoints-access.html)。

您可能會想要建立 VPC 端點政策來執行下列任務。
+ 僅允許特定組織或資源存取您的 VPC 端點，並調用您的 API。
+ 使用單一政策，避免使用工作階段型或角色型政策控制 API 的流量。
+ 從現場部署遷移到 時，請緊固應用程式的安全周邊 AWS。

## VPC 端點政策考量事項
<a name="apigateway-vpc-endpoint-policies-considerations"></a>

以下是 VPC 端點政策的考量事項：
+ 呼叫者的身分是根據 `Authorization` 標頭值進行評估。首先會評估 VPC 端點政策，然後 API Gateway 會根據方法請求上設定的授權類型評估請求。下表顯示如何根據 `Authorization` 標頭值的內容評估 VPC 端點政策。    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/apigateway/latest/developerguide/apigateway-vpc-endpoint-policies.html)
+ 如果您的存取控制取決於使用持有人權杖，例如 Lambda 或 Amazon Cognito 授權方，則您可以使用[資源的屬性](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-resource-properties)來控制您的安全邊界。
+  如果您的授權控制項使用 IAM 授權，則您可以使用[資源的屬性](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-resource-properties)和[主體的屬性](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html#condition-keys-resource-principal)來控制您的安全邊界。
+ VPC 端點政策可與 API Gateway 資源政策搭配使用。API Gateway 資源政策指定由哪個主體存取 API。端點政策指定誰可以存取 VPC，以及可以從 VPC 端點呼叫哪些 API。您的私有 API 需要資源政策，但您不需要建立自訂 VPC 端點政策。

## VPC 端點政策範例
<a name="apigateway-vpc-endpoint-policies-examples"></a>

您可以為 Amazon API Gateway 建立 Amazon Virtual Private Cloud 端點的政策，您可以在其中指定如下：
+ 可執行動作的主體。
+ 可執行的動作。
+ 可對其執行動作的資源。

這可能取決於授權標頭的內容。如需詳細資訊，請參閱[VPC 端點政策考量事項](#apigateway-vpc-endpoint-policies-considerations)。如需其他範例政策，請參閱 GitHub 網站上的[資料周界政策範例](https://github.com/aws-samples/data-perimeter-policy-examples)。

您需要使用 VPC 主控台，才能將政策連接至 VPC 端點。如需詳細資訊，請參閱[使用 VPC 端點控制服務的存取](https://docs.aws.amazon.com/vpc/latest/privatelink/vpc-endpoints-access.html)。

## 範例 1：授予兩個 API 存取權限的 VPC 端點政策
<a name="apigateway-vpc-endpoint-policies-example-1"></a>

以下範例政策會透過連接政策的 VPC 端點，僅授予使用者存取兩個特定 API 的權限。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Principal": "*",
            "Action": [
                "execute-api:Invoke"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:execute-api:us-east-1:123412341234:a1b2c3d4e5/*",
                "arn:aws:execute-api:us-east-1:123412341234:aaaaa11111/*"
            ]
        }
    ]
}
```

------

## 範例 2：授予 GET 方法存取權限的 VPC 端點政策
<a name="apigateway-vpc-endpoint-policies-example-2"></a>

以下範例政策會透過連接政策的 VPC 端點，授予使用者存取特定 API `GET` 方法的權限。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Principal": "*",
            "Action": [
                "execute-api:Invoke"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:execute-api:us-east-1:123412341234:a1b2c3d4e5/stageName/GET/*"
            ]
        }
    ]
}
```

------

## 範例 3：授予某個使用者特定 API 存取權限的 VPC 端點政策
<a name="apigateway-vpc-endpoint-policies-example-3"></a>

以下範例政策會透過連接政策的 VPC 端點，授予某個使用者存取特定 API 的權限。

在此情況下，由於政策限制對特定 IAM 主體的存取，您必須將方法 `authorizationType` 設定為 `AWS_IAM` 或 `NONE`。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Principal": {
                "AWS": [
                    "arn:aws:iam::123412341234:user/MyUser"
                ]
            },
            "Action": [
                "execute-api:Invoke"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:execute-api:us-east-1:123412341234:a1b2c3d4e5/*"
            ]
        }
    ]
}
```

------

## 範例 4：VPC 端點政策授予使用者對於特定自訂網域名稱，和每個映射到網域的 API 存取權
<a name="apigateway-vpc-endpoint-policies-example-4"></a>

以下範例政策會透過連接政策的 VPC 端點，授予使用者存取私有 API 之特定自訂網域名稱的權限。若使用此政策，只要使用者已在 VPC 端點與自訂網域名稱之間建立網域名稱存取關聯，並被授予調用自訂網域名稱和映射至自訂網域名稱的任何私有 API 的存取權，則使用者即可調用映射至此自訂網域名稱的任何 API。如需詳細資訊，請參閱[API Gateway 中的 API 私有 API 的自訂網域名稱](apigateway-private-custom-domains.md)。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "execute-api:Invoke",
      "Resource": [
        "*"
      ],
       "Condition": {
        "ArnEquals": {
          "execute-api:viaDomainArn": "arn:aws:execute-api:us-west-2:111122223333:/domainnames/private.test.com+f4g5h6",
        }
      }
    }
  ]
}
```

------

## 範例 5：授予或拒絕存取特定 API 和網域資源的 VPC 端點政策
<a name="apigateway-vpc-endpoint-policies-example-5"></a>

下列範例政策會授予使用者對特定 API 和網域資源的存取權。若使用此政策，只要使用者已在 VPC 端點與自訂網域名稱之間建立網域名稱存取關聯，並被授予調用自訂網域名稱和映射至自訂網域名稱的任何私有 API 的存取權，使用者即可調用允許的私有 API 和網域資源。如需詳細資訊，請參閱[API Gateway 中的 API 私有 API 的自訂網域名稱](apigateway-private-custom-domains.md)。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "*"
      },
      "Action": "execute-api:Invoke",
      "Resource": [
        "arn:aws:execute-api:us-west-2:111122223333:/domainnames/private.test.com+f4g5h6",
        "arn:aws:execute-api:us-west-2:111122223333:a1b2c3d4e5/*"
      ]
    },
    {
      "Effect": "Deny",
      "Principal": {
        "AWS": "*"
      },
      "Action": "execute-api:Invoke",
      "Resource": [
        "arn:aws:execute-api:us-west-2:111122223333:a1b2c3d4e5/admin/*",
        "arn:aws:execute-api:us-west-2:111122223333:bcd123455/*"
      ]
    }
  ]
}
```

------

## 範例 6：授予或拒絕屬於組織的主體和資源進行存取的 VPC 端點政策
<a name="apigateway-vpc-endpoint-policies-example-6"></a>

下列範例政策會將存取權授予屬於組織的主體和資源。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Condition": {
                "StringEquals": {
                    "aws:ResourceOrgID": "o-abcd1234",
                    "aws:PrincipalOrgID": "o-abcd1234"
                }
            },
            "Action": "*",
            "Resource": "*",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Sid": "AllowRequestsByOrgsIdentitiesToOrgsResources"
        }
    ]
}
```

------