

# 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 링크와 프라이빗 통합이 생성되는 방법을 보여줍니다. 다음과 같은 사전 조건을 충족해야 합니다.
+ Network Load Balancer를 생성하고 VPC 소스를 대상으로 구성해야 합니다. 자세한 내용은 [API Gateway 프라이빗 통합을 위한 Network Load Balancer 설정(레거시)](set-up-nlb-for-vpclink-using-console.md) 섹션을 참조하세요. 이는 API와 동일한 AWS 계정에 있어야 합니다. VPC 링크를 생성하려면 Network Load Balancer ARN이 필요합니다.
+ `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) 명령을 사용하면 지정된 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`이 됩니다. 다음 [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
   ```

   또는 API의 간접 호출 URL을 웹 브라우저에서 입력하여 결과를 볼 수 있습니다.