API Gateway - Integrate with Internal Services
繼續整理 Amazon API Gateway 的整合應用:如何使用 API Gateway 透過 NLB (Network Load Balancing) 整合 VPC 內部的服務與資源。
架構
我畫了一張架構圖如下:

其中 Custom Domain Name、Custom Authorizer 在之前有整理過基本概念,本文不贅述。
配置
- 建立 EC2 和 Application
 - 建立 NLB 和 Target Group
 - 在 API Gateway 設定 VPC Links
 - 設定 Integration Request
 
2. 建立 NLB 和 Target Group
這邊要注意的是
- NLB 必須是 
internal,記得放在Private Subnet - 確認 EC2 Security Groups 允許 NLB Subnet 可以連線。
- 注意:EC2 不需要開啟 WAN 的連線
 
 - 確認 Target Group 的 Health Check 狀態是 必須是 
healthy,完成之後,才繼續下一個步驟。 

3. 在 API Gateway 設定 VPC Links
在 Amazon API Gateway 左下角的 VPC Links 設定 NLB。建立之後,要確定 Status 是 Available,如果沒有好,表示 NLB Target Group 的 Health Check 有問題。

要注意的是,API Gateway 預設只能允許建立 5 個 VPC Links (Soft Limit),詳細參閱 Amazon API Gateway Limits
4. 設定 Integration Request
在 API Method 裡面,選擇 Integration Request,指定 VPC Link,如下圖:

接下來這裡是關鍵:
VPC Link: 選擇上一個步驟建立的Endpoint: 指定從Custom Domain Name近來的實際的名稱,例如http://api.rick.com/member/{id}

結論
通常需要 API Gateway 的時候,往往是需要整合既有的服務。實際的案例,也不會馬上導入 Lambda 這種新的技術架構,而是傳統的 EC2 Instance。透過 NLB + API Gateway 有機會讓這些服務可以重新擁有金鐘罩,可以讓既有的服務浴火重生,同時又可以享受 Overview API Gateway 提到的好處。
延伸閱讀
系列文章
- Study Notes - Overview API Gateway
 - Study Notes - Amazon API Gateway
 - API Gateway - Custom Authorizers using Lambda
 - API Gateway - Setup Logging
 - API Gateway - Custom Domain Names
 - API Gateway - Integrate with Internal Services
 - Using API Gateway as DynamoDB Proxy
 - API Gateway - Rate Limit and Throttling
 - API Gateway Private Endpoint
 - Design Async Invocation using API Gateway and SQS
 - 2018/06/28: AWS Summit - 邁向 API 經濟 - API Gateway 導入之旅
 



