AWS Lambda is a service which allows named functions to be directly invoked (via a client API), have their execution triggered by a variety of AWS events (S3 upload, DynamoDB activity, etc.) or to serve as HTTP endpoints (via API Gateway).
This README serves to document a Leiningen
plugin
(lein-cljs-lambda
), template (cljs-lambda
) and small
library (cljs-lambda
) to facilitate the
writing, deployment & invocation of Clojurescript Lambda functions
- Low instance warmup penalty
- Ability to specify execution roles and resource limits in project definition
- Use promises, or asynchronous channels for deferred completion
:optimizations
:advanced
support, for smaller zip files*- Utilities for testing Lambda entrypoints off of EC2
- Parallel deployments
- Function publishing/versioning
N.B. If using advanced compilation alongside Node's standard library, something like cljs-nodejs-externs will be required
This collection of projects is used extensively in production, for important
pieces of infrastructure. While efforts are made to ensure backward
compatibility in the Leiningen plugin, the cljs-lambda
API is subject to
breaking changes.
$ lein new cljs-lambda my-lambda-project
$ cd my-lambda-project
$ lein cljs-lambda default-iam-role
$ lein cljs-lambda deploy
$ lein cljs-lambda invoke work-magic \
'{"spell": "delay-promise", "magic-word": "my-lambda-project-token"}'
...
$ lein cljs-lambda update-config work-magic :memory-size 256 :timeout 66
- Clojurescript/Node on AWS Lambda (blog post)
- Chasing Chemtrails w/ Clojurescript (blog post)
- Example project (generated from template)
(Using promises)
(deflambda slowly-attack [{target :name} ctx]
(p/delay 1000 {:to target :data "This is an attack"}))
AWS Integration With eulalie
(Using core.async)
This function retrieves the name it was invoked under, then attempts to invoke itself in order to recursively compute the factorial of its input:
(deflambda fac [n {:keys [function-name] :as ctx}]
(go
(if (<= n 1)
n
(let [[tag result] (<! (lambda/request! (creds/env) function-name (dec n)))]
(* n result)))))
See the eulalie.lambda.util documentation for further details.
N.B. Functions interacting with AWS will require execution under roles with the
appropriate permissions, and will not execute under the placeholder IAM role
created by the plugin's default-iam-role
task.
Using SNS & SQS from Clojurescript Lambda functions is covered in this working example, and the blog post which discusses it.
$ lein cljs-lambda invoke my-lambda-fn '{"arg1": "value" ...}' [:region ...]
If you're interested in programmatically invoking Lambda functions from Clojure/Clojurescript, it's pretty easy with eulalie:
(eulalie.lambda.util/request!
{:access-key ... :secret-key ... [:token :region etc.]}
"my-lambda-fn"
{:arg1 "value" :arg2 ["value"]})
cljs-lambda is free and unencumbered public domain software. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.