forked from mcohen01/amazonica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.clj
46 lines (40 loc) · 1.7 KB
/
lambda.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
(ns amazonica.aws.lambda
(:use [robert.hooke :only (add-hook)])
(:require [amazonica.core :as amz])
(:import com.amazonaws.services.lambda.AWSLambdaClient))
(amz/set-client AWSLambdaClient *ns*)
(defn byte-buffer-zip-file [function-name ^String body]
(let [baos (java.io.ByteArrayOutputStream.)
zos (java.util.zip.ZipOutputStream. baos)]
(.putNextEntry zos (java.util.zip.ZipEntry. (str function-name ".js")))
(.write zos (.getBytes body))
(.closeEntry zos)
(.finish zos)
(java.nio.ByteBuffer/wrap (.toByteArray baos))))
(defn function-name [node-fn]
(-> ^String (re-find #"exports\..+=" node-fn)
(.replaceFirst "exports." "")
(.replaceFirst "=" "")
.trim))
(defn- parse-function
[f cred & args]
(let [arg-map (amz/parse-args cred args)
attrs (if (even? (count (:args arg-map)))
(apply hash-map (:args arg-map))
(first (:args arg-map)))
attrs (merge {:timeout 10
:memory-size 256
:mode "event"
:runtime "nodejs6.10"
:description "uploaded via amazonica"} attrs)
fn-name (or (:function-name attrs) (function-name (:function attrs)))
attrs (if (:function attrs)
(merge {:function-name fn-name
:code {:zip-file (byte-buffer-zip-file fn-name (:function attrs))}
:handler (str fn-name "." fn-name)} attrs)
attrs)
func (if (contains? arg-map :cred)
(partial f cred)
f)]
(func (dissoc attrs :function))))
(add-hook #'create-function #'parse-function)