使用 AWS CLI (舊版) 設定具有私有整合的 API Gateway API - Amazon API Gateway

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

使用 AWS CLI (舊版) 設定具有私有整合的 API Gateway API

注意

下列私有整合實作會使用 VPC 連結 V1。VPC 連結 V1 是舊版資源。我們建議您將 VPC 連結 V2 用於 REST APIs

下列教學課程說明如何使用 AWS CLI 來建立 VPC 連結和私有整合。需要下列先決條件:

使用 設定具有私有整合的 API AWS CLI
  1. 使用以下 create-vpc-link 命令會建立以指定的 Network Load Balancer 為目標的 VpcLink

    aws apigateway create-vpc-link \ --name my-test-vpc-link \ --target-arns arn:aws:elasticloadbalancing:us-east-2:123456789012:loadbalancer/net/my-vpclink-test-nlb/1234567890abcdef

    此命令的輸出會確認收到請求,並顯示建立中 VpcLinkPENDING 狀態。

    { "status": "PENDING", "targetArns": [ "arn:aws:elasticloadbalancing:us-east-2:123456789012:loadbalancer/net/my-vpclink-test-nlb/1234567890abcdef" ], "id": "gim7c3", "name": "my-test-vpc-link" }

    API Gateway 需要 2-4 分鐘才能完成建立 VpcLink。操作順利完成時,statusAVAILABLE。您可以使用下列 get-vpc-link 命令進行驗證:

    aws apigateway get-vpc-link --vpc-link-id gim7c3

    如果操作失敗,您會取得 FAILED 狀態,以及包含錯誤訊息的 statusMessage。例如,如果您嘗試建立具有已與 VPC 端點建立關聯之 Network Load Balancer 的 VpcLink,則會在 statusMessage 屬性上收到下列訊息:

    "NLB is already associated with another VPC Endpoint Service"

    順利建立 VpcLink 之後,您可以建立 API 並透過 VpcLink 將其與 VPC 資源整合。

    記下新建立之 VpcLinkid 值。在此範例輸出中為 gim7c3。您需要它才能設定私有整合。

  2. 使用以下 create-rest-api 命令建立 API Gateway RestApi 資源:

    aws apigateway create-rest-api --name 'My VPC Link Test'

    請記下所傳回結果中 RestApiid 值,和 RestApirootResourceId 值。您需要此值才能對 API 執行進一步操作。

    接著在根資源 (/) 上建立只有 GET 方法的 API,並整合此方法與 VpcLink

  3. 使用以下 put-method 命令建立 GET / 方法:

    aws apigateway put-method \ --rest-api-id abcdef123 \ --resource-id skpp60rab7 \ --http-method GET \ --authorization-type "NONE"

    如果您未搭配 VpcLink 使用代理整合,至少也必須設定 200 狀態碼的方法回應。在這裡使用代理整合。

  4. 建立 GET / 方法後,您可以設定整合。對於私有整合,您可以使用 connection-id 參數來提供 VpcLink ID。您可以使用階段變數或直接輸入 VpcLink ID。uri 參數不是用於將請求路由至端點,但用於設定 Host 標頭以及進行憑證驗證。

    Use the VPC link ID

    使用以下 put-integration 命令,以直接在整合中使用 VpcLink ID:

    aws apigateway put-integration \ --rest-api-id abcdef123 \ --resource-id skpp60rab7 \ --uri 'http://my-vpclink-test-nlb-1234567890abcdef.us-east-2.amazonaws.com' \ --http-method GET \ --type HTTP_PROXY \ --integration-http-method GET \ --connection-type VPC_LINK \ --connection-id gim7c3
    Use a stage variable

    使用以下 put-integration 命令,以使用階段變數來參考 VPC 連結 ID。當您將 API 部署到階段時,可以設定 VPC 連結 ID。

    aws apigateway put-integration \ --rest-api-id abcdef123 \ --resource-id skpp60rab7 \ --uri 'http://my-vpclink-test-nlb-1234567890abcdef.us-east-2.amazonaws.com' \ --http-method GET \ --type HTTP_PROXY \ --integration-http-method GET \ --connection-type VPC_LINK \ --connection-id "\${stageVariables.vpcLinkId}"

    請務必使用雙引號括住階段變數表達式 (${stageVariables.vpcLinkId}) 並逸出 $ 字元。

    您也可以隨時更新整合,以變更 connection-id。使用以下 update-integration 命令來更新整合:

    aws apigateway update-integration \ --rest-api-id abcdef123 \ --resource-id skpp60rab7 \ --http-method GET \ --patch-operations '[{"op":"replace","path":"/connectionId","value":"${stageVariables.vpcLinkId}"}]'

    請務必使用字串化 JSON 清單做為 patch-operations 參數值。

    因為您使用了私有代理整合,所以 API 現在已準備好進行部署和執行測試。

  5. 如果使用階段變數來定義 connection-id,您需要部署 API 來進行測試。使用以下 create-deployment 命令,以部署有階段變數的 API:

    aws apigateway create-deployment \ --rest-api-id abcdef123 \ --stage-name test \ --variables vpcLinkId=gim7c3

    若要以不同的 VpcLink ID 更新階段變數 (例如 asf9d7),請使用 update-stage 命令:

    aws apigateway update-stage \ --rest-api-id abcdef123 \ --stage-name test \ --patch-operations op=replace,path='/variables/vpcLinkId',value='asf9d7'

    當使用 VpcLink ID 常值硬式編碼 connection-id 屬性時,您不需要部署 API 來進行測試。使用 test-invoke-method 命令,在部署之前先測試您的 API。

  6. 使用下列命令叫用 API:

    curl -X GET https://abcdef123.execute-api.us-east-2.amazonaws.com/test

    或者,您可以在 Web 瀏覽器中輸入 API 的調用 URL 來檢視結果。