Deploying Functions to Knative The Serverless Framework Way
The Serverless Framework is one of the most widely used tools to deploy serverless functions to public cloud providers especially to AWS Lambda service. A typical workflow to deploy an AWS Lambda using serverless looks like this:
- serverless create
- serverless deploy
- serverless remove
With the advent of Knative, a Kubernetes extension that exposes Serverless primitives for auto-scaling including scaling to zero, traffic splitting and event management, it would be great to be able to support a similar UX than the serverless framework but targeting a Knative installation.
This post shows you how this can be achieved using tm the open-source TriggerMesh Knative client. To do this we are going to deploy a Python function.
Grab the latest release from the GitHub release page, make it executable and put it somewhere in your PATH, for example the linux binary:
Generate the scaffolding
To ease user on-boarding tm has a currently experimental feature to generate a scaffolding. To generate the scaffolding for a simple Python function do:
A directory named hello will be created with two files in it. Check the scaffolding:
You see a handler function and a serverless.yaml file, this is similar to what you would get with the serverless framework:
Deploy the function
To deploy the function you use a single command:
The --wait allows you to wait till all resources enter ready state. Shortly thereafter you will be presented with a URL for your function (i.e a Knative service):
You will be able to call it easily via curl:
How does this work
While this seems straightforward there is a bit of magic being the scene. That’s because the Python function being deployed does not have any invoker, it needs to be wrapped into a function runtime.
At TriggerMesh we have developed the Knative Lambda Runtime (https://github.com/triggermesh/knative-lambda-runtime) which can deploy an AWS Lambda function to a Knative cluster. We discussed it at length on the AWS blog and here.
With the scaffolding in place, the serverless.yaml manifest makes a reference to the KLR runtime:
In this runtime you will see a Task object that comes from the Tekton project. Hence in one command tm deploy we create a Task and execute it to build a container image that injects the function into our KLR AWS compatible runtime. This allows us to take an existing lambda function and deploy it to Knative in a Serverless framework manner.
Note that the manifest also contains the URL of the container registry where the resulting image will be stored.
Check the runtime URL to see how Tekton comes into play. With tm you can also list the resulting Task and Taskrun objects that have been created in the process:
This will delete all objects that have been created including the Tekton Task and TaskRun.
We hope you enjoyed it, if you have any feedback on the use of tm or Knative and Tekton don’t hesitate to contact us at firstname.lastname@example.org