Skip to content

An SDK for developing Lambda functions for the Manetu Platform in TypeScript

License

Notifications You must be signed in to change notification settings

manetu/lambda-sdk-ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lambda-sdk-ts

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

Prerequisites

Project setup

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.

Create a directory for your project

mkdir my-lambda
cd my-lambda

Create the build files

package.json

{
  "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"
  }
}

tsconfig.json

{
    "compilerOptions": {
        "module": "ES6",
        "target": "es2020",
        "outDir": "./target",
        "moduleResolution": "bundler"
    },
    "include": [
        "src/**/*"
    ]
}

rollup.config.js

import nodeResolve from "@rollup/plugin-node-resolve";

export default {
    plugins: [
        nodeResolve(), // <-- this allows npm modules to be added to bundle
    ],
};

Makefile

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

Create the Lambda source

Create the source path

mkdir -p src
pushd src

main.ts

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");

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.

About

An SDK for developing Lambda functions for the Manetu Platform in TypeScript

Resources

License

Stars

Watchers

Forks

Packages

No packages published