

# AWS CLI を使用してプライベート統合で API Gateway API をセットアップする (レガシー)
<a name="set-up-api-with-vpclink-cli"></a>

**注記**  
プライベート統合の次の実装では、VPC リンク V1 を使用します。VPC リンク V1 はレガシーリソースです。[REST API には VPC リンク V2](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 アカウントに存在する必要があります。VPC リンクを作成するには、Network Load Balancer ARN が必要です。
+ `VpcLink` を作成および管理するには、API で `VpcLink` を作成するためのアクセス許可が必要です。`VpcLink` を使用するアクセス許可は必要ありません。詳細については、「[VPC リンクを作成するためのアクセス許可を API Gateway に付与する (レガシー)](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) コマンドを使用して、指定した 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 が `VpcLink` の作成を完了するまでに 2〜4 分かかります。操作が正常に終了すると、`status` は `AVAILABLE` になります。作成した VPC リンクを確認するには、次の [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
   ```

   `asf9d7` などの別の `VpcLink` ID でステージ変数を更新するには、次の [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
   ```

   または、ウェブブラウザに API の invoke-URL を入力して結果を表示することもできます。