Skip to content

Latest commit

 

History

History
143 lines (107 loc) · 3.59 KB

README.md

File metadata and controls

143 lines (107 loc) · 3.59 KB

lambda-sdk-clj

Clojars Project

This repository hosts an SDK for developing Lambda functions for the Manetu Platform in the ClojureScript programming language.

Prerequisites

Project setup

The Manetu platform serves Lambda functions within a WebAssembly (WASM) environment. We can leverage the Clojurescript language in our Lambda functions using a combination of two tools: shadow-cljs to compile Clojurescript to an ECMAScript Module (ESM), followed by mjsc to compile ESM to WASM.

Create a directory for your project

mkdir my-lambda
cd my-lambda

Create the build files

shadow-cljs.edn

{:builds {:lambda  {:target     :esm
                    :output-dir "target"
                    :devtools   {:enabled false}
                    :modules    {:lambda {:init-fn hellocljs.core/main}}}}
 :lein true}

project.clj

(defproject hellocljs "0.0.1-SNAPSHOT"
  :min-lein-version "2.9.0"
  :dependencies [[org.clojure/clojure "1.11.2"]
                 [org.clojure/clojurescript "1.11.132"
                  :exclusions [com.google.javascript/closure-compiler-unshaded
                               org.clojure/google-closure-library]]
                 [io.github.manetu/lambda-sdk "0.0.2"]
                 [thheller/shadow-cljs "2.27.1"]]
  :source-paths ["src"])

Makefile

OBJECT=target/lambda.wasm
SRCS = $(shell find src -type f)

all: $(OBJECT)

target:
	mkdir target

target/lambda.js: Makefile project.clj shadow-cljs.edn $(SRCS)
	shadow-cljs release lambda

$(OBJECT): target/lambda.js
	mjsc compile $^ -o $@

clean:
	-rm -rf target

Create the Lambda source

Create the source path

mkdir -p src/hellocljs
pushd src/hellocljs

core.cljs

(ns hellocljs.core
  (:require [manetu.lambda :as lambda]))

(defn handle-request [{{:keys [name]} :params}]
  {:status 200
   :body (str "Hello, " name)})

(defn main []
  (lambda/register-handler handle-request)
  (println "Module Initialized"))

Return to the top-level directory

popd

Compile the program

make

You should now have a file 'target/lambda.wasm' ready for deployment (See general documentation).

Define a specification for your Lambda function

Create a file 'site.yml' with the following contents:

api-version: lambda.manetu.io/v1alpha1
kind: Site
metadata:
  name: hello
spec:
  runtime: wasi.1.alpha2
  image: oci://my-registry.example.com/my-lambda:v0.0.1
  env:
    LOG_LEVEL: trace
  triggers:
    http-queries:
      - route: /greet
        summary: "Returns a greeting to the user"
        description: "This request allows you to test the ability to deploy and invoke a simple lambda function."
        query-parameters:
          - name: "name"
            schema: { type: "string" }
            description: "The caller's name"
        responses:
          200:
            description: "computed greeting"
            content:
              text/plain:
                schema:
                  type: string

Be sure to adjust the image OCI URL.