How to develop a serverless app with OpenFaaS and Okteto
Tags: serverless, cloudcomputing, programming, kubernetes
Published: 2019-08-13 15:52:04

---
title: How to develop a serverless app with OpenFaaS and Okteto
published: true
canonical_url: https://medium.com/okteto/how-to-develop-a-serverless-app-with-openfaas-and-okteto-d85435f0eca1
series: Cloud Native Development
---

### How to Develop a Serverless App with OpenFaaS and Okteto

![](https://cdn-images-1.medium.com/max/1024/1*Q5JCUZlucY-FikYWj6o_Zw.jpeg)<figcaption>Okteto + OpenFaas</figcaption>

OpenFaaS (Functions as a Service) is a framework for building serverless functions with Docker and Kubernetes.

[OpenFaaS](https://docs.openfaas.com/) simplifies your application by helping you package your application logic in discrete packages that react to web events. Instead of having to deploy tens of pods to keep your application running at scale, OpenFaaS scales your functions automatically and independently based on web events and metrics.

On this blog post we'll show you how to deploy your own instance of OpenFaaS, launch your first function and how to develop it. Then we'll show you how you can use the Okteto CLI to accelerate your serverless development even more.

### Deploy OpenFaaS in Okteto Cloud

_If you already have your own installation of OpenFaaS, feel free to skip to the next step._

For this post, we'll deploy OpenFaaS into [Okteto Cloud](https://cloud.okteto.com). Okteto Cloud is a [self-service, multi-tenant Kubernetes cluster](https://okteto.com/docs/cloud) optimized for team collaboration and Cloud Native development.

Log in to [Okteto Cloud](https://cloud.okteto.com) and click on the Deploy button. Switch the deploy method to **Deploy from Helm Chart**, select OpenFaaS, pick a password, and click deploy.  Your OpenFaaS instance will be up and running in a matter of seconds.  

![Deploy OpenFaaS in okteto](https://dev-to-uploads.s3.amazonaws.com/i/hl48fjdbmtd752bdc3in.png)

OpenFaaS includes a web gateway as part of the deployment, which can be used to see your functions, create new ones, and to invoke them. Okteto Cloud automatically created an ingress for your OpenFaaS gateway. Open [Okteto Cloud](https://cloud.okteto.com) in your browser and click on the gateway URL to access it.


When opening the gateway the first time you will be prompted for credentials. The user is admin and the password will be the content of the `Password` field you provided in the deployment dialog (it defaults to `Password123!`).

![The Gateway UI via Okteto](https://dev-to-uploads.s3.amazonaws.com/i/3oc3vxea7c05zuo4zvr5.png)

### Install the OpenFaaS CLI

We need the OpenFaaS CLI available locally to deploy a function. If you are in Mac or Linux, run the command below to install it:

```
$ curl -sL cli.openfaas.com | sudo sh
```

On Windows download the latest faas-cli.exe from the [releases page](https://github.com/openfaas/faas-cli/releases) and place it somewhere in you $PATH.

Validate that it was installed correctly by opening a terminal and running:

```
$ faas-cli help
$ faas-cli version
```

To access the OpenFaaS gateway from the CLI, you need to export the `$OPENFAAS_URL` and `$OPENFAAS_PASSWORD` environment variables (you can get your gateway's URL from [Okteto Cloud's](https://cloud.okteto.com)) and login using the faas-cli login command:

```
export OPENFAAS_URL=$OPENFAAS_GATEWAY_URL
export OPENFAAS_PASSWORD="Password123!"
$ echo $OPENFAAS_PASSWORD | faas-cli login -u admin --password-stdin

Calling the OpenFaaS server to validate the credentials...
credentials saved for admin https://gateway-rberrelleza.cloud.okteto.net
```
### Deploy your first function

Now that we have our instance of OpenFaaS, it’s time to deploy our first function.

OpenFaaS supports pretty much any programming language, but since I'm a huge golang fan, we'll use that for this post. Use the `faas-cli` new command to create all the necessary files.

```
$ faas-cli new -lang go gohash
...
  ___                   _____           ____
 / _ \ _ __   ___ _ __ |  ___|_ _  __ _/ ___|
| | | | '_ \ / _ \ '_ \| |_ / _` |/ _` \___ \
| |_| | |_) |  __/ | | |  _| (_| | (_| |___) |
 \___/| .__/ \___|_| |_|_|  \__,_|\__,_|____/
      |_|Function created in folder: gohash
Stack file written: gohash.yml
...
```

We now have a folder called gohash with a file called `handler.go`, this is the main code of our function. We also have a file called `gohash.yml` with the function metadata, and a folder called template with the different function templates available.

Open `gohash.yml` with your favorite text editor and put your Docker Hub account into the `image:` section. i.e. `image: okteto/gohash:latest`.

Then, run the faas-cli up command to build, push and deploy your function.

> The faas-cli up command uses your local Docker client to build and push the images. It requires you to be logged to Docker Hub or a similar registry.

```
$ faas-cli up -f gohash.yml
[0] > Building gohash.
...
[0] < Building gohash done.
[0] worker done.

[0] > Pushing gohash [ramiro/gohash:latest].
...
[0] < Pushing gohash [ramiro/gohash:latest] done.
[0] worker done.

Deploying: gohash.

Deployed. 202 Accepted.
URL: https://gateway-openfaas-rberrelleza.cloud.okteto.net/function/gohash
```

Once your function has been deployed, we'll use the gateway's UI to invoke it. Open the gateway in your browser, click on the gohashon the left, type testas the Request body, and click the _Invoke _button.

![](https://cdn-images-1.medium.com/max/1024/1*X4w2Z2YOqM6hZDUqksVuTA.png)<figcaption>Your first OpenFaaS function</figcaption>

### Develop your function

Once our function is up and running, let's add a simple feature. Instead of just echoing back the input, we'll calculate the checksum of the string and return that instead.

Open `gohash/handler.go` with your favorite editor, and update it:

{% gist https://gist.github.com/rberrelleza/5032aceb18e31c11e81d8e5060285ea3 %}

Save the file, and build , push and deploy your function with the faas-cli up command.

```
$ faas-cli up -f gohash.yml
...
...
Deployed. 202 Accepted.
URL: https://gateway-rberrelleza.cloud.okteto.net/function/gohash
```

Wait 30 seconds, go back to the gateway's UI and invoke the function again to verify your code change:

![](https://cdn-images-1.medium.com/max/1024/1*DBFy15YsTVbQVJWrEAIdKQ.png)<figcaption>Result of the function after adding the checksum</figcaption>

You'll need to edit the `handler.go` file and run faas-cli up every time you want to deploy a new change.

### Develop your function, Cloud Native style

OpenFaaS offers you a great experience when building functions (way better and AWS Lambda and GCP's Cloud Functions, in my opinion), but having to build, push and redeploy every time you want to change a line of code adds a lot of friction to my inner loop.

What if instead of building, pushing and deploying, we take advantage of [Okteto](https://okteto.com) and develop our function directly in the cluster, Cloud Native style?

Okteto uses [a manifest](https://okteto.com/docs/reference/manifest/index.html) to know how to build and deploy your development environment. Create an file called gohash/okteto.yml, with the following content:

```
name: go-dev
image: okteto/openfaas:golang
mountpath: /go/src/handler/function
command: ["bash"]
securityContext:
 fsGroup: 100
services:
 - name: gohash
 mountpath: /home/app/src
 environment:
 - fprocess=src/handler
```

Go to your terminal, navigate to the gohash folder and run the okteto up command to start your remote development environment:

```
$ cd gohash
$ okteto up

Deployment 'go-dev' doesn't exist. Do you want to create a new one? [y/n]: y
 ✓ Persistent volume provisioned
 ✓ Files synchronized
 ✓ Okteto Environment activated
 Namespace: rberrelleza
 Name: go-dev

Welcome to your development environment. Happy coding!
okteto>
```

Build and install the binary in your remote environment:

```
okteto> go install
```

> If you want to use go build instead, call it like this: `go build -o /go/src/handler/function` for OpenFaaS to pick it up the changes.

Go back to the browser, and test the function again:

![](https://cdn-images-1.medium.com/max/1024/1*1_7pwxX8ZJcGp6ucg85KTw.png)<figcaption>Result of the function running with Okteto</figcaption>

Now, let's implement a second feature: We'll include a timestamp in the response. Open `gohash/handler.go`.go with your favorite editor and update it:

{% gist https://gist.github.com/rberrelleza/a71a467d909ce714c083143e7283d450 %}

Save your file, go back to your terminal, and build your function again.

```
okteto> go install
```

Go back to your browser, and test your function again:

![](https://cdn-images-1.medium.com/max/1024/1*8wtFESfxpG529JQec3qt6A.png)<figcaption>Result of the function after adding the timestamp</figcaption>

Our change made it to the function! How did this happen? With Okteto your changes were automatically applied to the remote containers as soon as you saved them. This way you can execute native go builds, leveraging golang's caches and incremental builds to see your changes in seconds. No commit, build, push or redeploy required 💪!

> Once you're done developing, run okteto down to return everything to the previous state.

### Whoa, how does it works?

When you run `okteto up`, Okteto enables what we call _development mode_. Development Mode is what enables you to test your changes directly in the cluster, instead of having to build, push and redeploy.

After running `okteto up` the following events happen:

1. Okteto creates a persistent volume on Okteto Cloud.
2. A bi-directional synchronization service is started between your local machine and the persistent volume.
3. Okteto launches your development environment, using the image you defined in your okteto.yml, with your persistent volume mounted. The deployment will use the image defined in your manifest. In this case, we are using `okteto/openfaas:golang`, an image that has all the tools you need to develop golang-based functions already preinstalled.
4. Okteto relaunches all the deployments defined in the services section of your manifest (in this case, your function). The deployment is slightly modified to have your persistent volume mounted in `mountpath`, and the environment variable `fprocess` injected.
5. A shell was opened into your development environment.

![](https://cdn-images-1.medium.com/max/1024/1*WxkSwFKdGOVBlAMrSWt0cA.jpeg)<figcaption>Your function in Okteto's development mode</figcaption>

Every time you change a file (e.g when you add the timestamp code), the code is synchronized between your machine and your remote environment. And every time you compile your go binary, the updated version is available both in your development environment and in your function's deployment (since they both share the same volume).

This is the 'magic' that allows you to validate your changes directly in the cluster, no commit, build, push or redeploy required. [Join us on slack](https://kubernetes.slack.com/messages/CM1QMQGS0/) to talk more about Okteto's features, architecture, and use cases!

### Conclusions

We just built and deployed our first function in OpenFaaS in minutes.

OpenFaaS makes it simple to turn anything into a serverless function that runs on Linux or Windows through Kubernetes. [Learn more about OpenFaaS here](https://openfaas.com).

And then, we used [Okteto](https://okteto.com) to show you the advantages of developing directly in Kubernetes while keeping the same developer experience than working on a local machine. By developing directly in the cluster you not only gain speed, you also avoid the burden of having to keep Kubernetes and OpenFaaS running in your local machine.

Working on the Cloud is always better. You don’t work on your spreadsheets and listen to media files locally, do you? Stop dealing with local environments and [become a Cloud Native Developer](https://okteto.com) today!

> Interested in boosting your team's Kubernetes development workflows? [Contact us](https://okteto.com/#sales) to start running Okteto Enterprise in your own infrastructure today.