기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
Node.js용 AWS SDK를 사용한 엣지 최적화 API 설정
그 예로서 AWS SDK를 사용해 API Gateway API를 생성하는 방법을 설명하기 위해 Node.js용 AWS SDK를 사용합니다. 개발
환경 설정 방법을 포함한 AWS SDK 사용에 대한 자세한 내용은 AWS SDK
Node.js용 AWS SDK를 사용한 API 설정에는 createRestApi
, createResource
또는 getResources
, putMethod
, putMethodResponse
, putIntegration
및 putIntegrationResponse
함수 호출이 수반됩니다.
다음 절차의 필수 단계에 따라 이러한 SDK 명령을 사용하여 GET /pets
및 GET /pets/{petId}
메서드를 지원하는 간단한 PetStore API를 설정할 수 있습니다.
Node.js용 AWS SDK를 사용해 간단한 PetStore API를 설정하려면
-
SDK 인스턴스화:
var AWS = require('aws-sdk'); AWS.config.region = 'us-west-2'; var apig = new AWS.APIGateway({apiVersion: '2015/07/09'});
-
createRestApi
함수를 호출하여RestApi
엔터티를 설정합니다.apig.createRestApi({ name: "Simple PetStore (node.js SDK)", binaryMediaTypes: [ '*' ], description: "Demo API created using the AWS SDK for node.js", version: "0.00.001" }, function(err, data){ if (!err) { console.log(data); } else { console.log('Create API failed:\n', err); } });
이 함수는 다음 결과와 비슷한 출력을 반환합니다.
{ id: 'iuo308uaq7', name: 'PetStore (node.js SDK)', description: 'Demo API created using the AWS SDK for node.js', createdDate: 2017-09-05T19:32:35.000Z, version: '0.00.001', binaryMediaTypes: [ '*' ] }
그 결과로 얻는 API의 식별자는
iuo308uaq7
입니다. API 설정을 계속하려면 이를 제공해야 합니다. -
getResources
함수를 호출하여RestApi
의 루트 리소스 식별자를 검색합니다.apig.getResources({ restApiId: 'iuo308uaq7' }, function(err, data){ if (!err) { console.log(data); } else { console.log('Get the root resource failed:\n', err); } })
이 함수는 다음 결과와 비슷한 출력 결과를 반환합니다.
{ "items": [ { "path": "/", "id": "s4fb0trnk0" } ] }
루트 리소스 식별자는
s4fb0trnk0
입니다. 이것은 다음 작업인 API 리소스 트리 구축의 시작점입니다. -
createResource
함수를 호출하여 API에 대해/pets
리소스를 설정함으로써s4fb0trnk0
속성에 루트 리소스 식별자(parentId
)를 지정합니다.apig.createResource({ restApiId: 'iuo308uaq7', parentId: 's4fb0trnk0', pathPart: 'pets' }, function(err, data){ if (!err) { console.log(data); } else { console.log("The '/pets' resource setup failed:\n", err); } })
이 작업이 성공하면 결과는 다음과 같습니다.
{ "path": "/pets", "pathPart": "pets", "id": "8sxa2j", "parentId": "s4fb0trnk0'" }
/pets/{petId}
리소스를 설정하려면 다음과 같이createResource
함수를 호출하여/pets
속성에 새로 생성된8sxa2j
리소스(parentId
)를 지정합니다.apig.createResource({ restApiId: 'iuo308uaq7', parentId: '8sxa2j', pathPart: '{petId}' }, function(err, data){ if (!err) { console.log(data); } else { console.log("The '/pets/{petId}' resource setup failed:\n", err); } })
이 작업이 성공하면 다음과 같이 새로 생성된 리소스
id
값을 반환합니다.{ "path": "/pets/{petId}", "pathPart": "{petId}", "id": "au5df2", "parentId": "8sxa2j" }
이 절차 내내
/pets
의 리소스 ID를 지정하여8sxa2j
리소스를 참조하고/pets/{petId}
의 리소스 ID를 지정하여au5df2
리소스를 참조합니다. -
putMethod
함수를 호출하여GET
리소스(/pets
)에8sxa2j
HTTP 메서드를 추가합니다. 이를 통해 오픈 액세스로GET /pets
Method
를 설정합니다.apig.putMethod({ restApiId: 'iuo308uaq7', resourceId: '8sxa2j, httpMethod: 'GET', authorizationType: 'NONE' }, function(err, data){ if (!err) { console.log(data); } else { console.log("The 'GET /pets' method setup failed:\n", err); } })
이 함수는 다음 결과와 비슷한 출력 결과를 반환합니다.
{ "apiKeyRequired": false, "httpMethod": "GET", "authorizationType": "NONE" }
오픈 액세스로
GET
의 API 메서드를 설정하는/pets/{petId}
리소스(au5df2
)에GET /pets/{petId}
HTTP 메서드를 추가하려면 다음과 같이putMethod
함수를 호출합니다.apig.putMethod({ restApiId: 'iuo308uaq7', resourceId: 'au5df2', httpMethod: 'GET', authorizationType: 'NONE', requestParameters: { "method.request.path.petId" : true } }, function(err, data){ if (!err) { console.log(data); } else { console.log("The 'GET /pets/{petId}' method setup failed:\n", err); } })
이 함수는 다음 결과와 비슷한 출력 결과를 반환합니다.
{ "apiKeyRequired": false, "httpMethod": "GET", "authorizationType": "NONE", "requestParameters": { "method.request.path.petId": true } }
앞의 예제와 같이
requestParameters
속성을 설정하여 클라이언트에서 제공한petId
값을 백엔드로 매핑 및 전달해야 합니다. -
putMethodResponse
함수를 호출하여GET /pets
메서드에 메서드 응답을 설정합니다.apig.putMethodResponse({ restApiId: 'iuo308uaq7', resourceId: "8sxa2j", httpMethod: 'GET', statusCode: '200' }, function(err, data){ if (!err) { console.log(data); } else { console.log("Set up the 200 OK response for the 'GET /pets' method failed:\n", err); } })
이 함수는 다음 결과와 비슷한 출력 결과를 반환합니다.
{ "statusCode": "200" }
GET /pets/{petId}
메서드의 200 OK 응답을 설정하려면putMethodResponse
함수를 호출하여/pets/{petId}
속성에au5df2
리소스 식별자(resourceId
)를 지정합니다.apig.putMethodResponse({ restApiId: 'iuo308uaq7', resourceId: "au5df2", httpMethod: 'GET', statusCode: '200' }, function(err, data){ if (!err) { console.log(data); } else { console.log("Set up the 200 OK response for the 'GET /pets/{petId}' method failed:\n", err); } })
-
putIntegration
함수를 호출하여Integration
메서드에 지정된 HTTP 엔드포인트로GET /pets
을 설정함으로써/pets
속성에8sxa2j
리소스 식별자(parentId
)를 제공합니다.apig.putIntegration({ restApiId: 'iuo308uaq7', resourceId: '8sxa2j', httpMethod: 'GET', type: 'HTTP', integrationHttpMethod: 'GET', uri: 'http://perstore-demo-endpoint.execute-api.com/pets' }, function(err, data){ if (!err) { console.log(data); } else { console.log("Set up the integration of the 'GET /' method of the API failed:\n", err); } })
이 함수는 다음과 비슷한 출력 결과를 반환합니다.
{ "httpMethod": "GET", "passthroughBehavior": "WHEN_NO_MATCH", "cacheKeyParameters": [], "type": "HTTP", "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets", "cacheNamespace": "8sxa2j" }
백엔드의
GET /pets/{petId}
의 HTTP 엔드포인트로http://perstore-demo-endpoint.execute-api.com/pets/{id}
메서드의 통합을 설정하려면 다음과 같이putIntegration
함수를 호출하여/pets/{petId}
속성에 API의au5df2
리소스 식별자(parentId
)를 제공합니다.apig.putIntegration({ restApiId: 'iuo308uaq7', resourceId: 'au5df2', httpMethod: 'GET', type: 'HTTP', integrationHttpMethod: 'GET', uri: 'http://perstore-demo-endpoint.execute-api.com/pets/{id}', requestParameters: { "integration.request.path.id": "method.request.path.petId" } }, function(err, data){ if (!err) { console.log(data); } else { console.log("The 'GET /pets/{petId}' method integration setup failed:\n", err); } })
이 함수가 성공적으로 실행되면 다음과 비슷한 출력 결과를 반환합니다.
{ "httpMethod": "GET", "passthroughBehavior": "WHEN_NO_MATCH", "cacheKeyParameters": [], "type": "HTTP", "uri": "http://petstore-demo-endpoint.execute-api.com/petstore/pets/{id}", "cacheNamespace": "au5df2", "requestParameters": { "integration.request.path.id": "method.request.path.petId" } }
-
putIntegrationResponse
함수를 호출하여GET /pets
메서드에 대한 200 OK 통합 응답을 설정함으로써/pets
속성에 API의8sxa2j
리소스 식별자(resourceId
)를 지정합니다.apig.putIntegrationResponse({ restApiId: 'iuo308uaq7', resourceId: '8sxa2j', httpMethod: 'GET', statusCode: '200', selectionPattern: '' }, function(err, data){ if (!err) { console.log(data); } else console.log("The 'GET /pets' method integration response setup failed:\n", err); })
이 함수는 다음 결과와 비슷한 출력 결과를 반환합니다.
{ "selectionPattern": "", "statusCode": "200" }
GET /pets/{petId}
메서드의 200 OK 통합 응답을 설정하여putIntegrationResponse
함수를 호출함으로써resourceId
속성에 API의/pets/{petId}
리소스 식별자(au5df2
)를 지정합니다.apig.putIntegrationResponse({ restApiId: 'iuo308uaq7', resourceId: 'au5df2', httpMethod: 'GET', statusCode: '200', selectionPattern: '' }, function(err, data){ if (!err) { console.log(data); } else console.log("The 'GET /pets/{petId}' method integration response setup failed:\n", err); })
-
API를 배포하기 전에 API 호출을 테스트해봅니다.
GET /pets
메서드 호출을 테스트하려면 다음과 같이testInvokeMethod
를 호출하여/pets
속성에8sxa2j
리소스 식별자(resourceId
)를 지정합니다.apig.testInvokeMethod({ restApiId: 'iuo308uaq7', resourceId: '8sxa2j', httpMethod: "GET", pathWithQueryString: '/' }, function(err, data){ if (!err) { console.log(data) } else { console.log('Test-invoke-method on 'GET /pets' failed:\n', err); } })
GET /pets/{petId}
메서드 호출을 테스트하려면 다음과 같이testInvokeMethod
를 호출하여/pets/{petId}
속성에au5df2
리소스 식별자(resourceId
)를 지정합니다.apig.testInvokeMethod({ restApiId: 'iuo308uaq7', resourceId: 'au5df2', httpMethod: "GET", pathWithQueryString: '/' }, function(err, data){ if (!err) { console.log(data) } else { console.log('Test-invoke-method on 'GET /pets/{petId}' failed:\n', err); } })
-
끝으로 고객이 호출할 수 있게 API를 배포합니다.
apig.createDeployment({ restApiId: 'iuo308uaq7', stageName: 'test', stageDescription: 'test deployment', description: 'API deployment' }, function(err, data){ if (err) { console.log('Deploying API failed:\n', err); } else { console.log("Deploying API succeeded\n", data); } })