From Glojure source to a bootstrap binary

Glojure is a Clojure interpreter hosted on Go. Go values are Glojure values, so the runtime loop here calls net/http directly instead of pulling in an HTTP library. Glojure can also compile namespaces ahead of time into Go source, and that's how this project ends up with one static bootstrap binary and no interpreter warm-up on the request path.

The three namespaces

src/net/b12n/lambda_mvp/ has three files, and each is in the language it needs:

FileWhat it does
handler.cljThe demo handler. Plain Clojure, character for character the same logic as lambda-mvp-jlt's, so it ends in .clj
runtime.gljThe Lambda Runtime API loop, written against Go's net/http, io and strings. It only runs on Glojure, so it ends in .glj
bootstrap.glj-main: runs the loop and reports a startup failure to /runtime/init/error

Glojure looks for .glj, then .clj, then .cljc, and reads #?(:glj ...) in reader conditionals.

Talking to Go

A Go function that returns two values comes back as a two-element vector, so (net:http.Get url) is [resp err]. runtime.glj passes each call through a small check that throws an ex-info when err isn't nil and otherwise returns the value. Package paths swap / for :, as in net:http.NewRequest.

Struct fields and methods both use the leading-dot form, (.StatusCode resp) and (.Get (.Header resp) "Lambda-Runtime-Aws-Request-Id"). The .-field form JVM Clojure uses for fields fails with unimplemented op: 25 in Glojure 0.7.24. (go/string bytes) turns the []byte from io.ReadAll into a string.

Building

bb build runs three steps:

  1. bb glj builds the Glojure compiler, cmd/glj at the version go.mod pins, into .build/glj.
  2. bb compile runs (compile 'ns) on each namespace. Each one becomes a Go package with a LoadNS function, written to src/net/b12n/lambda_mvp/<name>/loader.go. These files are build output and stay out of git.
  3. go build -tags glj_aot_runtime cross-compiles cmd/bootstrap for linux/arm64 with CGO_ENABLED=0, and the static binary is zipped on its own.

The glj_aot_runtime tag drops the parts of Glojure a precompiled program doesn't need, such as the Go source generator and the fallback standard-library sources. Because the binary is static, the zip has no lib/ directory and doesn't depend on which glibc provided.al2023 ships. That also means no Docker build, unlike lambda-mvp-jlt.

Two things that cost a debugging round

The entry namespace can't be called main. Its generated package would be package main, and Go won't import a package with that name. So the namespace is net.b12n.lambda-mvp.bootstrap, which matches the name Lambda gives the executable anyway.

cmd/bootstrap/main.go calls every namespace's LoadNS itself, in dependency order. With only the entry namespace loaded, its :require found the other namespaces already present and left their vars unbound, and the first invoke failed with cannot call unbound var: #'net.b12n.lambda-mvp.runtime/run. If you add a namespace, add it to namespaces in bb.edn and to main.go.

Testing without AWS

bb probe builds the bootstrap for your own machine and runs it against tools/mock_runtime_api.py, which serves two events with the headers Lambda sends, checks both responses, and ends the loop with 410 Gone. It needs only Go and Python.