{"id":13626066,"url":"https://github.com/triggermesh/knative-lambda-runtime","last_synced_at":"2025-04-16T11:31:15.243Z","repository":{"id":46227855,"uuid":"162238629","full_name":"triggermesh/knative-lambda-runtime","owner":"triggermesh","description":"Running AWS Lambda Functions on Knative/Kubernetes Clusters","archived":true,"fork":false,"pushed_at":"2023-09-18T23:37:47.000Z","size":3362,"stargazers_count":274,"open_issues_count":9,"forks_count":22,"subscribers_count":13,"default_branch":"main","last_synced_at":"2024-12-20T00:34:09.426Z","etag":null,"topics":["amazon","knative","kubernetes","lambda","management","serverless"],"latest_commit_sha":null,"homepage":"https://triggermesh.com","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/triggermesh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-12-18T06:06:45.000Z","updated_at":"2024-08-13T03:40:01.000Z","dependencies_parsed_at":"2024-01-07T18:09:28.820Z","dependency_job_id":null,"html_url":"https://github.com/triggermesh/knative-lambda-runtime","commit_stats":null,"previous_names":[],"tags_count":48,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/triggermesh%2Fknative-lambda-runtime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/triggermesh%2Fknative-lambda-runtime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/triggermesh%2Fknative-lambda-runtime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/triggermesh%2Fknative-lambda-runtime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/triggermesh","download_url":"https://codeload.github.com/triggermesh/knative-lambda-runtime/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249235046,"owners_count":21235137,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["amazon","knative","kubernetes","lambda","management","serverless"],"created_at":"2024-08-01T21:02:09.106Z","updated_at":"2025-04-16T11:31:12.520Z","avatar_url":"https://github.com/triggermesh.png","language":"Python","funding_links":[],"categories":["Python","Serverless Platforms"],"sub_categories":[],"readme":"**Note**: Tekton pipelines and [tm](https://github.com/triggermesh/tm) CLI are no longer supported as a part of TriggerMesh distribution. `tm`-based KLR examples below are not guaranteed to work properly on the latest versions of Knative or Tekton.\nThe latest KLR versions are maintained as a base for TriggerMesh [Function](https://docs.triggermesh.io/1.24/transformation/functions/) objects.\n\n___\n\n![TriggerMesh Knative Lambda Runtime](./triggermeshklr.png \"TriggerMesh Knative Lambda Runtime\")\n\nKnative Lambda Runtimes (e.g KLR, pronounced _clear_) are Tekton [Tasks](https://github.com/tektoncd/pipeline/blob/master/docs/tasks.md) that can be used to run an AWS Lambda function in a Kubernetes cluster installed with Knative.\n\nThe execution environment where the AWS Lambda function runs is a clone of the AWS Lambda cloud environment thanks to a custom [AWS runtime interface](https://github.com/triggermesh/aws-custom-runtime) and some inspiration from the [LambCI](https://github.com/lambci/docker-lambda) project.\n\nWith these templates, you can run your AWS Lambda functions **as is** in a Knative powered Kubernetes cluster.\n\nThe examples below use the [tm](https://github.com/triggermesh/tm/releases/latest) CLI to interact with Knative but one could also use `kubectl`.\n\n### Docker registry for builds\n\nTo combine the runtime with your source, the examples below produce a new Docker image each time.\nWhile these images can be considered temporary,\nbuilds must be pushed to a Docker registry in order for Kubernetes to be able to pull.\nBy default `tm` uses [Knative Local Registry](https://github.com/triggermesh/knative-local-registry),\nequivalent to adding `--registry-host knative.registry.svc.cluster.local` to the commands below,\nso that builds can run without registry authentication.\nTo override, set `--registry-secret` according to [tm docs](https://github.com/triggermesh/tm#docker-registry).\n\n### Concurrency\n\nConcurrency in KLR represented by two components: parallel running [bootstrap](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html) processes per container and Knative [container concurrency](https://github.com/knative/serving/blob/master/docs/spec/spec.md#revision) model. By default [AWS runtime interface](https://github.com/triggermesh/aws-custom-runtime) fires up 4 bootstrap processes (functions, in other words) and allows multiple concurrent requests (`containerConcurrency: 0`) to be handled by each container. Default concurrency configuration can be changed on function deployment or update using `tm deploy service` command parameters:\n\n`--concurrency \u003cN\u003e` - sets Knative service `containerConcurrency` value to `N`\n\n`--build-argument INVOKER_COUNT=\u003cN\u003e` - passes number of parallel running functions to AWS lambda runtime\n\nValues for these two parameters should be calculated individually for each function and depends on operation characteristics. Knative [autoscaling](https://github.com/knative/docs/blob/master/docs/serving/samples/autoscale-go/README.md) is another important factor that affects service performance, but right now KLR uses default autoscaling configuration.\n\n### Examples\n\nNOTE: all examples below work with [Local Registry](https://github.com/triggermesh/knative-local-registry). If you don't have local registry in knative cluster, you can use external registry as discribed in CLI [documentation](https://github.com/triggermesh/tm#docker-registry)\n\n\n#### Python\n\n1. Install runtime\n\n```\ntm deploy task -f https://raw.githubusercontent.com/triggermesh/knative-lambda-runtime/main/python37/runtime.yaml\n```\n\n2. Deploy [function](https://github.com/serverless/examples/tree/master/aws-python-simple-http-endpoint)\n\n```\ntm deploy service python-test -f https://github.com/serverless/examples \\\n                              --build-template knative-python37-runtime \\\n                              --build-argument DIRECTORY=aws-python-simple-http-endpoint \\\n                              --build-argument HANDLER=handler.endpoint \\\n                              --wait\n```\n\n3. Execute function via public URL\n\n```\ncurl python-test.default.dev.triggermesh.io\n\n{\"statusCode\": 200, \"body\": \"{\\\"message\\\": \\\"Hello, the current time is 06:45:49.174383\\\"}\"}\n```\n\n\nTo use Python 2.7 runtime simply replace version tag in step 1 and 2 with `python27` and `knative-python27-runtime` accordingly.\n\n\n#### Nodejs\n\n1. Install node 4.3 runtime\n\n```\ntm deploy task -f https://raw.githubusercontent.com/triggermesh/knative-lambda-runtime/main/node4/runtime.yaml\n```\n\n2. Deploy example function\n\n```\ntm deploy service node4-test -f https://github.com/serverless/examples \\\n                             --build-template knative-node4-runtime \\\n                             --build-argument DIRECTORY=aws-node-serve-dynamic-html-via-http-endpoint \\\n                             --build-argument HANDLER=handler.landingPage \\\n                             --wait\n```\n\n3. Function is ready\n\n```\ncurl http://node43-test.default.dev.triggermesh.io\n\n{\"statusCode\":200,\"headers\":{\"Content-Type\":\"text/html\"},\"body\":\"\\n  \u003chtml\u003e\\n    \u003cstyle\u003e\\n      h1 { color: #73757d; }\\n    \u003c/style\u003e\\n    \u003cbody\u003e\\n      \u003ch1\u003eLanding Page\u003c/h1\u003e\\n      \u003cp\u003eHey Unknown!\u003c/p\u003e\\n    \u003c/body\u003e\\n  \u003c/html\u003e\"}\n```\n\n#### Node 10 with `async` handler\n\n1. Prepare function code\n\n```\nmkdir example-lambda-nodejs\ncd example-lambda-nodejs\ncat \u003e handler.js \u003c\u003cEOF\nasync function justWait() {\n  return new Promise((resolve, reject) =\u003e setTimeout(resolve, 100));\n}\n\nmodule.exports.sayHelloAsync = async (event) =\u003e {\n  await justWait();\n  return {hello: event \u0026\u0026 event.name || \"Missing a name property in the event's JSON body\"};\n};\nEOF\n\nnode -e \"require('./handler').sayHelloAsync({}).then(h =\u003e console.log(h))\"\n```\n\n2. Install node10 runtime\n\n```\ntm deploy task -f https://raw.githubusercontent.com/triggermesh/knative-lambda-runtime/main/node10/runtime.yaml\n```\n\n3. Deploy function\n\n```\ntm deploy service node-lambda -f . --build-template knative-node10-runtime \\\n                                   --build-argument HANDLER=handler.sayHelloAsync \\\n                                   --wait\n```\n\nDone:\n\n```\ncurl http://node-lambda.default.dev.triggermesh.io --data '{\"name\": \"Foo\"}'\n# {\"hello\":\"Foo\"}\n```\n\n#### Go\n\n1. Prepare function code\n\nYou will create a `main.go` file in the `example-lambda-go` directory.\n\nCreate the directory and get into it:\n\n```\nmkdir example-lambda-go\ncd example-lambda-go\n```\n\nCopy and Paste the following into a `main.go` file:\n\n```\npackage main\n\nimport (\n        \"fmt\"\n        \"context\"\n        \"github.com/aws/aws-lambda-go/lambda\"\n)\n\ntype MyEvent struct {\n        Name string `json:\"name\"`\n}\n\nfunc HandleRequest(ctx context.Context, name MyEvent) (string, error) {\n        return fmt.Sprintf(\"Hello %s!\", name.Name ), nil\n}\n\nfunc main() {\n        lambda.Start(HandleRequest)\n}\n```\n\n2. Install Go runtime\n\n```\ntm deploy task -f https://raw.githubusercontent.com/triggermesh/knative-lambda-runtime/main/go/runtime.yaml\n```\n\n3. Deploy function\n\n```\ntm deploy service go-lambda -f . --build-template knative-go-runtime --wait\n```\n\nDone:\n\n```\ncurl http://go-lambda.default.dev.triggermesh.io --data '{\"Name\": \"Foo\"}'\n\"Hello Foo!\"\n```\n\n##### Dependecies\n\nTo have more control over the go project dependecies, KLR runtime will read and \"ensure\" `Gopkg.toml` file if it's available in the project's root. Without toml file, only `go get` will be executed before build.\nIf the project has dependencies stored in a private repository, you can create k8s secret with SSH key that will be used for `git clone` operation:\n\n```\ncat ~/.ssh/id_rsa | tm set git-auth\n```\nwhere `~/.ssh/id_rsa` is a path to SSH private key associated with your git account\n\n#### Ruby\n\n1. Install Ruby 2.5 runtime\n\n```\ntm deploy task -f https://raw.githubusercontent.com/triggermesh/knative-lambda-runtime/main/ruby25/runtime.yaml\n```\n\n2. Deploy example function\n\n```\ntm 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\n```\n\n3. Function is ready\n\n```\ncurl http://ruby-test-25.default.dev.triggermesh.io\n{\"statusCode\":200,\"body\":\"{\\\"date\\\":\\\"2019-01-14 19:10:29 +0000\\\"}\"}\n```\n\n#### Java8\n\n1. Clone serverless [examples](https://github.com/serverless/examples) repository, change directory to `aws-java-simple-http-endpoint`, edit `serverless.yaml`:\n\n```\nservice: aws-java-sample\ndescription: Triggermesh Java8 sample\nprovider:\n  name: triggermesh\nfunctions:\n  java-function:\n    source: ../aws-java-simple-http-endpoint\n    runtime: https://raw.githubusercontent.com/triggermesh/knative-lambda-runtime/main/java8/runtime.yaml\n    buildargs:\n    - HANDLER=com.serverless.Handler\n    environment:\n      EVENT_TYPE: \"API_GATEWAY\"\n```\n\n2. Remove incompatible leftovers from `build.gradle` file:\n\n```\ntask wrapper(type: Wrapper) {\n    gradleVersion = '3.2.1'\n}\n```\n\n3. Deploy:\n\n```\ntm deploy\n```\n\nFunction is ready:\n\n```\ncurl http://aws-java-sample-java-function.default.dev.triggermesh.io -d '{\"event\":\"foo\"}'\n{\"message\":\"Hello, the current time is Tue Apr 07 13:59:17 GMT 2020\"}\n```\n\n### Run in Docker\n\nFor cases in which the use of additional components (tm CLI, Tekton, Knative, k8s) is undesirable, it is possible to build a KLR function as a standalone Docker container and run it in any environment. To do this, you should extract the Dockerfile from the runtime you are interested in, put it in the directory with your function, update the handler variable, and build the container. Here are Dockerfile definitions for all runtimes:\n\n- [go](https://github.com/triggermesh/knative-lambda-runtime/blob/main/go/runtime.yaml#L44-L68)\n- [java](https://github.com/triggermesh/knative-lambda-runtime/blob/main/java8/runtime.yaml#L43-L53)\n- [node-10](https://github.com/triggermesh/knative-lambda-runtime/blob/main/node10/runtime.yaml#L43-L48)\n- [node-4](https://github.com/triggermesh/knative-lambda-runtime/blob/main/node4/runtime.yaml#L43-L48)\n- [python-2](https://github.com/triggermesh/knative-lambda-runtime/blob/main/python27/runtime.yaml#L43-L50)\n- [python-3](https://github.com/triggermesh/knative-lambda-runtime/blob/main/python37/runtime.yaml#L43-L50)\n- [ruby-2](https://github.com/triggermesh/knative-lambda-runtime/blob/main/ruby25/runtime.yaml#L43-L47)\n\nLet's build a Python 3.7 function as an example:\n\n1. Create directory and save your function code there:\n\n```\n  mkdir python\n  cd python\n  cat \u003e handler.py \u003c\u003cEOF\nimport json\nimport datetime\ndef endpoint(event, context):\n  current_time = datetime.datetime.now().time()\n  body = {\n      \"message\": \"Hello, the current time is \" + str(current_time)\n  }\n  response = {\n      \"statusCode\": 200,\n      \"body\": json.dumps(body)\n  }\n  return response\nEOF\n```\n\n2. Extract the runtime's [Dockerfile](https://github.com/triggermesh/knative-lambda-runtime/blob/master/python37/runtime.yaml#L43-L50), store it in the same directory, and update the `_HANDLER` variable:\n\n```\n  cat \u003e Dockerfile \u003c\u003cEOF\n  FROM gcr.io/triggermesh/knative-lambda-python37\n  ENV _HANDLER handler.endpoint\n  COPY . .\n  RUN if [ -f requirements.txt ]; then pip3.7 install -r requirements.txt ;fi\n  ENTRYPOINT [\"/opt/aws-custom-runtime\"]\nEOF\n```\n\nThe `_HANDLER` variable in most cases consists of the filename without the file extension, and the function name.\n\n3. Build, run, test:\n\n```\n  docker build -t python-klr-image .\n  docker run -d --rm --name python-klr-container python-klr-image\n  # following command will work if you use Docker bridge network and you have jq tool\n  # otherwise, you should get the container address manually\n  curl $(docker inspect python-klr-container | jq .[].NetworkSettings.Networks.bridge.IPAddress -r):8080\n```\n\n  The response will contain a JSON document with the current time.\n\n4. Cleanup:\n\n```\n  docker stop python-klr-container\n```\n\nThe image can be pushed to the container registry and used to create Knative Service:\n\n```\nkubectl apply -f - \u003c\u003cEOF\napiVersion: serving.knative.dev/v1\nkind: Service\nmetadata:\n  name: python-klr-service\nspec:\n  template:\n    spec:\n      containers:\n        - image: \u003cpython-klr-image\u003e\nEOF\n```\n\n### Support\n\nWe 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](https://github.com/triggermesh/knative-lambda-runtime/issues/new)\n\n### Code of Conduct\n\nThis plugin is by no means part of [CNCF](https://www.cncf.io/) but we abide by its [code of conduct](https://github.com/cncf/foundation/blob/master/code-of-conduct.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftriggermesh%2Fknative-lambda-runtime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftriggermesh%2Fknative-lambda-runtime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftriggermesh%2Fknative-lambda-runtime/lists"}