Clojure + AWS Lambda + API Gateway에서 JSON API 만들기
Lambda Function 등록
Clojure Project를 만들고 Lambda에서 작동하는 코드를 작성합니다.
 Clojure project 만들기
lein new hello
종속성에 com.amazonaws/aws-lambda-java-core 추가
project.clj(defproject hello "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.7.0"]
                 [org.clojure/data.json "0.2.6"]
                 [com.amazonaws/aws-lambda-java-core "1.0.0"]]
  :aot :all)
 소스 코드 편집
이번에는 {:name "foo"}라는 객체를 받으면 {"message": "Hello foo"}라는 json을 반환하는 코드를 작성합니다.
src/hello/core.clj(ns hello.core
  (:gen-class
   :implements [com.amazonaws.services.lambda.runtime.RequestStreamHandler])
  (:require [clojure.data.json :as json]
            [clojure.string :as s]
            [clojure.java.io :as io]))
(defn hello [params]
  {:message (str "Hello " (:name params))})
(defn -handleRequest [this is os context]
  (let [w (io/writer os)]
    (-> (json/read (io/reader is) :key-fn keyword)
        (hello)
        (json/write w))
    (.flush w)))
 jar 생성
lein uberjar
 Lambda Function 만들기
aws lambda create-function \
--function-name hello \
--handler hello.core \
--runtime java8 \
--memory 512 \
--timeout 10 \
--role arn:aws:iam::YOUR_AWS_ACCOUNT_ID:role/YOUR_ROLE \
--zip-file filed://./target/hello-0.1.0-SNAPSHOT-standalone.jar
이제 Clojure 코드를 AWS Lambda에서 실행할 수 있어야 합니다.
 API Gateway에서 Lambda Function 호출
 API 만들기
API Gateway 콘솔에서 새 API를 만듭니다.
 리소스 추가
"Create Method"버튼을 클릭하면 "Resources"에 풀다운이 표시되어 HTTP 메서드를 선택할 수 있습니다.
여기에서는 GET 메소드를 선택해 추가.
"Integration type"을 "Lambda Function"으로 설정하면 AWS Lambda 함수를 호출할 수 있습니다.
 
 URL Query String Parameter의 등록 (어쩌면 이것이 필요하지 않을지도)
요청 매개 변수를 API Gateway에서 처리하는 데이터 객체로 변환하기 위해 "Method Request"에서 매개 변수를 추가합니다.
 
"URL Query String Parameters"-> "Add query string"을 선택하면 매개 변수를 추가 할 수 있습니다.
이번에는 "name"이라는 파라미터를 추가
 
 Lambda Function에 전달할 매개 변수에 대한 mapping 정의 추가
요청 파라미터로부터 받은 파라미터를 Lambda Function에서 취급하는 데이터에 mapping합니다.
"Integration Request"선택
 
"Mapping Templates"-> "Add mapping template"을 선택하고 "application/json"을 추가.
 
"Input passthrough"옆의 icon을 클릭하여 "Mapping template"로 변경
 
템플릿에 다음을 추가
{"name": "$input.params('name')"}
 
저장하면 test로 확인해 봅니다.
 
예상대로 JSON이 반환되는 것 같아서 괜찮을 것 같습니다.
 Deploy
생성한 API는 아직 공개되지 않은 상태이므로 이를 배포합니다.
화면 왼쪽의 "Deploy API"를 클릭하여 Deploy 정보를 작성합니다.
("Deployment Stage"를 "New Stage"로 설정하면 test용이나 production용 API를 분리할 수 있는 것 같습니다.) 필요한 설정을 기술하면 "Deploy"버튼을 클릭.
 
이제 API가 게시되었습니다.
브라우저 등에서 "Invoke URL"에 기재되어 있는 URL에 액세스 할 수 있게 되어 있을 것입니다.
 
 참고 기사
 htps : // 아 ws. 아마존. 코 m/jp/bぉgs/코 m푸테/cぉ쥬레/
 ぃ tp // 코 m / c 사카 토쿠 / ms / 27b f d3 c87901 p 24 c7
 ぃ tp // 코 m / c 사카 토쿠 / ms / 푹신 195dc871 아 b059 에 535
 ぃ tp // 이 m/r7 가무라/있어 ms/6420538789다 95cd2f47
 【추기】/hello/:name과 같은 형태로 parameter를 건네주는 경우
API Gateway에서 다음과 같이 리소스를 추가하면 가능합니다.
1. "hello"추가
2. "hello"아래에 "{name}"추가
3. "{name}"에 GET 메소드 설정
이렇게하면 /test/hello/hoge와 같은 액세스에 대해 {"message" : "Hello hoge"}와 같은 JSON을 반환 할 수 있습니다.
(Lambda에 전달하는 파라미터의 Template는 변경하지 않아도 괜찮습니다)
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(Clojure + AWS Lambda + API Gateway에서 JSON API 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/atsuya046/items/39ef7a28fbe278376e19
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
lein new hello
(defproject hello "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.7.0"]
                 [org.clojure/data.json "0.2.6"]
                 [com.amazonaws/aws-lambda-java-core "1.0.0"]]
  :aot :all)
