

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

# API Gateway 資源政策範例
<a name="apigateway-resource-policies-examples"></a>

本頁面顯示 API Gateway 資源政策之一般使用案例的一些範例。

下列範例政策使用簡化的語法來指定 API 資源。這種簡化的語法是您可以參考 API 資源的縮寫方式，而不是指定完整的 Amazon Resource Name (ARN)。當您儲存政策時，API Gateway 會將縮寫的語法轉換為完整的 ARN。例如，您可以在資源政策中指定資源 `execute-api:/stage-name/GET/pets`。當您儲存資源政策時，API Gateway 會將資源轉換為 `arn:aws:execute-api:us-east-2:123456789012:aabbccddee/stage-name/GET/pets`。API Gateway 會使用目前的區域、 AWS 您的帳戶 ID 和與資源政策相關聯的 REST API ID 來建置完整的 ARN。您可以使用 `execute-api:/*` 來表示目前 API 中的所有階段、方法和路徑。如需存取原則語言的詳細資訊，請參閱 [Amazon API Gateway 的存取原則語言概觀](apigateway-control-access-policy-language-overview.md)。

**Topics**
+ [

## 範例：允許另一個 AWS 帳戶中的角色使用 API
](#apigateway-resource-policies-cross-account-example)
+ [

## 範例：拒絕根據來源 IP 地址或範圍的 API 流量
](#apigateway-resource-policies-source-ip-address-example)
+ [

## 範例：使用私有 API 時，根據來源 IP 位址或範圍拒絕 API 流量
](#apigateway-resource-policies-source-ip-address-vpc-example)
+ [

## 範例：允許以來源 VPC 或 VPC 端點為依據的私有 API 流量
](#apigateway-resource-policies-source-vpc-example)

## 範例：允許另一個 AWS 帳戶中的角色使用 API
<a name="apigateway-resource-policies-cross-account-example"></a>

下列範例資源政策會透過 [Signature 第 4 ](https://docs.aws.amazon.com/IAM/latest/UserGuide/create-signed-request.html)版 (SigV4) 或 [Signature 第 4a 版](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_sigv.html#how-sigv4a-works) (SigV4a) 通訊協定，將一個 AWS 帳戶中的 API 存取權授予不同 AWS 帳戶中的兩個角色。具體而言， 所識別 AWS 帳戶的開發人員和管理員角色`account-id-2`會獲授予 `execute-api:Invoke`動作，以對您 AWS 帳戶中`pets`的資源 (API) 執行 `GET`動作。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::111122223333:role/developer",
                    "arn:aws:iam::111122223333:role/Admin"
                ]
            },
            "Action": "execute-api:Invoke",
            "Resource": [
                "execute-api:/stage/GET/pets"
            ]
        }
    ]
}
```

------

## 範例：拒絕根據來源 IP 地址或範圍的 API 流量
<a name="apigateway-resource-policies-source-ip-address-example"></a>

以下範例資源政策會拒絕 (封鎖) 從兩個指定的來源 IP 位址區塊傳入 API 流量。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": [
                "execute-api:/*"
            ]
        },
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": [
               "execute-api:/*"
            ],
            "Condition" : {
                "IpAddress": {
                    "aws:SourceIp": ["192.0.2.0/24", "198.51.100.0/24" ]
                }
            }
        }
    ]
}
```

------

如果您使用任何 IAM 使用者政策或 API Gateway 資源政策來控制 API Gateway 或任何 API Gateway API 的存取權，請確認您的政策已更新並包含 IPv6 位址範圍。若政策未更新，因而無法處理 IPv6 位址，則可能在用戶端開始使用雙堆疊端點時，影響用戶端對 API Gateway 的存取。如需詳細資訊，請參閱[在 IAM 原則中使用 IPv6 地址](api-ref.md#api-reference-service-endpoints-dualstack-iam)。

## 範例：使用私有 API 時，根據來源 IP 位址或範圍拒絕 API 流量
<a name="apigateway-resource-policies-source-ip-address-vpc-example"></a>

以下範例資源政策會拒絕 (封鎖) 從兩個指定的來源 IP 位址區塊傳入 API 流量。使用私有 API 時，`execute-api` 的 VPC 端點會重新寫入原始來源 IP 位址。`aws:VpcSourceIp` 條件會根據原始請求者 IP 位址篩選請求。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": [
                "execute-api:/*"
            ]
        },
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": [
               "execute-api:/*"
            ],
            "Condition" : {
                "IpAddress": {
                    "aws:VpcSourceIp": ["192.0.2.0/24", "198.51.100.0/24"]
                }
            }
        }
    ]
}
```

------

## 範例：允許以來源 VPC 或 VPC 端點為依據的私有 API 流量
<a name="apigateway-resource-policies-source-vpc-example"></a>

以下範例資源政策僅允許來自指定的 Virtual Private Cloud (VPC) 或 VPC 端點的流量傳入私有 API。

此範例資源政策會指定來源 VPC：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": [
                "execute-api:/*"
            ]
        },
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": [
                "execute-api:/*"
            ],
            "Condition" : {
                "StringNotEquals": {
                   "aws:SourceVpc": "vpc-1a2b3c4d"
                }
            }
        }
    ]
}
```

------

此範例資源政策會指定來源 VPC 端點：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": [
                "execute-api:/*"
            ]
        },
        {
            "Effect": "Deny",
            "Principal": "*",
            "Action": "execute-api:Invoke",
            "Resource": [
                "execute-api:/*"
            ],
            "Condition" : {
                "StringNotEquals": {
                    "aws:SourceVpce": "vpce-1a2b3c4d"
                }
            }
        }
    ]
}
```

------