Fork me on GitHub
#duct
<
2018-05-31
>
scaturr05:05:56

woooo - got duct running on lambda

scaturr05:05:43

muuntaja presented some problems with how it formats the body (ByteArrayInputStream), and aws’ expected format

scaturr05:05:55

but the gist of it was:

(ns slack-api.lambda
    (:require [ :as io]
              [duct.core :as duct]
              [integrant.core :as ig]
              [clojure.data.json :as json]
              [uswitch.lambada.core :refer [deflambdafn]]))

(duct/load-hierarchy)

(let [config  (-> (duct/read-config (io/resource "slack_api/config.edn"))
                  duct/prep)
      handler (-> config
                  (ig/init [:duct.core/handler])
                  :duct.core/handler)]
  (deflambdafn slack-api.lambda.LambdaFn [is os ctx]
    (with-open [writer (io/writer os)]
      (let [request (json/read (io/reader is :encoding "UTF-8") :key-fn keyword)
            response (handler request)]
        (-> response
            :body
            slurp
            (as-> body (assoc response :body body))
            (assoc :isBase64Encoded false)
            (select-keys [:body :statusCode :headers :isBase64Encoded])
            (json/write writer))))))

scaturr05:05:45

Defined a single middleware leveraging an existing library:

(ns slack-api.middleware.apigw
  (:require [integrant.core :as ig]
            [ring.middleware.apigw :as apigw]))

(defmethod ig/init-key :slack-api.middleware/apigw [_ options]
  #(apigw/wrap-apigw-lambda-proxy % (or options {})))

scaturr05:05:35

its late but ill try to improve and make a module if others find it useful ¯\(ツ)

👍 8