Skip to content

Latest commit

 

History

History
111 lines (82 loc) · 2.43 KB

README.md

File metadata and controls

111 lines (82 loc) · 2.43 KB

lambda-sdk-go

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

Prerequisites

Project setup

Create a directory for your project

mkdir my-lambda
cd my-lambda

Initialize a new go project in your working directory

go mod init my-lambda

Install dependencies

This SDK may be installed with

go get -u github.com/manetu/lambda-sdk-go

We will also be using zerolog as part of our example

go get -u github.com/rs/zerolog/log

Create a main module and HTTP event handler

Create a file 'main.go' with the following contents:

package main

import (
	"fmt"
	"github.com/manetu/lambda-sdk-go"
	"github.com/rs/zerolog/log"
	"os"
)

type context struct {
}

func (c context) Handler(request lambda.Request) lambda.Response {
	return lambda.Response{
		Status:  200,
		Headers: lambda.Headers{"Content-Type": "text/plain"},
		Body:    fmt.Sprintf("Hello, %s", request.Params["name"])}
}

func main() {
	lambda.Init(&context{})
	log.Print("Module initialized:", os.Environ())
}

Compile the program

The Manetu platform serves Lambda functions within a WebAssembly environment. We can leverage the WASI support in tinygo to compile our program.

tinygo build -o my-lambda.wasm --target=wasi main.go

You may now deploy my-lambda.wasm (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.