iT邦幫忙

2025 iThome 鐵人賽

DAY 15
0

https://ithelp.ithome.com.tw/upload/images/20250929/20172743qyMEt7TQ5V.png

一、前言

API Gateway 是 Serverless 架構的對外流量入口,若缺乏流量管控,可能導致單一使用者或惡意請求過度消耗資源,進而影響服務穩定性與成本。透過 Usage Plan 搭配 API Key,可以針對不同使用者或應用程式進行流量限制與配額管理,確保系統資源公平分配並提升安全性。

此 Lab 針對「多租戶 API 共用」與「防止濫用」兩大痛點:
(1) 不同使用者對 API 的流量需求不同,若無流量管控,容易造成單一用戶耗盡資源,影響其他用戶體驗。
(2) 雲端資源會依照請求次數計費,缺乏限制將導致成本失控。

在整體 Serverless 架構中,API Gateway Usage Plan 扮演「智慧流量閘門」角色,能針對不同等級的用戶(免費用戶、付費用戶、內部服務)進行差異化流量管理,確保服務穩定且具成本效益。

二、需要使用到的服務

(1) Amazon API Gateway:作為 API 流量入口,負責請求路由、身份驗證與流量限制。
(2) **REST API:**以資源為中心的 API 設計風格,透過標準的 HTTP 方法(GET、POST、PUT、DELETE)對資源進行操作。適合需要穩定結構、明確資源導向、且與多種客戶端(Web、行動裝置、第三方服務)整合的場景。
(3) Usage Plan:設定 API Key 的速率限制與配額,對不同使用者群組提供差異化的使用權限。
(4) API Key:用於識別不同使用者,並套用對應的 Usage Plan。
(5) Amazon CloudWatch:監控 API 請求次數、錯誤比例與限制觸發狀況,協助後續調整流量規則。

三、架構/概念圖

https://ithelp.ithome.com.tw/upload/images/20250929/20172743hnbq3f9sDd.png

四、技術重點

💡「Usage Plan」只能搭配「REST API」來使用,這是AWS本身服務的限制[1],由於HTTP API本身就是設計給公開API的應用,故無法使用「限制API key訪問」的功能。

(1) 根據不同用戶群組(免費/付費/內部服務)建立多個 Usage Plan,並動態調整配額。
(2) API Key 不等於安全憑證,仍需搭配 IAM、Cognito 或 JWT 進行身份驗證。
(3) 結合 CloudWatch Alarm,當流量接近上限時發送警示通知。
(4) 定期審視 Usage Plan 與實際流量,避免過度限制或資源浪費。

五、Lab流程

1️⃣ 前置作業

2️⃣ 主要配置

1. 創建一個API gateway(REST API)

  1. 進去「API Gateway」頁面。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743TpJhnKcw7m.png

  2. 創建一個新的API Gateway。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743eU7VTkgbTS.png

  3. 創建類型選擇REST API。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743ag9uDh6Oor.png

  4. 點選新創、輸入名稱後創建。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743lX0ZqthJJ3.png

2. 設定API Gateway路徑&訪問方法

  1. 新增一個資源路徑。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743yDigkwV2Op.png

  2. 輸入路徑名稱並創建。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743ECaOK2Wc5J.png

  3. 創建訪問方式。
    https://ithelp.ithome.com.tw/upload/images/20250929/201727431Z8QMdyQAf.png

  4. 選擇HTTP方式為「GET」,並選擇「模擬」。(後續需要有自定義動作,可以整合Lambda)
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743Ts2DASkgNH.png

  5. 原路徑也要新增訪問方法。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743F1Bnikmcln.png

  6. 整合項目一樣選擇「模擬」即可。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743V1C8KFF3w1.png

  7. 最終完成畫面。
    https://ithelp.ithome.com.tw/upload/images/20250929/201727433iAUA1aH8s.png

3. 設定API Gateway限制金鑰存取

  1. 在指定的訪問路徑下編輯請求方式。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743z3Zj5qrGHh.png

  2. 啟用「需要API金鑰」。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743mhtDDseBDW.png