(ns hello.core
  (:gen-class
   :implements [com.amazonaws.services.lambda.runtime.RequestStreamHandler])
  (:require [clojure.data.json :as json]
            [clojure.string :as s]
            [clojure.java.io :as io]))
(defn hello [params]
  {:message (str "Hello " (:name params))})
(defn -handleRequest [this is os context]
  (let [w (io/writer os)]
    (-> (json/read (io/reader is) :key-fn keyword)
        (hello)
        (json/write w))
    (.flush w)))
lein uberjar
aws lambda create-function \
--function-name hello \
--handler hello.core \
--runtime java8 \
--memory 512 \
--timeout 10 \
--role arn:aws:iam::YOUR_AWS_ACCOUNT_ID:role/YOUR_ROLE \
--zip-file filed://./target/hello-0.1.0-SNAPSHOT-standalone.jar
API 만들기
API Gateway 콘솔에서 새 API를 만듭니다.
리소스 추가
"Create Method"버튼을 클릭하면 "Resources"에 풀다운이 표시되어 HTTP 메서드를 선택할 수 있습니다.
여기에서는 GET 메소드를 선택해 추가.
"Integration type"을 "Lambda Function"으로 설정하면 AWS Lambda 함수를 호출할 수 있습니다.

URL Query String Parameter의 등록 (어쩌면 이것이 필요하지 않을지도)
요청 매개 변수를 API Gateway에서 처리하는 데이터 객체로 변환하기 위해 "Method Request"에서 매개 변수를 추가합니다.

"URL Query String Parameters"-> "Add query string"을 선택하면 매개 변수를 추가 할 수 있습니다.
이번에는 "name"이라는 파라미터를 추가

Lambda Function에 전달할 매개 변수에 대한 mapping 정의 추가
요청 파라미터로부터 받은 파라미터를 Lambda Function에서 취급하는 데이터에 mapping합니다.
"Integration Request"선택

"Mapping Templates"-> "Add mapping template"을 선택하고 "application/json"을 추가.

"Input passthrough"옆의 icon을 클릭하여 "Mapping template"로 변경

템플릿에 다음을 추가
{"name": "$input.params('name')"}

저장하면 test로 확인해 봅니다.

예상대로 JSON이 반환되는 것 같아서 괜찮을 것 같습니다.
Deploy
생성한 API는 아직 공개되지 않은 상태이므로 이를 배포합니다.
화면 왼쪽의 "Deploy API"를 클릭하여 Deploy 정보를 작성합니다.
("Deployment Stage"를 "New Stage"로 설정하면 test용이나 production용 API를 분리할 수 있는 것 같습니다.) 필요한 설정을 기술하면 "Deploy"버튼을 클릭.

이제 API가 게시되었습니다.
브라우저 등에서 "Invoke URL"에 기재되어 있는 URL에 액세스 할 수 있게 되어 있을 것입니다.

참고 기사
 htps : // 아 ws. 아마존. 코 m/jp/bぉgs/코 m푸테/cぉ쥬레/
 ぃ tp // 코 m / c 사카 토쿠 / ms / 27b f d3 c87901 p 24 c7
 ぃ tp // 코 m / c 사카 토쿠 / ms / 푹신 195dc871 아 b059 에 535
 ぃ tp // 이 m/r7 가무라/있어 ms/6420538789다 95cd2f47
 【추기】/hello/:name과 같은 형태로 parameter를 건네주는 경우
API Gateway에서 다음과 같이 리소스를 추가하면 가능합니다.
1. "hello"추가
2. "hello"아래에 "{name}"추가
3. "{name}"에 GET 메소드 설정
이렇게하면 /test/hello/hoge와 같은 액세스에 대해 {"message" : "Hello hoge"}와 같은 JSON을 반환 할 수 있습니다.
(Lambda에 전달하는 파라미터의 Template는 변경하지 않아도 괜찮습니다)
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(Clojure + AWS Lambda + API Gateway에서 JSON API 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/atsuya046/items/39ef7a28fbe278376e19
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
API Gateway에서 다음과 같이 리소스를 추가하면 가능합니다.
1. "hello"추가
2. "hello"아래에 "{name}"추가
3. "{name}"에 GET 메소드 설정
이렇게하면
/test/hello/hoge와 같은 액세스에 대해 {"message" : "Hello hoge"}와 같은 JSON을 반환 할 수 있습니다.(Lambda에 전달하는 파라미터의 Template는 변경하지 않아도 괜찮습니다)
Reference
이 문제에 관하여(Clojure + AWS Lambda + API Gateway에서 JSON API 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/atsuya046/items/39ef7a28fbe278376e19텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)