Running AWS Lambda Functions on Knative/Kubernetes Clusters
Clone or download
tzununbekov Merge pull request #22 from sebgoa/master
clarify the go example a bit
Latest commit 88afbdf Jan 15, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
go-1.x Copyrights and licenses added, #12 Jan 9, 2019
node-10.x one more escape issue Jan 13, 2019
node-4.x one more escape issue Jan 13, 2019
python-2.7 one more escape issue Jan 13, 2019
python-3.7 one more escape issue Jan 13, 2019
ruby-2.5 Base image name fix Jan 14, 2019
LICENSE Add apache license at the root Jan 9, 2019
README.md clarify the go example a bit Jan 15, 2019
cloudbuild.yaml Base image for ruby-2.5 runtime, #3 Jan 14, 2019
triggermeshklr.png add logo Dec 26, 2018

README.md

TriggerMesh Knative Lambda Runtime

Knative Lambda Runtimes (e.g KLR, pronounced clear) are Knative build templates that can be used to run an AWS Lambda function in a Kubernetes cluster installed with Knative.

The execution environment where the AWS Lambda function runs is a clone of the AWS Lambda cloud environment thanks to a custom AWS runtime interface and some inspiration from the LambCI project.

With these templates, you can run your AWS Lambda functions as is in a Knative powered Kubernetes cluster.

The examples below use the tm CLI to interact with Knative but one could also use kubectl:

Python

  1. Install buildtemplate
tm deploy buildtemplate -f https://raw.githubusercontent.com/triggermesh/knative-lambda-runtime/master/python-3.7/buildtemplate.yaml
  1. Deploy function
tm deploy service python-test -f https://github.com/serverless/examples \
                              --build-template knative-python37-runtime \
                              --build-argument DIRECTORY=aws-python-simple-http-endpoint \
                              --build-argument HANDLER=handler.endpoint \
                              --wait
  1. Execute function via public URL
curl python-test.default.dev.triggermesh.io

{"statusCode": 200, "body": "{\"message\": \"Hello, the current time is 06:45:49.174383\"}"}

To use Python 2.7 runtime simply replace version tag in step 1 and 2 with python-2.7 and knative-python27-runtime accordingly.

Nodejs

  1. Install node 4.3 buildtemplate
tm deploy buildtemplate -f https://raw.githubusercontent.com/triggermesh/knative-lambda-runtime/master/node-4.x/buildtemplate.yaml
  1. Deploy example function
tm deploy service node4-test -f https://github.com/serverless/examples \
                             --build-template knative-node4-runtime \
                             --build-argument DIRECTORY=aws-node-serve-dynamic-html-via-http-endpoint \
                             --build-argument HANDLER=handler.landingPage \
                             --wait
  1. Function is ready
curl http://node43-test.default.dev.triggermesh.io

{"statusCode":200,"headers":{"Content-Type":"text/html"},"body":"\n  <html>\n    <style>\n      h1 { color: #73757d; }\n    </style>\n    <body>\n      <h1>Landing Page</h1>\n      <p>Hey Unknown!</p>\n    </body>\n  </html>"}

Node 10 with async handler

  1. Prepare function code
mkdir example-lambda-nodejs
cd example-lambda-nodejs
cat > handler.js <<EOF
async function justWait() {
  return new Promise((resolve, reject) => setTimeout(resolve, 100));
}

module.exports.sayHelloAsync = async (event) => {
  await justWait();
  return {hello: event && event.name || "Missing a name property in the event's JSON body"};
};
EOF

node -e "require('./handler').sayHelloAsync({}).then(h => console.log(h))"
  1. Install node-10.x buildtemplate
tm deploy buildtemplate -f https://raw.githubusercontent.com/triggermesh/knative-lambda-runtime/master/node-10.x/buildtemplate.yaml
  1. Deploy function
tm deploy service node-lambda -f . --build-template knative-node10-runtime \
                                   --build-argument HANDLER=handler.sayHelloAsync \
                                   --wait

Done:

curl http://node-lambda.default.dev.triggermesh.io --data '{"name": "Foo"}'
# {"hello":"Foo"}

Go

  1. Prepare function code

You will create a main.go file in the example-lambda-go directory.

Create the directory and get into it:

mkdir example-lambda-go
cd example-lambda-go

Copy and Paste the following into a main.go file:

package main

import (
        "fmt"
        "context"
        "github.com/aws/aws-lambda-go/lambda"
)

type MyEvent struct {
        Name string `json:"name"`
}

func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
        return fmt.Sprintf("Hello %s!", name.Name ), nil
}

func main() {
        lambda.Start(HandleRequest)
}
EOF
  1. Install Go buildtemplate
tm deploy buildtemplate -f https://raw.githubusercontent.com/triggermesh/knative-lambda-runtime/master/go-1.x/buildtemplate.yaml
  1. Deploy function
tm deploy service go-lambda -f . --build-template knative-go-runtime --wait

Done:

curl http://go-lambda.default.dev.triggermesh.io --data '{"Name": "Foo"}'
"Hello Foo!"

Ruby

  1. Install Ruby 2.5 buildtemplate
tm deploy buildtemplate -f https://raw.githubusercontent.com/triggermesh/knative-lambda-runtime/master/ruby-2.5/buildtemplate.yaml
  1. Deploy example function
tm deploy service ruby-lambda -f https://github.com/serverless/examples --build-argument DIRECTORY=aws-ruby-simple-http-endpoint --build-argument HANDLER=handler.endpoint --build-template knative-ruby25-runtime --wait
  1. Function is ready
curl http://ruby-test-25.default.dev.triggermesh.io
{"statusCode":200,"body":"{\"date\":\"2019-01-14 19:10:29 +0000\"}"}

Support

We would love your feedback on this tool so don't hesitate to let us know what is wrong and how we could improve it, just file an issue

Code of Conduct

This plugin is by no means part of CNCF but we abide by its code of conduct