设置私有集成 - Amazon API Gateway
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

设置私有集成

要创建与应用程序负载均衡器或网络负载均衡器的私有集成,请创建 HTTP 代理集成,指定要使用的 VPC 链接 V2,并提供网络负载均衡器或应用程序负载均衡器的 ARN。默认情况下,私有集成流量使用 HTTP 协议。要使用 HTTPS,请指定包含安全服务器名称的 uri,例如 https://example.com:443/test。有关如何创建使用私有集成的 REST API 的完整教程,请参阅教程:利用私有集成创建 REST API

创建私有集成

以下步骤介绍如何创建私有集成,该私有集成通过 VPC 链接 V2 连接到负载均衡器。

Amazon Web Services 管理控制台

有关如何创建私有集成的教程,请参阅教程:利用私有集成创建 REST API

Amazon 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 具有 VPC 链接,通过阶段变量 (${stageVariables.vpcLinkIdV2}) 来引用:

{ "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。

Amazon Web Services 管理控制台
更新私有集成
  1. 通过以下网址登录到 Amazon API Gateway 控制台:https://console.aws.amazon.com/apigateway

  2. 选择使用私有集成的 REST API。

  3. 选择使用私有集成的资源和方法。

  4. 集成请求选项卡的集成请求设置下,选择编辑

  5. 您可以编辑私有集成的设置。如果您当前使用的是 VPC 链接 V1,则可以将您的 VPC 链接更改为 VPC 链接 V2。

  6. 选择保存

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

Amazon 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\"}]"