Your first .NET Core Serverless function on OpenFaas

Screenshot 2019-05-08 at 20.16.15In my previous blog I described how to set-up OpenFaas on a Kubernetes cluster with a little help from Rancher. Now it’s time to deploy our first serverless function. In this blog we’ll create and deploy a .NET Core function that reads a bit of config using both an environment variable and Kubernetes secrets.

Creating your first function

In the previous blog we installed the OpenFaas cli. If you didn’t already, here are the instructions. Create a new directory.

Now run the following command to create the function

Run the ‘ls’ command to view the output. There are two folders, template and yourfirstfunction, and the yourfirstfunction.yml. The yml file describes the function and will be used to build and deploy it. We will see it in more detail in a bit. Open the project in your favorite editor and open the FunctionHandler.cs. This is the entrypoint for this function. Data that comes in will always be present as a string. You’ll probably always post a Json object so let’s add some code to convert the input. I added a simple ‘Person’ model and the Newtonsoft.Json library and pretended to add it to a database.

Before we start adding config like the sql username and password, let’s first deploy and run this function. There are two settings we need to change in the yourfirstfunction.yml before we can do that. First, we have to modify the gateway url to point to OpenFaas on our Kubernets cluster instead of a local instance. Second, we will be pushing the image to Docker Hub so we also need to change the image. Don’t forget to change the version of the image. After these changes, this is what my yourfirstfunction.yml looks like:

Time to deploy! Run the following three commands, they will explain themselves. Make sure you are logged-in to your Docher Hub account, otherwise the push will not work.

Running these three commands every time you wish to redeploy your function isn’t ideal, especially when you start adding some repetitive flags to these commands. Luckily, there’s also the ‘up’ command which combines these three commands:

Open the OpenFaas portal and select the new function. Add some json to the body and hit invoke. You should have a similar result:

Screenshot 2019-05-06 at 21.55.56

 

One intermezzo before we start adding some config. After the build command a build folder was added to our projects folder. The files from the template folder we saw earlier were used to create this content. Have a look at both the Dockerfile and the program.cs to get a better understanding of what’s going on here with OpenFaas.

Let’s add some config!

Every app needs a little config. OpenFaas supports two ways of retrieving config; environment variables and secrets. We will add one of both in this example. First, let’s change the app to read the config. After the change, this is my function:

There’s nothing special about the environment variable. Secrets however are mounted as files to your function at runtime and are always located at /var/openfaas/secrets/{secretName}. Here is a more detailed description of how secrets work in OpenFaas. The blog describes how to add your secret using the command line tooling of Kubernetes, Swarm and even OpenFaas. I’ve used kubectl:

Add the following configuration to the yourfirstfunction.yml right under the image tag to add the environment variable and let OpenFaas know which secret to mount.

Change the version of the image to 1.1 and redeploy! You’re output should be similar to this:

Screenshot 2019-05-06 at 22.25.44

That’s it; your first .Net Core serverless function on OpenFaas. In the next blog I will show you how you can publish a message to a Kafka topic have another function triggered by that and receive the message.

You may also like...