

# 使用 AWS CLI 设置具有私有集成的 API Gateway API（旧版）
<a name="set-up-api-with-vpclink-cli"></a>

**注意**  
以下私有集成实现使用 VPC 链接 V1。VPC 链接 V1 是旧版资源。建议您将 [VPC 链接 V2 用于 REST API](apigateway-vpc-links-v2.md)。

以下教程演示了如何使用 AWS CLI 创建 VPC 链接和私有集成。需要满足以下先决条件：
+ 您需要创建并配置了一个以 VPC 源为目标的网络负载均衡器。有关更多信息，请参阅 [为 API Gateway 私有集成设置网络负载均衡器（旧版）](set-up-nlb-for-vpclink-using-console.md)。这必须与您的 API 位于同一个 AWS 账户中。您需要网络负载均衡器 ARN 来创建 VPC 链接。
+ 要创建和管理 `VpcLink`，您必须具有在 API 中创建 `VpcLink` 的权限。您不需要具有使用 `VpcLink` 的权限。有关更多信息，请参阅 [授予 API Gateway 创建 VPC 链接的权限（旧版）](grant-permissions-to-create-vpclink.md)。

**使用 AWS CLI 设置具有私有集成的 API**

1. 使用以下 [create-vpc-link](https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-vpc-link.html) 命令创建以指定的网络负载均衡器作为目标的 `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
   ```

   此命令的输出确认收到请求并显示正在创建的 `VpcLink` 的 `PENDING` 状态。

   ```
   {
       "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`。操作成功完成后，`status` 为 `AVAILABLE`。您可以通过调用以下 [get-vpc-link](https://docs.aws.amazon.com/cli/latest/reference/apigateway/get-vpc-link.html) 命令来验证这一点：

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

   如果操作失败，您将收到 `FAILED` 状态，以及包含错误消息的 `statusMessage`。例如，如果您尝试使用已经与 VPC 端点关联的网络负载均衡器创建 `VpcLink`，在 `statusMessage` 属性上将获得以下内容：

   ```
   "NLB is already associated with another VPC Endpoint Service"
   ```

   在成功创建 `VpcLink` 之后，您可以创建 API 并通过 `VpcLink` 将其与 VPC 资源集成。

   记下新创建的 `VpcLink` 的 `id` 值。在此示例输出中，它是 `gim7c3`。设置私有集成时需要该值。

1. 使用以下 [create-rest-api](https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-rest-api.html) 命令创建 API Gateway [https://docs.aws.amazon.com/apigateway/latest/api/API_RestApi.html](https://docs.aws.amazon.com/apigateway/latest/api/API_RestApi.html) 资源：

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

   请记录返回结果中 `RestApi` 的 `id` 值和 `RestApi` 的 `rootResourceId` 值。您需要该值才能在 API 上进一步执行操作。

   接下来，我们将在根资源（`/`）上创建仅具有 `GET` 方法的 API，并将方法与 `VpcLink` 集成。

1. 使用以下 [put-method](https://docs.aws.amazon.com/cli/latest/reference/apigateway/put-method.html) 命令创建 `GET /` 方法：

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

   如果您不将代理集成用于 `VpcLink`，则还必须至少设置 `200` 状态代码的一个方法响应。您在这里使用代理集成。

1. 创建 `GET /` 方法后，即可设置集成。对于私有集成，请使用 `connection-id` 参数来提供 `VpcLink` ID。您可以使用阶段变量或直接输入 `VpcLink` ID。`uri` 参数不用于将请求路由到端点，而是用于设置 `Host` 标头和证书验证。

------
#### [ Use the VPC link ID ]

   使用以下 [put-integration](https://docs.aws.amazon.com/cli/latest/reference/apigateway/put-integration.html) 命令，在集成中直接使用 `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](https://docs.aws.amazon.com/cli/latest/reference/apigateway/put-integration.html) 命令，来使用阶段变量引用 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](https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-integration.html) 命令更新集成：

   ```
    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 现已准备好，可用于部署和测试运行。

1. 如果您使用了阶段变量来定义 `connection-id`，则需要部署 API 来对其进行测试。使用以下 [create-deployment](https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-deployment.html) 命令，部署具有阶段变量的 API：

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

   要使用不同 `VpcLink` ID（例如 `asf9d7`）更新阶段变量，请使用以下 [update-stage](https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-stage.html) 命令：

   ```
   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 来对其进行测试。在部署 API 之前，使用 [test-invoke-method](https://docs.aws.amazon.com/cli/latest/reference/apigateway/test-invoke-method.html) 命令对其进行测试。

1. 使用以下命令来调用 API：

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

   此外，您可以在 Web 浏览器中输入 API 的调用 URL 来查看结果。