Using Serverless Framework With Knative on OpenShift

Serverless Framework depends on Knative to deploy a serverless function on OpenShift, and then all you need is to run serverless deploy

If you didn't already know, Serverless Framework supports Node.js, Python, Java, Go, C#, F#, Ruby, Swift, Kotlin, PHP and Scala


After all the prerequisites are met, let's get started.

First, we want to login to serverless, just type serverless login in your terminal, and it'll guide you.

Then, we'll need to clone a project running the command serverless create -u <github-link>, you can pick any of these sample projects or setup your own. I will use npx serverless init knative-express-starter to create a sample project locally.

Serverless Framework, requires a file called serverless.yml as you can see it exists in all the sample projects.

The one I generated looks like this:

app: knative-express-starter
name: knative-express-starter
component: knative
src: ./src

Nothing needs to change here other than the app and name values.

What we need to look at next is the .env file

Initially it looks like this:

# Learn more about about the credentials needed
# to deploy your app
# AWS:
# AWS_ACCESS_KEY_ID=<your access key>
# AWS_SECRET_ACCESS_KEY=<your secret access key>

And for Knative to work, it requires these environment variables, so we change it to:

KUBERNETES_ENDPOINT= # or run oc whoami --show-server
KUBERNETES_SERVICE_ACCOUNT_TOKEN= # run oc whoami --show-token

Now you can run serverless deploy or serverless deploy -v to display logs

You can also check the status of your deployment on dashboard, or check your OpenShift Web Console, where your function will deploy to a newly created namespace equal to your app's name. e.g knative-express-starter-sdaf1a

To read more about how to set up your serverless with Knative and more advanced configs, head over to Serverless Framework's Knative Introduction Guide