This repository hosts an SDK for developing Lambda functions for the Manetu Platform in the TypeScript programming language.
The Manetu platform serves Lambda functions within a WebAssembly (WASM) environment. We can leverage the TypeScript language in our Lambda functions using a combination of two tools: rollup to bundle TypeScript to an ECMAScript Module (ESM), followed by mjsc to compile ESM to WASM.
mkdir my-lambda
cd my-lambda
{
"name": "hello-ts",
"type": "module",
"version": "0.0.1",
"description": "Hello World as a Manetu Lambda Function",
"main": "src/main.js",
"dependencies": {
"manetu-lambda-sdk": "0.0.1",
"@rollup/plugin-node-resolve": "^15.0.1"
}
}
{
"compilerOptions": {
"module": "ES6",
"target": "es2020",
"outDir": "./target",
"moduleResolution": "bundler"
},
"include": [
"src/**/*"
]
}
import nodeResolve from "@rollup/plugin-node-resolve";
export default {
plugins: [
nodeResolve(), // <-- this allows npm modules to be added to bundle
],
};
OBJECT=target/lambda.wasm
SRCS = $(shell find src -type f)
all: $(OBJECT)
target:
mkdir target
node_modules: package*.json
npm install
target/main.js: target Makefile node_modules $(SRCS)
tsc
target/lambda.js: target/main.js rollup.config.js
rollup $< -o $@ -f es -c
$(OBJECT): target/lambda.js
mjsc compile $^ -o $@
clean:
-rm -rf target node_modules
mkdir -p src
pushd src
import {register} from 'manetu-lambda-sdk';
import {LambdaRequest, LambdaResponse} from 'manetu-lambda-sdk';
function handler(req: LambdaRequest): LambdaResponse {
// @ts-ignore
return {status: 200, body: "Hello, " + req.params.name}
}
register(handler);
console.log("Module initialized");
popd
make
You should now have a file 'target/lambda.wasm' ready for deployment (See general documentation).
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.