

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

# 在 API Gateway 中設定 REST API 的用量計畫
<a name="api-gateway-create-usage-plans"></a>

建立用量計畫之前，請確認您已設定 API 金鑰。如需詳細資訊，請參閱[在 API Gateway 中設定 REST API 的 API 金鑰](api-gateway-setup-api-keys.md)。

**Topics**
+ [遷移您的 API 到預設用量計劃 (如需要)](#api-gateway-usage-plan-migrate-to-default)
+ [建立用量計劃](#api-gateway-usage-plan-create)
+ [將階段新增至用量計畫](#api-gateway-usage-plan-add-stage)
+ [將 API 階段新增至用量計畫](#api-gateway-usage-plan-add-key)

## 遷移您的 API 到預設用量計劃 (如需要)
<a name="api-gateway-usage-plan-migrate-to-default"></a>

如果您在 2016 年 8 月 11 日推出用量計劃功能「之後」**開始使用 API Gateway，就可以自動在所有支援的區域中啟用用量計劃。

如果您是在該日期之前開始使用 API Gateway，則可能需要遷移到預設用量計劃。在所選區域第一次使用用量計劃之前，系統會提示您 **Enable Usage Plans** (啟用用量計劃) 選項。當您啟用此選項時，您已為與現有 API 金鑰相關聯的每個唯一 API 階段，建立預設的用量計劃。在預設的用量計劃中，一開始並無設定調節和配額限制，而 API 金鑰和 API 階段之間的關聯會複製到用量計劃。API 的行為和以前一樣。不過，您必須使用 [https://docs.aws.amazon.com/apigateway/latest/api/API_UsagePlan.html](https://docs.aws.amazon.com/apigateway/latest/api/API_UsagePlan.html) `apiStages` 屬性建立指定 API 階段值 (`apiId` 和 `stage`) 與內含 API 金鑰 (透過 [https://docs.aws.amazon.com/apigateway/latest/api/API_UsagePlanKey.html](https://docs.aws.amazon.com/apigateway/latest/api/API_UsagePlanKey.html)) 的關聯，而非使用 [ApiKey](https://docs.aws.amazon.com/apigateway/latest/api/API_ApiKey.html) `stageKeys` 屬性。

若要查看您是否已遷移到預設用量計劃，請使用 [https://docs.aws.amazon.com/cli/latest/reference/apigateway/get-account.html](https://docs.aws.amazon.com/cli/latest/reference/apigateway/get-account.html) CLI 命令。在命令輸出之中，當用量計劃已啟用，`features` 清單就會包含 `"UsagePlans"` 項目。

您也可以使用 將 APIs遷移至預設用量計劃 AWS CLI ，如下所示：

**使用 遷移至預設用量計劃 AWS CLI**

1. 呼叫 CLI 命令：[https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-account.html](https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-account.html)。

1. 針對 `cli-input-json` 參數，請使用下列 JSON：

   ```
   [
       {
           "op": "add",
           "path": "/features",
           "value": "UsagePlans"
       }
   ]
   ```

## 建立用量計劃
<a name="api-gateway-usage-plan-create"></a>

下列程序說明如何建立用量計劃。

------
#### [ AWS 管理主控台 ]

**建立用量計劃**

1. 在以下網址登入 API Gateway 主控台：[https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)。

1. 在 API Gateway 主導覽窗格中，選擇**用量計劃**，然後選擇**建立用量計劃**。  
![\[API 用量計劃實體\]](http://docs.aws.amazon.com/zh_tw/apigateway/latest/developerguide/images/api-gateway-new-console-usage-plan-keys-create-setup.png)

1. 針對**名稱**，輸入名稱。

1.  在**描述**，請輸入描述。

1. 根據預設，用量計劃會啟用限流。輸入用量計劃的**費率**和**暴增**。選擇**限流**以關閉限流。

1. 根據預設，用量計劃會針對一段時間啟用一個配額。針對**請求**，輸入使用者在您的用量計劃期間內可提出的請求總數。選擇**配額**以關閉配額。

1. 選擇**建立用量計劃**。

------
#### [ AWS CLI ]

以下 [create-usage-plan](https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-usage-plan.html) 命令會建立用量計畫，此計畫會在月初重設：

```
aws apigateway create-usage-plan \
    --name "New Usage Plan" \
    --description "A new usage plan" \
    --throttle burstLimit=10,rateLimit=5 \
    --quota limit=500,offset=0,period=MONTH
```

------
#### [ REST API ]

呼叫 [https://docs.aws.amazon.com/apigateway/latest/api/API_CreateUsagePlan.html](https://docs.aws.amazon.com/apigateway/latest/api/API_CreateUsagePlan.html) 來建立用量計劃。

------

## 將階段新增至用量計畫
<a name="api-gateway-usage-plan-add-stage"></a>

下列程序說明如何將階段新增至用量計畫。

------
#### [ AWS 管理主控台 ]

**將階段新增至用量計畫**

1. 選取您的用量計劃。

1.  在**關聯的階段**索引標籤下，選擇**新增階段**。  
![\[將 API 階段新增至用量計畫表。\]](http://docs.aws.amazon.com/zh_tw/apigateway/latest/developerguide/images/api-gateway-new-console-usage-plan-keys-create-add-stage.png)

1.  針對 **API**，選取 API。

1.  針對**階段**，選取階段。

1. (選用) 若要開啟方法層級限流，請執行下列操作：

   1. 選擇**方法層級限流**，然後選擇**新增方法**。

   1. 針對**資源**，從 API 中選取資源。

   1. 針對**方法**，從 API 中選取方法。

   1.  輸入用量計劃的**費率**和**暴增**。

1. 選擇**新增至用量計劃**。

------
#### [ AWS CLI ]

以下 [update-usage-plan](https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-usage-plan.html) 命令會將 API 的 `Prod` 階段新增至用量計畫：

```
aws apigateway update-usage-plan \
    --usage-plan-id abc123 \
    --patch-operations op="add",path="/apiStages",value="a1b1c2:Prod"
```

------
#### [ REST API ]

呼叫 [https://docs.aws.amazon.com/apigateway/latest/api/API_UpdateUsagePlan.html](https://docs.aws.amazon.com/apigateway/latest/api/API_UpdateUsagePlan.html) 以更新用量計畫。

------

## 將 API 階段新增至用量計畫
<a name="api-gateway-usage-plan-add-key"></a>

下列程序說明如何將 API 金鑰新增至用量計畫。

------
#### [ AWS 管理主控台 ]

**將金鑰新增至用量計畫**

1. 在**關聯的 API 金鑰**索引標籤下，選擇**新增 API 金鑰**。  
![\[API 用量計劃實體\]](http://docs.aws.amazon.com/zh_tw/apigateway/latest/developerguide/images/api-gateway-new-console-usage-plan-keys-create-add-key.png)

1. 

   1.  若要將現有金鑰與用量計劃建立關聯，請選取**新增現有金鑰**，然後從下拉式選單選取您現有的金鑰。

   1. 若要建立新的 API 金鑰，請選取**建立並新增新金鑰**，然後建立新金鑰。如需如何建立新金鑰的詳細資訊，請參閱 [建立 API 金鑰](api-gateway-setup-api-keys.md#api-gateway-usage-plan-create-apikey)。

1. 選擇**新增 API 金鑰**。

------
#### [ AWS CLI ]

以下 [create-usage-plan-key](https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-usage-plan-key.html) 命令會將現有的 API 金鑰與用量計畫建立關聯：

```
aws apigateway create-usage-plan-key \
    --usage-plan-id a1b2c3 \
    --key-type "API_KEY" \
    --key-id aaa111bbb
```

------
#### [ REST API ]

呼叫 [https://docs.aws.amazon.com/apigateway/latest/api/API_CreateUsagePlanKey.html](https://docs.aws.amazon.com/apigateway/latest/api/API_CreateUsagePlanKey.html)，以將現有的 API 金鑰與用量計畫建立關聯。

您也可以在匯入 API 金鑰時，直接將金鑰與用量計畫建立關聯。呼叫 [https://docs.aws.amazon.com/apigateway/latest/api/API_ImportApiKeys.html](https://docs.aws.amazon.com/apigateway/latest/api/API_ImportApiKeys.html) 將一或多個 API 金鑰直接新增至指定的用量計劃。請求承載應該包含 API 金鑰值、相關聯的用量計劃識別符、指出用量計劃已啟用金鑰的布林值旗標，以及 API 金鑰名稱和說明 (如可能)。

以下 `apikey:import` 請求範例會將三個 API 金鑰 (識別為 `key`、`name` 和 `description`) 新增至一個用量計劃 (識別為 `usageplanIds`)：

```
POST /apikeys?mode=import&format=csv&failonwarnings=fase HTTP/1.1
Host: apigateway.us-east-1.amazonaws.com
Content-Type: text/csv
Authorization: ...

key,name, description, enabled, usageplanIds
abcdef1234ghijklmnop8901234567, importedKey_1, firstone,  tRuE, n371pt 
abcdef1234ghijklmnop0123456789, importedKey_2, secondone, TRUE, n371pt
abcdef1234ghijklmnop9012345678, importedKey_3,          , true, n371pt
```

因此，會在 `UsagePlanKey` 中建立和新增三項 `UsagePlan` 資源。

您也可以用這種方式將 API 金鑰新增至多個用量計劃。若要這樣做，請將每個 `usageplanIds` 欄值變更成包含所選用量計劃識別符的逗號分隔字串，以一對引號 (`"n371pt,m282qs"` 或 `'n371pt,m282qs'`) 括住。

------

**注意**  
API 金鑰可以關聯到多個用量計劃。用量計劃可以關聯到多個階段。不過，特定 API 金鑰只能與每個 API 階段的一個用量計劃相關聯。