

# 针对 API Gateway 中的 HTTP API 创建私有集成
<a name="http-api-develop-integrations-private"></a>

私有集成使您能够与 VPC 中的私有资源（如 Application Load Balancer 或基于 Amazon ECS 容器的应用程序）创建 API 集成。

您可以使用私有集成公开 VPC 中的资源，以便 VPC 外部的客户端访问。您可以使用 API Gateway 支持的任何[授权方法](http-api-access-control.md)来控制对 API 的访问。

**注意**  
要创建私有集成，您必须首先创建 VPC 链接。现在，HTTP 和 REST API 都支持 VPC 链接 V2。要了解有关 VPC 链接 V2 的更多信息，请参阅 [在 API Gateway 中设置 VPC 链接 V2](apigateway-vpc-links-v2.md)。

创建 VPC 链接 V2 后，您可以设置私有集成，以连接到应用程序负载均衡器、网络负载均衡器或注册到 AWS Cloud Map 服务的资源。

## 注意事项
<a name="http-api-develop-integrations-private-considerations"></a>

以下注意事项可能会影响您对私有集成的使用：
+ 所有资源必须由同一 AWS 账户拥有。这包括负载均衡器或 AWS Cloud Map 服务、VPC 链接和 HTTP API。
+ 默认情况下，私有集成流量使用 HTTP 协议。要使用 HTTPS，请指定一个 [https://docs.aws.amazon.com/apigatewayv2/latest/api-reference/apis-apiid-integrations-integrationid.html](https://docs.aws.amazon.com/apigatewayv2/latest/api-reference/apis-apiid-integrations-integrationid.html)。要使用 AWS 管理控制台执行此操作，请在创建私有集成时，选择**高级设置**，然后输入安全服务器名称。
+ 对于私有集成，API Gateway 在对后端资源的请求中包括 API 端点的[阶段](http-api-stages.md)部分。例如，对 API 的 `test` 阶段的请求在对私有集成的请求中包含 `test/route-path`。要从对后端资源的请求中删除阶段名，请使用[参数映射](http-api-parameter-mapping.md)覆盖 `$request.path` 的请求路径。

## 使用 Application Load Balancer 或 Network Load Balancer 创建私有集成
<a name="http-api-develop-integrations-private-ELB"></a>

在创建私有集成之前，您必须创建 VPC 链接 V2。要了解有关 VPC 链接 V2 的更多信息，请参阅 [在 API Gateway 中设置 VPC 链接 V2](apigateway-vpc-links-v2.md)。

要创建与 Application Load Balancer 或 Network Load Balancer 的私有集成，请创建 HTTP 代理集成，指定要使用的 VPC 链接，并提供负载均衡器的侦听器 ARN。

以下 [create-integration](https://docs.aws.amazon.com/cli/latest/reference/apigatewayv2/create-integration.html) 命令创建私有集成，该集成通过 VPC 链接连接到负载均衡器。

```
aws apigatewayv2 create-integration --api-id api-id --integration-type HTTP_PROXY \
    --integration-method GET --connection-type VPC_LINK \
    --connection-id VPC-link-ID \
    --integration-uri arn:aws:elasticloadbalancing:us-east-2:123456789012:listener/app/my-load-balancer/50dc6c495c0c9188/0467ef3c8400ae65
    --payload-format-version 1.0
```

## 使用 AWS Cloud Map 服务发现创建私有集成
<a name="http-api-develop-integrations-private-Cloud-Map"></a>

在创建私有集成之前，您必须创建 VPC 链接 V2。要了解有关 VPC 链接的更多信息，请参阅 [在 API Gateway 中设置 VPC 链接 V2](apigateway-vpc-links-v2.md)。

为了与 AWS Cloud Map 集成，API Gateway 使用 `DiscoverInstances` 来识别资源。您可以使用查询参数来定位特定资源。已注册资源的属性必须包括 IP 地址和端口。API Gateway 在从 `DiscoverInstances` 返回的运行状况良好的资源之间分发请求。如需了解详情，请参阅 AWS Cloud Map API 参考中的 [DiscoverInstances](https://docs.aws.amazon.com/cloud-map/latest/api/API_DiscoverInstances.html)。

**注意**  
如果您使用 Amazon ECS 填充 AWS Cloud Map 中的条目，则必须将您的 Amazon ECS 任务配置为通过 Amazon ECS 服务发现使用 SRV 记录，或者开启 Amazon ECS Service Connect。有关更多信息，请参阅《Amazon Elastic Container Service 开发人员指南》中[互连服务](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/interconnecting-services.html)。

要使用 AWS Cloud Map 创建私有集成，请创建 HTTP 代理集成，指定要使用的 VPC 链接，并提供 AWS Cloud Map 服务的 ARN。

以下 [create-integration](https://docs.aws.amazon.com/cli/latest/reference/apigatewayv2/create-integration.html) 命令创建私有集成，该集成通过 AWS Cloud Map 服务发现来识别资源。

```
aws apigatewayv2 create-integration --api-id api-id --integration-type HTTP_PROXY  \
    --integration-method GET --connection-type VPC_LINK \
    --connection-id VPC-link-ID \
    --integration-uri arn:aws:servicediscovery:us-east-2:123456789012:service/srv-id?stage=prod&deployment=green_deployment
    --payload-format-version 1.0
```