4. 設定API Gateway回應訊息

  1. 在「整合回應」分頁點選「編輯」。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743VLp60GSrx3.png

  2. 輸入回應腳本,並儲存。

    • 程式碼範本
      {"message": "訪問成功"}
      

    https://ithelp.ithome.com.tw/upload/images/20250929/20172743JB8upqyO1U.png
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743J0uKbJepNy.png

  3. 部署REST API Gateway。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743zbxYtNBWZx.png

  4. 選擇要部署的階段,並部署。
    https://ithelp.ithome.com.tw/upload/images/20250929/201727435yZsG7OPle.png

  5. 完成後,可以看到你的
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743spEGjy10z6.png

5. 創建API Key

  1. 進入 「API Gateway」頁面。
    https://ithelp.ithome.com.tw/upload/images/20250929/201727439wAyO6z8yw.png

  2. 創建API Key。
    https://ithelp.ithome.com.tw/upload/images/20250929/201727438iixZO1210.png

  3. 命名API Key。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743b0XVEAJCGM.png

  4. 創建完後,將API Key記錄下來。

    • 本次範例

      rAwAN8W1Dk45G3E0VshmvawK5iB2LwLi5s0hi747
      

    https://ithelp.ithome.com.tw/upload/images/20250929/20172743suKhVYCdcj.png

6. 創建用量計畫(Usage plan)

  1. 建立 Usage Plan
    https://ithelp.ithome.com.tw/upload/images/20250929/201727438x37fzuY2j.png

  2. 創建用量計劃,根據需求定義限速、限制用量等。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743JqRHUJrQZy.png

  3. 完成畫面。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743LsfPZ6jkjO.png

7. 將Usage plan關聯API Key及API Gateway

  1. 進到剛剛創建的「用量計畫(usage plan)」中。
    https://ithelp.ithome.com.tw/upload/images/20250929/201727439ZWwfXeIR5.png

  2. 關聯REST API Gateway的階段。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743jRHMogICq4.png

  3. 選擇關聯資訊,並確認。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743cAuka2KNXJ.png

  4. 關聯完成畫面。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743W2nCsjFI65.png

  5. 關聯API Key。
    https://ithelp.ithome.com.tw/upload/images/20250929/201727431JZAayjkqu.png

  6. 選擇稍早創建的API Key。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743LBzX0h5cvO.png

  7. 關聯完成畫面。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743vem3Z5IVqI.png

8.啟用API Gateway上的CloudWatch功能

  1. 進到路徑最高階層。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743o7D4ZyfQSp.png
  2. 選擇日誌類型,並儲存。
    https://ithelp.ithome.com.tw/upload/images/20250929/201727438KfZUVXYHU.png

3️⃣ 測試驗證

1. 使用 curl 或 Postman 測試,嘗試超過流量限制時,確認 API Gateway 回應錯誤

curl -H "x-api-key: <ClientA-Key>" https://<api-id>.execute-api.<region>.amazonaws.com/<階段名稱>/<資源名稱>

https://ithelp.ithome.com.tw/upload/images/20250929/20172743kYhihTnB3S.png

2. 在 CloudWatch 中檢查 API Gateway Metrics,觀察流量限制是否正確生效

  1. 到後台的「儀表板」頁面查看,並點擊進入CloudWatch監控頁面。
    https://ithelp.ithome.com.tw/upload/images/20250929/201727435537YU014u.png

  2. 可以看到API觸發狀況。
    https://ithelp.ithome.com.tw/upload/images/20250929/20172743wBF5au4fM2.png

六、結語

本 Lab 示範了如何透過 API Gateway 的 Usage Plan 與 API Key 進行流量管控,避免資源濫用並保障系統穩定。其核心價值在於 提升安全性、確保多租戶公平使用、降低成本風險。結合監控與告警機制,能讓 Serverless API 架構在高流量環境下依然穩健運行。


上一篇
Day 14 會員資料同步:Cognito x Lambda x DynamoDB Profile 管理
下一篇
Day 16 資料庫效能優化:DynamoDB GSI/LSI 與 Query / Scan 應用
系列文
從一個網站的誕生,看懂 AWS 架構與自動化的全流程!16
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言