$30 off During Our Annual Pro Sale. View Details »

邁向 API 經濟 - API Gateway 導入之旅

邁向 API 經濟 - API Gateway 導入之旅

Avatar for Rick Hwang

Rick Hwang

July 18, 2018
Tweet

More Decks by Rick Hwang

Other Decks in Education

Transcript

  1. Rick Hwang Sr. Manager, 91APP July 28, 2018 1 邁向

    API 經濟 API Gateway 導入之旅
  2. 田野調查 • 現場有多少朋友看過 Rick 的 Blog? • 有多少朋友有在 AWS Summit

    聽過 Rick 分享? • 有多少朋友公司 Production,已經在使用 API Gateway? 5
  3. • 打雜 @ 91APP • 經營管理 • Cloud / AWS

    / GCP • DevOps / SRE • Distributed Systems • 音樂 吉他 鍵盤 編曲 • 哲學 科幻 金庸 喇賽 Rick Hwang https://www.gtcafe.com 6
  4. 7

  5. 11

  6. 防禦機制不健全 • 用資源 (加機器) 來支撐異常攻擊 ◦ 用 NACL 暫解 ◦

    換 ALB + WAF 可以解 • API 管控機制不健全 ◦ 不同客戶的 KeySet,無法個別管控權限 ◦ 無法依據 Key 鎖定不同來源客戶 IP • 無法限制流量 (Rate Limit) 17
  7. • 客戶使用我們的 API ◦ 客戶的夥伴也使用我們的 API,我們不知道! ◦ 權限管控問題 • 有些客戶不吃

    JSON、吃 XML • 或者,客戶丟過來的是 XML,我們吃 JSON 外部整合力的問題 20
  8. • Serverless Framework • CloudFront / WAF • API Gateway:

    Private and Regional • Lambda • DynamoDB / DAX • CloudWatch 26 Serverless Architecture
  9. • 即時 API 使用率統計 • API Key 管理 • API

    授權機制 • 監控指標 • Log 機制 28 維運自動化
  10. 1. 更好的認證 (Authentication)、授權 (Authorization) 機制 2. Request & Response 資料結構轉換

    3. 更好的監控機制、API 使用率 4. 控速、防火牆 5. 標準化 API 介面 (RESTful) 6. 簡化既有認證機制 7. 簡化 Request Payload 8. 調整 Response Data Model 9. 後端盡量不用改 10. Serverless Architecture 30 問題與期望
  11. 控管:控速、防火牆 API Key 管理 認證機制 即時監控 後端盡量不用改 新舊版本並行 簡化 Payload

    處理回傳資料結構 標準化 API 介面 API 文件化 外部 Developer Portal 內部文件發布流程標準化 資安、架構、維運 介面拉皮 敵動我不動 開發流程 31
  12. • Public ◦ Edge - w/ CDN, Global ◦ Regional

    w/o CDN • Private ◦ Regional w/o CDN 34 考慮一:架構可視性 開放給 Business User 開放內部服務對接 內部服務對串 注意:使用 Custom Domain Name 不要用 Edge
  13. • Serverless 架構 • API swagger 整合與管理 • 測試環境規劃 •

    文件 • 部署流程 39 考慮五:開發流程
  14. • Security Built-in: CDN + WAF • Availability and Reliability

    • 環境建置 • 部署流程 42 架構的考量
  15. 44 Distribution https://api.abc.com WAF SSL Certificate DNS API Gateway CDN

    Stages /order API Stage POST /v2/Sales/Order POST /v2/Sales/Orders Custom Authorizer DynamoDB Handler Backend Service Network Load Balancer 1. EncryptData 2. InvokeAPI 3. Restructure Data Formation Third Party Application Load Balancer 客戶 Service A
  16. Service C Service A 客戶 (Prod) HTTPS / TLS x-api-key

    HTTPS HTTPS API Gateway Service B HTTPS CDN 用戶 WAF API Endpoint /ServiceName /Version/RestAPI 客戶 (Dev) HTTPS / TLS x-api-key 1. Rate Limit 2. Access Control 3. Monitoring 4. Usage Plan API Gateway API Gateway 46 Service A, B, C ...
  17. 48 Service A Rest API Rest API Service B Rest

    API Rest API ServiceD Rest API Rest API ServiceC Rest API Rest API CloudFront AWS WAF Route 53 ALB Client: Desktop / Mobile S3 Private Public Protected Access Control Public Subnet Private Subnet Public Network /category /order /auth /theme js, css, img Message Broker Common Services Rest API Service Discovery
  18. • API Gateway / Service Mesh • Service Discovery •

    Message Broker 49 微服務的核心微服務
  19. • 月繳 1999 • 每月五千萬呼叫次 • API 限速:5000rps • 50

    把 x-api-key + MORE + 52 API 經濟學 - Usage Plan • 月繳 1499 • 每月無上限使用次數 • API 限速:無限 • 100 把 x-api-key + MORE + 91APP 鑽石客戶 多種優惠 無上限吃到飽 91APP VIP 客戶 多種優惠 精選用到爽 • 月繳 699 • 每月一千萬呼叫次 • API 限速:1000rps • 10 把 x-api-key + MORE + 91APP 經濟客戶 多種選擇 經濟又實惠
  20. • Key 1 • Key 2 • Key 3 •

    Key 4 • … • Key n • Rate Limit: 5000 • Throttling: 500 • Quota: 50,000 / day 53 Usage Plan A API Keys API Stage A API Stage B 0..n 0..n 499 方案! Simcard 基地台
  21. Usage Plan 的應用 • 給客戶測試的 Key ◦ Rate Limit: 調降

    ◦ Quote: 有上限 ◦ 定時 Disable or Rotate • 給客戶正式的 Key ◦ 給兩把:備援的概念 → IAM Access Key ◦ Rate Limit: 依照業務等級 ◦ Quote: 無上限 ◦ 定時 Disable or Rotate 55
  22. Developer Site - 基本款 • Getting Start • User Guide,

    Sample Code • Developer Guide • API Reference • Blog • Forum 58
  23. 63 • Custom Domain Name 不是有 CloudFront,為什麼要自 己弄? • 後端回

    JSON,客戶收 XML,這是怎麼回事? • 如何 Debug API Gateway 的問題? • API Gateway 可以串其他 AWS Services? 像是 DdynamoDB,而不需要 Lambda? • Rate Limit 要注意的地方? Questions
  24. ~$ nslookup 7fal10lwzj.execute-api.us-west-2.amazonaws.com 8.8.8.8 Non-authoritative answer: Name: 7fal10lwzj.execute-api.us-west-2.amazonaws.com Address: 52.84.205.154

    Name: 7fal10lwzj.execute-api.us-west-2.amazonaws.com Address: 52.84.205.177 Name: 7fal10lwzj.execute-api.us-west-2.amazonaws.com Address: 52.84.205.168 Name: 7fal10lwzj.execute-api.us-west-2.amazonaws.com Address: 52.84.205.155 ~$ nslookup 52.84.205.154 8.8.8.8 Non-authoritative answer: 154.205.84.52.in-addr.arpa name = server-52-84-205-154.tpe50.r.cloudfront.net. Authoritative answers can be found from: 66 Edge API Gateway
  25. ~$ nslookup 2dev1cgrqj.execute-api.us-west-2.amazonaws.com 8.8.8.8 Non-authoritative answer: Name: 2dev1cgrqj.execute-api.us-west-2.amazonaws.com Address: 34.218.11.31

    Name: 2dev1cgrqj.execute-api.us-west-2.amazonaws.com Address: 52.10.38.34 Name: 2dev1cgrqj.execute-api.us-west-2.amazonaws.com Address: 52.88.144.89 ~$ nslookup 34.218.11.31 8.8.8.8 Non-authoritative answer: 31.11.218.34.in-addr.arpa name = ec2-34-218-11-31.us-west-2.compute.amazonaws.com. Authoritative answers can be found from: 67 Regional API Gateway
  26. 68 Private API Gateway ~$ nslookup wertv1jfp2.execute-api.us-west-2.amazonaws.com 8.8.8.8 ** server

    can't find wertv1jfp2.execute-api.us-west-2.amazonaws.com: NXDOMAIN ~$ nslookup wertv1jfp2.execute-api.us-west-2.amazonaws.com Server: 172.31.0.2 Address: 172.31.0.2#53 Non-authoritative answer: wertv1jfp2.execute-api.us-west-2.amazonaws.com canonical name = execute-api.us-west-2.amazonaws.com. Name: execute-api.us-west-2.amazonaws.com Address: 172.31.5.61 Name: execute-api.us-west-2.amazonaws.com Address: 172.31.10.251
  27. Service C Service A 客戶 (Prod) HTTPS / TLS x-api-key

    HTTPS HTTPS API Gateway Service B HTTPS CDN 用戶 WAF 客戶 (Dev) HTTPS / TLS x-api-key API Gateway API Gateway 69 Service A, B, C ...
  28. • CloudWatch Logs • API Logging for Stage • Custom

    Access Logging (Stage) 74 API Gateway Log 分類
  29. 75

  30. 76

  31. 78

  32. 79

  33. 80

  34. 82 • Region 的總量是固定的 • 單位時間的最大值 • 可以控制到每一個 API •

    Rate Limit 可以調整,但是 Burst 固定最大 5000 Rate Limit
  35. 83