Deploying Functions to Knative The Serverless Framework Way

Sebastien Goasguen
Sebastien Goasguen
Jul 16, 2019
Deploying Functions to Knative The Serverless Framework Way
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.

Install tm

Grab the latest release from the GitHub release page, make it executable and put it somewhere in your PATH, for example the linux binary:

chmod +x tm
mv tm /usr/local/bin

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:

tm generate python hello

A directory named hello will be created with two files in it. Check the scaffolding:

tree hello
hello
├── handler.py
└── serverless.yaml

You see a handler function and a serverless.yaml file, this is similar to what you would get with the serverless framework:

sls create -t aws-python -p foo
...
tree foo
foo
├── handler.py
└── serverless.yml

Deploy the function

To deploy the function you use a single command:

tm deploy -f hello --wait

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):

$ tm deploy -f hello --wait
Uploading "python" to demo-service-python-function-mcsgp-pod-6dfb31
Waiting for taskrun "demo-service-python-function-mcsgp" ready state
Creating "demo-service-python-function" service
Waiting for service "demo-service-python-function" ready state
Service demo-service-python-function URL: http://demo-service-python-function-sebgoa.k.triggermesh.io

You will be able to call it easily via curl:

{"message": "Hello, the current time is 07:51:46.853233"}

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:

service: demo-service
  description: Sample knative service

provider:
  name: triggermesh
  registry: knative.registry.svc.cluster.local

functions:
  python-function:
    source: handler.py
    runtime: https://raw.githubusercontent.com/triggermesh/runtime-build-tasks/master/aws-lambda/python37-runtime.yaml
    buildargs:
    - HANDLER=handler.endpoint
    environment:
      EVENT: API_GATEWAY

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:

$ tm get tasks
NAMESPACE NAME                              READY
sebgoa   demo-service-python-function-22sgr True  

$ tm get taskrun
NAMESPACE NAME                               READY
sebgoa    demo-service-python-function-l8pkm True 

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 info@triggermesh.com

Related Posts

Join our newsletter

Subscribe to TriggerMesh newsletter. You'll get only the most important news about TriggerMesh