AWS CLI 명령을 사용한 엣지 최적화 API 설정
AWS CLI를 사용하여 API를 설정하려면 create-rest-api
, create-resource
또는 get-resources
, put-method
, put-method-response
, put-integration
및 put-integration-response
명령을 사용해야 합니다. 다음 절차에서는 이러한 AWS CLI 명령을 사용하여 HTTP
통합 유형의 간단한 PetStore API를 생성하는 방법을 보여줍니다.
AWS CLI를 이용해 간단한 PetStore API를 생성하려면
-
create-rest-api
명령을 호출하여 특정 리전(RestApi
)에서us-west-2
를 설정합니다.aws apigateway create-rest-api --name 'Simple PetStore (AWS CLI)' --region us-west-2
다음은 이 명령의 출력입니다.
{ "name": "Simple PetStore (AWS CLI)", "id": "vaz7da96z6", "createdDate": 1494572809 }
새로 생성한
id
의 반환된RestApi
를 기록해 둡니다. 이 값은 API의 다른 부분을 설정할 때 필요합니다. -
get-resources
명령을 호출하여RestApi
의 루트 리소스 식별자를 검색합니다.aws apigateway get-resources --rest-api-id vaz7da96z6 --region us-west-2
다음은 이 명령의 출력입니다.
{ "items": [ { "path": "/", "id": "begaltmsm8" } ] }
루트 리소스
Id
를 기록해 둡니다. 이 값은 API의 리소스 트리 설정과 메서드 및 통합 구성을 시작할 때 필요합니다. -
create-resource
명령을 호출하여 루트 리소스(pets
) 아래에 하위 리소스(begaltmsm8
)를 추가합니다.aws apigateway create-resource --rest-api-id vaz7da96z6 \ --region us-west-2 \ --parent-id begaltmsm8 \ --path-part pets
다음은 이 명령의 출력입니다.
{ "path": "/pets", "pathPart": "pets", "id": "6sxz2j", "parentId": "begaltmsm8" }
루트 아래에 하위 리소스를 추가하려면 루트 리소스
Id
를parentId
속성 값으로 지정합니다. 이와 마찬가지로pets
리소스 아래에 하위 리소스를 추가하려면 다음과 같이parent-id
값을pets
의id
리소스6sxz2j
로 교체하면서 이전 단계를 반복합니다.aws apigateway create-resource --rest-api-id vaz7da96z6 \ --region us-west-2 \ --parent-id 6sxz2j \ --path-part '{petId}'
경로 부분을 경로 파라미터로 만들려면 중괄호 {}로 묶습니다. 이 명령이 제대로 실행되면 다음과 같은 응답을 반환합니다.
{ "path": "/pets/{petId}", "pathPart": "{petId}", "id": "rjkmth", "parentId": "6sxz2j" }
/pets
(6sxz2j
) 및/pets/{petId}
(rjkmth
) 두 리소스를 생성했으므로 이제 이 리소스의 메서드를 설정하는 단계로 이동할 수 있습니다. -
put-method
명령을 호출하여GET
리소스에/pets
HTTP 메서드를 추가합니다. 이렇게 하면 오픈 액세스로Method
의 APIGET /pets
가 생성되어/pets
의 ID 값에 따라6sxz2j
리소스를 참조합니다.aws apigateway put-method --rest-api-id vaz7da96z6 \ --resource-id 6sxz2j \ --http-method GET \ --authorization-type "NONE" \ --region us-west-2
다음은 이 명령이 제대로 실행되었을 경우의 출력 결과입니다.
{ "apiKeyRequired": false, "httpMethod": "GET", "authorizationType": "NONE" }
이 메서드는
authorization-type
이NONE
으로 설정되었으므로 오픈 액세스를 위한 것입니다. 인증된 사용자만 메서드를 호출하도록 허용하려면 IAM 역할과 정책, Lambda 권한 부여자(이전에는 사용자 지정 권한 부여자라고 함) 또는 Amazon Cognito 사용자 풀을 사용할 수 있습니다. 자세한 내용은 API Gateway의 REST API에 대한 액세스 제어 및 관리 단원을 참조하세요./pets/{petId}
리소스(rjkmth
)에 대한 읽기 액세스를 허용하려면 다음과 같이 그 리소스에GET
HTTP 메서드를 추가하여Method
의 APIGET /pets/{petId}
를 생성합니다.aws apigateway put-method --rest-api-id vaz7da96z6 \ --resource-id rjkmth --http-method GET \ --authorization-type "NONE" \ --region us-west-2 \ --request-parameters method.request.path.petId=true
다음은 이 명령이 제대로 실행되었을 경우의 출력 결과입니다.
{ "apiKeyRequired": false, "httpMethod": "GET", "authorizationType": "NONE", "requestParameters": { "method.request.path.petId": true } }
petId
의 메서드 요청 경로 파라미터를 필수 요청 파라미터로 지정하여 동적으로 설정된 값이 해당 통합 요청 파라미터에 매핑되고 백엔드로 전달되도록 해야 합니다. -
put-method-response
명령을 호출하여GET /pets
메서드의 200 OK 응답을 설정함으로써/pets
의 ID 값에 따라6sxz2j
리소스를 지정합니다.aws apigateway put-method-response --rest-api-id vaz7da96z6 \ --resource-id 6sxz2j --http-method GET \ --status-code 200 --region us-west-2
다음은 이 명령의 출력입니다.
{ "statusCode": "200" }
이와 마찬가지로
GET /pets/{petId}
메서드의 200 OK 응답을 설정하려면 다음과 같이 하여/pets/{petId}
의 리소스 ID 값에 따라rjkmth
리소스를 지정합니다.aws apigateway put-method-response --rest-api-id vaz7da96z6 \ --resource-id rjkmth --http-method GET \ --status-code 200 --region us-west-2
API에 대해 간단한 클라이언트 인터페이스를 설정한 후에는 백엔드로 API 메서드의 통합을 설정하는 단계로 나아갈 수 있습니다.
-
put-integration
명령을 호출하여Integration
메서드에 지정된 HTTP 엔드포인트로GET /pets
을 설정합니다./pets
리소스는 다음과 같이 리소스 ID6sxz2j
로 식별됩니다.aws apigateway put-integration --rest-api-id vaz7da96z6 \ --resource-id 6sxz2j --http-method GET --type HTTP \ --integration-http-method GET \ --uri 'http://petstore-demo-endpoint.execute-api.com/petstore/pets' \ --region us-west-2
다음은 이 명령의 출력입니다.
{ "httpMethod": "GET", "passthroughBehavior": "WHEN_NO_MATCH", "cacheKeyParameters": [], "type": "HTTP", "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "cacheNamespace": "6sxz2j" }
uri
의 통합http://petstore-demo-endpoint.execute-api.com/petstore/pets
는GET /pets
메서드의 통합 엔드포인트를 지정합니다.이와 마찬가지로 다음과 같이
GET /pets/{petId}
메서드에 대한 통합 요청을 생성합니다.aws apigateway put-integration \ --rest-api-id vaz7da96z6 \ --resource-id rjkmth \ --http-method GET \ --type HTTP \ --integration-http-method GET \ --uri 'http://petstore-demo-endpoint.execute-api.com/petstore/pets/{id}' \ --request-parameters '{"integration.request.path.id":"method.request.path.petId"}' \ --region us-west-2
여기에서 통합 엔드포인트(
uri
의http://petstore-demo-endpoint.execute-api.com/petstore/pets/{id}
) 역시 경로 파라미터(id
)를 사용합니다. 그 값은 상응하는{petId}
의 메서드 요청 경로 파라미터로부터 매핑됩니다. 매핑은request-parameters
의 일부로 정의됩니다. 이 매핑이 여기에 정의되지 않은 경우 클라이언트는 메서드 호출 시 오류 응답을 받습니다.다음은 이 명령의 출력입니다.
{ "passthroughBehavior": "WHEN_NO_MATCH", "cacheKeyParameters": [], "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets/{id}", "httpMethod": "GET", "cacheNamespace": "rjkmth", "type": "HTTP", "requestParameters": { "integration.request.path.id": "method.request.path.petId" } }
-
put-integration-response
명령을 호출하여 HTTP 백엔드와 통합된IntegrationResponse
메서드의GET /pets
를 생성합니다.aws apigateway put-integration-response --rest-api-id vaz7da96z6 \ --resource-id 6sxz2j --http-method GET \ --status-code 200 --selection-pattern "" \ --region us-west-2
다음은 이 명령의 출력입니다.
{ "selectionPattern": "", "statusCode": "200" }
이와 마찬가지로 다음과 같이
put-integration-response
명령을 호출하여IntegrationResponse
메서드의GET /pets/{petId}
를 생성합니다.aws apigateway put-integration-response --rest-api-id vaz7da96z6 \ --resource-id rjkmth --http-method GET --status-code 200 --selection-pattern "" --region us-west-2
이전 단계를 통해 고객이 PetStore 웹 사이트에서 사용 가능한 반려 동물을 쿼리하고 지정된 식별자의 개별 반려 동물을 볼 수 있게 해주는 간단한 API를 설정하는 작업을 마쳤습니다. 고객이 이를 호출할 수 있게 하려면 API를 배포해야 합니다.
-
예를 들어,
stage
를 호출하여 API를create-deployment
단계에 배포합니다.aws apigateway create-deployment --rest-api-id vaz7da96z6 \ --region us-west-2 \ --stage-name test \ --stage-description 'Test stage' \ --description 'First deployment'
브라우저에 https://vaz7da96z6.execute-api.us-west-2.amazonaws.com/test/pets
URL을 입력하고 vaz7da96z6
을 API의 ID로 대체하여 이 API를 테스트할 수 있습니다. 예상 결과는 다음과 같습니다.
[ { "id": 1, "type": "dog", "price": 249.99 }, { "id": 2, "type": "cat", "price": 124.99 }, { "id": 3, "type": "fish", "price": 0.99 } ]
GET /pets/{petId}
메서드를 테스트하려면 브라우저에 https://vaz7da96z6.execute-api.us-west-2.amazonaws.com/test/pets/3
를 입력합니다. 다음과 같은 응답을 받게 됩니다.
{ "id": 3, "type": "fish", "price": 0.99 }