Skip to content

Instantly share code, notes, and snippets.

@usametov
Last active June 4, 2021 16:29
Show Gist options
  • Save usametov/f5863c21ef4d8e8f86c96c0ab016603c to your computer and use it in GitHub Desktop.
Save usametov/f5863c21ef4d8e8f86c96c0ab016603c to your computer and use it in GitHub Desktop.
;; This simnle tutorial is using AWS CDK (Cloud Development Kit, JavaScript flavor),
;; Clojure CLI (tools.deps), Cognitect Lab's aws-api, and Oracle's JDBC library.
;; deps.edn file looks like this:
{:deps {org.clojure/clojure {:mvn/version "1.10.1"} org.clojure/data.json {:mvn/version "1.0.0"}
com.amazonaws/aws-lambda-java-core {:mvn/version "1.2.0"}
com.cognitect.aws/api {:mvn/version "0.8.456"} com.cognitect.aws/endpoints {:mvn/version "1.1.11.753"} com.cognitect.aws/s3 {:mvn/version "784.2.593.0"} com.cognitect.aws/sqs {:mvn/version "770.2.568.0"} com.cognitect.aws/secretsmanager {:mvn/version "793.2.626.0"}
com.oracle.database.jdbc/ojdbc10 {:mvn/version "19.3.0.0"} seancorfield/next.jdbc {:mvn/version "1.0.409"}}
:paths ["src" "classes"]
:aliases {:nrepl {:extra-deps {nrepl/nrepl {:mvn/version "0.6.0"} cider/cider-nrepl {:mvn/version "0.24.0"}} :main-opts ["--main" "nrepl.cmdline" "--middleware" "[cider.nrepl/cider-middleware]"]}
:dev {:extra-paths ["dev"] :extra-deps {expound {:mvn/version "0.8.4"}}}
:test {:extra-deps {lambdaisland/kaocha {:mvn/version "0.0-601"} lambdaisland/kaocha-junit-xml {:mvn/version "0.0-70"}} :extra-paths ["test"] :main-opts ["--main" "kaocha.runner" "--reporter" "documentation" "--plugin" "kaocha.plugin/junit-xml" "--junit-xml-file" "target/junit.xml"]}
:uberjar {:deps {uberdeps {:mvn/version "0.1.8"}} :main-opts ["--main" "uberdeps.uberjar" "--target" "target/example.jar"]}}}
;; And here is what the hello.clj (the entry point) looks like in its simplest form:
(ns com.example.aws-lambda.handler
(:require [clojure.data.json :as json]
[clojure.java.io :as io])
(:gen-class :implements [com.amazonaws.services.lambda.runtime.RequestStreamHandler]))
(defn handle-event [event context]
(str "Hello, " (get event "firstName") (get event "lastName")))
(defn -handleRequest
"Converts IO streams to data structures."
[_ input-stream output-stream context]
(with-open [reader (io/reader input-stream)
writer (io/writer output-stream)]
(-> reader
(json/read)
(handle-event context)
(json/write writer))))
;;Finally, here's how you would create the function and invoke it on the command line (for testing-purposes):
aws lambda create-function \
--function-name hello \
--handler com.example.aws_lambda.hello \
--memory 512 \
--role arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole \
--runtime java11 \
--timeout 10 \
--zip-file fileb://target/hello.jar
;; how to invoke it:
aws lambda invoke \
--function-name hello \
--cli-binary-format raw-in-base64-out \
--payload '{"firstName":"john","lastName":"doe"}' \
response.json
;; create and invoke example, for testing purposes
aws lambda create-function \
--function-name example \
--handler com.example.aws_lambda.handler \
--memory 512 \
--role arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole \
--runtime java11 \
--timeout 10 \
--zip-file fileb://./target/example.jar invoke it: aws lambda invoke \
--function-name example \
--cli-binary-format raw-in-base64-out \
--payload '{"firstName":"john","lastName":"doe"}' \
response.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment