AWS를 이용한 Serverless 환경 구축 3 - AWS API Gateway를 통해 기존 DynamoDB 접근

Lambda와 DynamoDB를 연결한 상태에서 외부에서 접근하여 쓰기 및 읽기를 할 수 있도록 AWS API Gateway를 추가 해보자.

이번에 해 볼 것은 API 생성 및 설정이다.

API 설정(GET, POST)

1. API Gateway를 생성한다.

2. OK 버튼 클릭 후 "New API" 선택, API name 기입, 생성 한다.

3. Resource 항목의 Actions를 pull-down하여 Create Resource를 선택한다. Get, Post를 위한 각각 Resource를 만든다.

4. 각 Resource 아래 "Create Method" 통해 GET, POST를 생성함.(v 표 클릭해서 완성)


5. GET, POST method를 클릭하여 세부설정을 해 준다. 각각 DynamoDB에 GET, POST 한 Lambda 함수를 지역과 이름을 지정해 준다.

6. GET method를 선택하고 오른쪽에 있는 Method Execution에서 Method Request를 선택한다. "URL Query String Parameters"에 "id"를 추가 해 준다.

7. Method Execution에서 Integration Request를 선택한다. 하단의 "Body Mapping Templates"을 펼 친후 Content-Type에 "application/json"을 추가 한다. 아래 소스를 붙여 넣어 준다.

#set($inputRoot = $input.path('$'))
{ "id": "$input.params('id')" }



8. Method Execution의 첫 화면으로 돌아 가서 "Test'를 클릭한다.


9. Query String "id"에 전 화면에서 추가 했던 "App"를 넣고 테스트 한다.

10. 테스트 이상이 없으면 "Deploy"한다.("Deploy"는 Action pull-down 메뉴에서 찾을 수 있다.)


11. 화면이 바뀌면서 해당 API의 URL을 알 수 있다.
https://id.excute-api.지역/test 식으로 표시 된다.

12. 알아낸 URL을 통해 Curl 해보면 정상 작동 함을 알 수 있다.


$curl https://id.excute-api.지역/test/getdata?id=App

"Dynamo Success: {\n  \"Item\": {\n    \"id\": \"App\",\n    \"email\": \"test@example.com\",\n    \"timedate\": \"1464858849770\",\n    \"name\": \"Smith\"\n  }\n}"


13. POST도 할 수 있다.

$curl -H "Content-Type: application/json" -X POST -d "{\"id\":\"Client\",\"name\":\"Alice\",\"email\":\"post@example.com\"}" https://id.excute-api.지역/test/postdata


"Dynamo Success: {}"


$curl https://id.excute-api.지역/test/getdata?id=Client


"Dynamo Success: {\n  \"Item\": {\n    \"id\": \"Client\",\n    \"email\": \"post@example.com\",\n    \"timedate\": \"1465374921213\",\n    \"name\": \"Alice\"\n  }\n}"

댓글

이 블로그의 인기 게시물

AWS를 이용한 Serverless 환경 구축 2 - Lambda로 DynamoDB 쓰고 읽기

Gitlab CI 설정

AWS Ceritificate Manger 이용방법