

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

# 使用 AWS CLI （舊版） 設定具有私有整合的 API Gateway API
<a name="set-up-api-with-vpclink-cli"></a>

**注意**  
下列私有整合實作會使用 VPC 連結 V1。VPC 連結 V1 是舊版資源。建議您將 [VPC 連結 V2 用於 REST APIs](apigateway-vpc-links-v2.md)。

下列教學課程說明如何使用 AWS CLI 來建立 VPC 連結和私有整合。需要下列先決條件：
+ 您需要使用 VPC 來源來建立並設定 Network Load Balancer，並將其當成目標。如需詳細資訊，請參閱[設定 API Gateway 私有整合的 Network Load Balancer （舊版）](set-up-nlb-for-vpclink-using-console.md)。這必須與 API AWS 帳戶 位於相同的 中。您需要 Network Load Balancer ARN 才能建立 VPC 連結。
+ 若要建立和管理 `VpcLink`，您需要可在 API 中建立 `VpcLink` 的許可權。使用 `VpcLink` 不需要許可權。如需詳細資訊，請參閱[授予 API Gateway 建立 VPC 連結的許可 （舊版）](grant-permissions-to-create-vpclink.md)。

**使用 設定具有私有整合的 API AWS CLI**

1. 使用以下 [create-vpc-link](https://docs.aws.amazon.com/cli/latest/reference/apigateway/create-vpc-link.html) 命令會建立以指定的 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
   ```

   此命令的輸出會確認收到請求，並顯示建立中 `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 端點建立關聯之 Network Load Balancer 的 `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 來進行測試。使用 [test-invoke-method](https://docs.aws.amazon.com/cli/latest/reference/apigateway/test-invoke-method.html) 命令，在部署之前先測試您的 API。

1. 使用下列命令叫用 API：

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

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