設定私有整合 - Amazon API Gateway

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

設定私有整合

若要建立與 Application Load Balancer 或 Network Load Balancer 的私有整合,您可以建立 HTTP 代理整合、指定要使用的 VPC 連結 V2,並提供 Network Load Balancer 或 Application Load Balancer 的 ARN。依預設,私有整合流量會使用 HTTP 通訊協定。若要使用 HTTPS,請指定包含安全伺服器名稱uri的 ,例如 https://example.com:443/test。如需如何使用私有整合建立 REST API 的完整教學課程,請參閱 教學:建立具私有整合的 REST API

建立私有整合

下列程序說明如何使用 VPC 連結 V2 建立連線至負載平衡器的私有整合。

AWS 管理主控台

如需如何建立私有整合的教學課程,請參閱 教學:建立具私有整合的 REST API

AWS CLI

下列 put-integration 命令會使用 VPC 連結 V2 建立連線至負載平衡器的私有整合:

aws apigateway put-integration \ --rest-api-id abcdef123 \ --resource-id aaa000 \ --integration-target 'arn:aws:elasticloadbalancing:us-east-2:111122223333:loadbalancer/app/myLoadBalancerName/1234567891011' \ --uri 'https://example.com:443/path' \ --http-method GET \ --type HTTP_PROXY \ --integration-http-method GET \ --connection-type VPC_LINK \ --connection-id bbb111

您可以改為使用階段變數,而不是直接提供連線 ID。當您將 API 部署到階段時,您可以設定 VPC 連結 V2 ID。下列 put-integration 命令會使用 VPC 連結 V2 ID 的階段變數建立私有整合:

aws apigateway put-integration \ --rest-api-id abcdef123 \ --resource-id aaa000 \ --integration-target 'arn:aws:elasticloadbalancing:us-east-2:111122223333:loadbalancer/app/myLoadBalancerName/1234567891011' \ --uri 'https://example.com:443/path' \ --http-method GET \ --type HTTP_PROXY \ --integration-http-method GET \ --connection-type VPC_LINK \ --connection-id "\${stageVariables.vpcLinkV2Id}"

請務必對階段變數表達式 (${stageVariables.vpcLinkV2Id}) 進行雙引號,並逸出 $ 字元。

OpenAPI

您可以匯入 API OpenAPI 檔案來設定具有私有整合的 API。這些設定類似具有 HTTP 整合之 API 的 OpenAPI 定義,但例外如下:

  • 您必須將 connectionType 明確地設定為 VPC_LINK

  • 您必須將 connectionId 明確地設定為 VpcLinkV2 的 ID 或參考 VpcLinkV2 ID 的階段變數。

  • 私有整合中的 uri 參數指向 VPC 中的 HTTP/HTTPS 端點,但改為用來設定整合請求的 Host 標頭。

  • 具有 VPC 中 HTTPS 端點之私有整合中的 uri 參數用來針對 VPC 端點上所安裝憑證中的網域名稱來驗證所指出的網域名稱。

您可以使用階段變數來參考 VpcLinkV2 ID。或者,您可以將 ID 值直接指派給 connectionId

下列 JSON 格式化 OpenAPI 檔案會顯示 API 的範例,具有階段變數 (${stageVariables.vpcLinkIdV2}) 所參考的 VPC 連結:

{ "swagger": "2.0", "info": { "version": "2017-11-17T04:40:23Z", "title": "MyApiWithVpcLinkV2" }, "host": "abcdef123.execute-api.us-west-2.amazonaws.com", "basePath": "/test", "schemes": [ "https" ], "paths": { "/": { "get": { "produces": [ "application/json" ], "responses": { "200": { "description": "200 response", "schema": { "$ref": "#/definitions/Empty" } } }, "x-amazon-apigateway-integration": { "responses": { "default": { "statusCode": "200" } }, "uri": "https://example.com:443/path", "passthroughBehavior": "when_no_match", "connectionType": "VPC_LINK", "connectionId": "${stageVariables.vpcLinkV2Id}", "integration-target": "arn:aws:elasticloadbalancing:us-east-2:111122223333:loadbalancer/app/myLoadBalancerName/1234567891011", "httpMethod": "GET", "type": "http_proxy" } } } }, "definitions": { "Empty": { "type": "object", "title": "Empty Schema" } } }

更新私有整合

下列範例會更新私有整合的 VPC 連結 V2。

AWS 管理主控台
更新私有整合
  1. 在以下網址登入 API Gateway 主控台:https://console.aws.amazon.com/apigateway

  2. 選擇具有私有整合的 REST API。

  3. 選擇使用私有整合的資源和方法。

  4. 整合請求標籤上,於整合請求設定下,選擇編輯

  5. 您可以編輯私有整合的設定。如果您目前正在使用 VPC 連結 V1,您可以將 VPC 連結變更為 VPC 連結 V2。

  6. 選擇儲存

  7. 重新部署 API 以使變更生效。

AWS CLI

下列 update-integration 命令會更新私有整合,以使用 VPC 連結 V2:

aws apigateway update-integration \ --rest-api-id a1b2c3d4e5 \ --resource-id a1b2c3 \ --http-method GET \ --patch-operations "[{\"op\":\"replace\",\"path\":\"/connectionId\",\"value\":\"pk0000\"}, {\"op\":\"replace\",\"path\":\"/uri\",\"value\":\"http://example.com\"}, {\"op\":\"replace\",\"path\":\"/integrationTarget\",\"value\":\"arn:aws:elasticloadbalancing:us-east-2:111122223333:loadbalancer/app/myLoadBalancerName/1234567891011\"}]"