

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

# API Gateway 中 REST API 的私有整合
<a name="private-integration"></a>

使用私有整合公開 Amazon VPC 內的 HTTP/HTTPS 資源，供 VPC 外部的用戶端存取。這會將對私有 VPC 資源的存取延伸到 VPC 邊界之外。您可以使用 API Gateway 支援的任何[授權方法](apigateway-control-access-to-api.md)來控制對您的 API 的存取。

若要建立私有整合，您必須先建立 VPC 連結。API Gateway 支援適用於 REST APIs VPC 連結 V2。VPC 連結 V2 可讓您建立私有整合，將 REST API 連線至 Application Load Balancer，而無需使用 Network Load Balancer。使用 Application Load Balancer 可讓您連線至 Amazon ECSs容器型應用程式和許多其他後端。VPC 連結 V1 被視為舊版整合類型。雖然 API Gateway 支援這些連結，但建議您不要建立新的 VPC 連結 V1。

## 考量事項
<a name="private-integrations-considerations"></a>

下列考量事項可能會影響您使用私有整合：
+ 所有資源都必須由相同的 擁有 AWS 帳戶。這包括負載平衡器、VPC 連結和 REST API。
+ 依預設，私有整合流量會使用 HTTP 通訊協定。若要使用 HTTPS，請指定包含安全伺服器名稱[https://docs.aws.amazon.com/apigateway/latest/api/API_PutIntegration.html#apigw-PutIntegration-request-uri](https://docs.aws.amazon.com/apigateway/latest/api/API_PutIntegration.html#apigw-PutIntegration-request-uri)的 ，例如 `https://example.com:443/test`。
+ 在私有整合中，API Gateway 會在對後端資源的請求中包含 API 端點的[階段](set-up-stages.md)部分。例如，如果您請求 API 的`test`階段，API Gateway 會在私有整合的請求`test/path`中包含 。若要從對後端資源的請求移除階段名稱，請使用[參數映射](rest-api-parameter-mapping.md)來建立`$context.requestOverride.path`變數的覆寫。
+  AWS Cloud Map 不支援與 的私有整合。

**Topics**
+ [考量事項](#private-integrations-considerations)
+ [在 API Gateway 中設定 VPC 連結 V2](apigateway-vpc-links-v2.md)
+ [設定私有整合](set-up-private-integration.md)
+ [使用 VPC 連結 V1 的私有整合 （舊版）](vpc-links-v1.md)

# 在 API Gateway 中設定 VPC 連結 V2
<a name="apigateway-vpc-links-v2"></a>

VPC 連結可讓您建立私有整合，將 API 路由連線至 VPC 中的私有資源，例如 Application Load Balancer 或 Amazon ECS 容器型應用程式。私有整合使用 VPC 連結 V2 封裝 API Gateway 與目標 VPC 資源之間的連線。您可以重複使用不同資源和 APIs VPC 連結。

當您建立 VPC 連結時，API Gateway 會建立和管理您帳戶中 VPC 連結 V2 的[彈性網路介面](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-eni.html)。此程序需要幾分鐘的時間。當 VPC 連結 V2 可供使用時，其狀態會從 轉換為 `PENDING` `AVAILABLE`。

**注意**  
如果 60 天內沒有透過 VPC 連結傳送流量，則會變成 `INACTIVE`。當 VPC 連結處於 `INACTIVE` 狀態時，API Gateway 會刪除所有 VPC 連結的網路介面。這會導致依賴 VPC 連結的 API 請求失敗。如果 API 請求繼續，API Gateway 會重新佈建網路介面。建立網路介面並重新啟用 VPC 連結可能需要幾分鐘的時間。您可以使用 VPC 連結狀態來監控 VPC 連結的狀態。

## 使用 建立 VPC 連結 V2 AWS CLI
<a name="apigateway-vpc-links-v2-create"></a>

若要建立 VPC 連結 V2，所有涉及的資源都必須由相同的 AWS 帳戶擁有。以下 [create-vpc-link](https://docs.aws.amazon.com/cli/latest/reference/apigatewayv2/create-vpc-link.html) 命令會建立 VPC 連結：

```
aws apigatewayv2 create-vpc-link --name MyVpcLink \
    --subnet-ids subnet-aaaa subnet-bbbb \
    --security-group-ids sg1234 sg5678
```

**注意**  
VPC 連結 V2 是不可變的。建立 VPC 連結 V2 之後，您無法變更其子網路或安全群組。

## 使用 刪除 VPC 連結 V2 AWS CLI
<a name="apigateway-vpc-links-v2-delete"></a>

以下 [delete-vpc-link](https://docs.aws.amazon.com/cli/latest/reference/apigatewayv2/delete-vpc-link.html) 命令會刪除 VPC 連結。

```
aws apigatewayv2 delete-vpc-link --vpc-link-id abcd123
```

## 各個區域的供應情形
<a name="apigateway-vpc-links-v2-availability"></a>

下列區域和可用區域支援 VPC 連結 V2：

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/apigateway/latest/developerguide/apigateway-vpc-links-v2.html)

# 設定私有整合
<a name="set-up-private-integration"></a>

若要建立與 Application Load Balancer 或 Network Load Balancer 的私有整合，您可以建立 HTTP 代理整合、指定要使用的 [VPC 連結 V2](apigateway-vpc-links-v2.md)，並提供 Network Load Balancer 或 Application Load Balancer 的 ARN。依預設，私有整合流量會使用 HTTP 通訊協定。若要使用 HTTPS，請指定包含安全伺服器名稱[https://docs.aws.amazon.com/apigateway/latest/api/API_PutIntegration.html#apigw-PutIntegration-request-uri](https://docs.aws.amazon.com/apigateway/latest/api/API_PutIntegration.html#apigw-PutIntegration-request-uri)的 ，例如 `https://example.com:443/test`。如需如何使用私有整合建立 REST API 的完整教學課程，請參閱 [教學：建立具私有整合的 REST API](getting-started-with-private-integration.md)。

## 建立私有整合
<a name="set-up-private-integration-create"></a>

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

------
#### [ AWS 管理主控台 ]

如需如何建立私有整合的教學課程，請參閱 [教學：建立具私有整合的 REST API](getting-started-with-private-integration.md)。

------
#### [ AWS CLI ]

下列 [put-integration](https://docs.aws.amazon.com/cli/latest/reference/latest/api/API_PutIntegration.html) 命令會使用 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](https://docs.aws.amazon.com/cli/latest/reference/latest/api/API_PutIntegration.html) 命令會使用 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}"
```

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

------
#### [ 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"
    }
  }
}
```

------

## 更新私有整合
<a name="set-up-private-integration-update"></a>

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

------
#### [ AWS 管理主控台 ]

**更新私有整合**

1. 在以下網址登入 API Gateway 主控台：[https://console.aws.amazon.com/apigateway](https://console.aws.amazon.com/apigateway)。

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

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

1. 在**整合請求**標籤上，於**整合請求設定**下，選擇**編輯**。

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

1. 選擇**儲存**。

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

------
#### [ AWS CLI ]

下列 [update-integration](https://docs.aws.amazon.com/cli/latest/reference/latest/api/API_PutIntegration.html) 命令會更新私有整合，以使用 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\"}]"
```

------

# 使用 VPC 連結 V1 的私有整合 （舊版）
<a name="vpc-links-v1"></a>

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

若要建立私有整合，您必須先建立 Network Load Balancer。您的 Network Load Balancer 必須有一個[接聽程式](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-listeners.html)，將請求路由至 VPC 中的資源。為了改善 API 可用性，請確定您的 Network Load Balancer 可將流量路由至 AWS 區域中一個以上可用區域內的資源。然後，您會建立用來連線 API 和 Network Load Balancer 的 VPC 連結。建立 VPC 連結後，您可以建立私有整合，以透過 VPC 連結和 Network Load Balancer 將流量從 API 路由到 VPC 中的資源。Network Load Balancer 和 API 必須擁有相同的 AWS 帳戶。

**Topics**
+ [設定 API Gateway 私有整合的 Network Load Balancer （舊版）](set-up-nlb-for-vpclink-using-console.md)
+ [授予 API Gateway 建立 VPC 連結的許可 （舊版）](grant-permissions-to-create-vpclink.md)
+ [使用 AWS CLI （舊版） 設定具有私有整合的 API Gateway API](set-up-api-with-vpclink-cli.md)
+ [用於私有整合的 API Gateway 帳戶 （舊版）](set-up-api-with-vpclink-accounts.md)

# 設定 API Gateway 私有整合的 Network Load Balancer （舊版）
<a name="set-up-nlb-for-vpclink-using-console"></a>

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

 下列程序概述使用 Amazon EC2 主控台設定 API Gateway 私有整合之 Network Load Balancer (NLB) 的步驟，並提供每個步驟之詳細說明的參考。

對於每個 VPC 中的資源，您只需要設定一個 NLB 和一個 VPCLink。每個 NLB 皆支援多個[接聽程式](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-listeners.html)和[目標群組](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-target-groups.html)。您可以設定每個服務為 NLB 上特定的接聽程式，使用單一 VPCLink 連接到 NLB。當您在 API Gateway 中建立私有整合時，將使用指定到每個服務的特定連接埠定義每個服務。如需詳細資訊，請參閱[教學：建立具私有整合的 REST API](getting-started-with-private-integration.md)。Network Load Balancer 和 API 必須擁有相同的 AWS 帳戶。

**使用 API Gateway 主控台建立私有整合的 Network Load Balancer**

1. 登入 AWS 管理主控台 ，並在 [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/)：// 開啟 Amazon EC2 主控台。

1. 在 Amazon EC2 執行個體上設定 Web 伺服器。如需範例設定，請參閱[在 Amazon Linux 2 上安裝 LAMP Web Server](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-lamp-amazon-linux-2.html)。

1. 建立 Network Load Balancer、註冊具有目標群組的 EC2 執行個體，並將目標群組新增至 Network Load Balancer 接聽程式。如需詳細資訊，請參閱 [Network Load Balancer 入門](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/network-load-balancer-getting-started.html)中的指示。

1. 建立 Network Load Balancer 之後，執行下列操作：

   1.  記下 Network Load Balancer 的 ARN。您需要它才能在 API Gateway 中建立 VPC 連結，以整合 API 與受 Network Load Balancer 保護的 VPC 資源。

   1.  針對 PrivateLink 關閉安全群組評估。
      + 若要使用主控台關閉 PrivateLink 流量的安全群組評估，您可以選擇**安全**標籤，然後選擇**編輯**。在**安全設定**中，清除**在 PrivateLink 流量強制執行輸入規則**。
      + 使用以下 [set-security-groups](https://docs.aws.amazon.com/cli/latest/reference/elbv2/set-security-groups.html) 命令關閉 PrivateLink 流量的安全群組評估：

        ```
        aws elbv2 set-security-groups --load-balancer-arn arn:aws:elasticloadbalancing:us-east-2:111122223333:loadbalancer/net/my-loadbalancer/abc12345 \
          --security-groups sg-123345a --enforce-security-group-inbound-rules-on-private-link-traffic off
        ```

**注意**  
請勿將任何相依性新增至 API Gateway CIDR，因為這些相依性必然會在未通知的情況下變更。

# 授予 API Gateway 建立 VPC 連結的許可 （舊版）
<a name="grant-permissions-to-create-vpclink"></a>

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

若要讓您或您帳戶中的使用者建立和維護 VPC 連結，您或使用者必須有權建立、刪除和檢視 VPC 端點服務組態、變更 VPC 端點服務許可，以及檢查負載平衡器。若要授予這類許可，請使用下列步驟。

**授予建立、更新和刪除 VPC 連結的許可**

1. 建立與下列類似的 IAM 政策：

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Action": [
                   "apigateway:POST",
                   "apigateway:GET",
                   "apigateway:PATCH",
                   "apigateway:DELETE"
               ],
               "Resource": [
                   "arn:aws:apigateway:us-east-1::/vpclinks",
                   "arn:aws:apigateway:us-east-1::/vpclinks/*"
               ]
           },
           {
               "Effect": "Allow",
               "Action": [
                   "elasticloadbalancing:DescribeLoadBalancers"
               ],
               "Resource": "*"
           },
           {
               "Effect": "Allow",
               "Action": [
                   "ec2:CreateVpcEndpointServiceConfiguration",
                   "ec2:DeleteVpcEndpointServiceConfigurations",
                   "ec2:DescribeVpcEndpointServiceConfigurations",
                   "ec2:ModifyVpcEndpointServicePermissions"
               ],
               "Resource": "*"
           }
       ]
   }
   ```

------

   如果您想要啟用 VPC 連結的標記，請務必允許標記操作。如需詳細資訊，請參閱[允許標記操作](apigateway-tagging-iam-policy.md#allow-tagging)。

1. 建立或選擇 IAM 角色，並將先前的政策連接至角色。

1. 將 IAM 角色指派給您或您帳戶中建立 VPC 連結的使用者。

# 使用 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 來檢視結果。

# 用於私有整合的 API Gateway 帳戶 （舊版）
<a name="set-up-api-with-vpclink-accounts"></a>

當您建立 `VpcLink` 時，下列特定區域 API Gateway 帳戶 ID 也會作為 `AllowedPrincipals` 自動新增至 VPC 端點服務。


| **區域** | **帳戶 ID** | 
| --- | --- | 
| us-east-1 | 392220576650 | 
| us-east-2 | 718770453195 | 
| us-west-1 | 968246515281 | 
| us-west-2 | 109351309407 | 
| ca-central-1 | 796887884028 | 
| eu-west-1 | 631144002099 | 
| eu-west-2 | 544388816663 | 
| eu-west-3 | 061510835048 | 
| eu-central-1 | 474240146802 | 
| eu-central-2 | 166639821150 | 
| eu-north-1 | 394634713161 | 
| eu-south-1 | 753362059629 | 
| eu-south-2 | 359345898052 | 
| ap-northeast-1 | 969236854626 | 
| ap-northeast-2 | 020402002396 | 
| ap-northeast-3 | 360671645888 | 
| ap-southeast-1 | 195145609632 | 
| ap-southeast-2 | 798376113853 | 
| ap-southeast-3 | 652364314486 | 
| ap-southeast-4 | 849137399833 | 
| ap-south-1 | 507069717855 | 
| ap-south-2 | 644042651268 | 
| ap-east-1 | 174803364771 | 
| sa-east-1 | 287228555773 | 
| me-south-1 | 855739686837 | 
| me-central-1 | 614065512851 | 