Skip to content

Commit

Permalink
Update the blog to address review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Singh <[email protected]>
  • Loading branch information
viveksyngh committed Jul 10, 2020
1 parent c63fa24 commit e4efc7f
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions _posts/2020-07-08-event-driven-functions-with-openfaas-and-nats.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dark_background: true
---

In this blog post, I will show how you can invoke OpenFaaS function in response to messages sent on NATS topics in publish-subscribe model.
OpenFaaS functions are accessible over HTTP endpoints via gateway service but OpenFaaS provides several other way to invoke OpenFaaS functions with help of [connector-sdk](https://github.com/openfaas-incubator/connector-sdk). 
OpenFaaS functions are accessible over HTTP endpoints via gateway service but OpenFaaS provides several other way to invoke OpenFaaS functions with help of the [connector-sdk](https://github.com/openfaas-incubator/connector-sdk).The connector sdk provides a reusable and tested interface and implementation that allows developers to quickly create a new event source. The code that is unique to OpenFaaS is standardized so that the develop can focus on the details of the receiving message from the event source.

* [kafka-connector](https://github.com/openfaas-incubator/kafka-connector) connects OpenFaaS functions to Kafka topics.
* [nats-connector](https://github.com/openfaas-incubator/nats-connector) an OpenFaaS event-connector to trigger functions from NATS. 
Expand All @@ -24,17 +24,14 @@ There are several other connectors which allows you to trigger OpenFaaS function

## NATS

[NATS](https://nats.io) is a simple, secure and high performance open source messaging system for cloud native applications, IoT messaging, and microservices architectures. In this blog post, I will be using NATS publish-subscribe concept where publishers publishes a message on a topic/subject and subscribers consumes that message by subscribing to that topic/subject.
[NATS](https://nats.io) is a simple, secure and high performance open source messaging system for cloud native applications, IoT messaging, and microservices architectures. In this blog post, I will be using NATS publish-subscribe concept where publishers publishes a message on a topic/subject and subscribers consumes that message by subscribing to that subject (sometimes called topics in other event systems).

![NATS Publish Subscribe](/images/2020-openfaas-nats/nats-publish-subscribe.png "https://docs.nats.io/nats-concepts/pubsub")

## Pre-requisite Installation

* [k3d](https://github.com/rancher/k3d) to create a [k3s](https://github.com/rancher/k3s) cluster. k3s is lightweight distribution of kubernetes from Rancher Labs.
* `kubectl` to manage kubernetes cluster.
* `helm` to install `nats-connector` using helm chart
* [arkade](https://github.com/alexellis/arkade) to install OpenFaaS. `arkade` is simple CLI tool to install helm charts and apps to your cluster in one command.
* [faas-cli](https://github.com/openfaas/faas-cli) CLI tool to manage OpenFaaS functions.
* [arkade](https://github.com/alexellis/arkade) to install OpenFaaS, nats-connector, kubectl and faas-cl. `arkade` is simple CLI tool to install helm charts and apps to your cluster in one command.

> Please make sure you have these tools installed before you proceed next.
Expand All @@ -57,19 +54,26 @@ Install OpenFaaS using `arkade`
arkade install openfaas --set basic_auth=true --set functionNamespace=openfaas-fn
```

Follow instructions printed to obtain the gateway password and login with `faas-cli`

#### Install NATS connector using helm chart
#### Install NATS connector using arkade
`nats-connector` is connector which invokes OpenFaaS function when a message is sent to a NATS topic.

```
helm repo add nats-connector https://openfaas.github.io/faas-netes
arkade install nats-connector
```

> NATS comes with OpenFaaS installation, so we are not setting up NATS here
#### Install kubectl and faas-cli

helm install nats-connector openfaas/nats-connector --namespace=openfaas
install `kubectl` and `faas-cli` using `arkade` if they are not already installed.

```
arkade get kubectl
```

> NATS comes with OpenFaaS installation, so we are not setting up NATS here
```
arkade get faas-cli
```

#### Login to OpenFaaS gateway using CLI

Expand Down Expand Up @@ -102,29 +106,30 @@ echo -n $PASSWORD | faas-cli login --username admin --password-stdin
Deploy `receive-message` function

```
faas-cli deploy --name receive-message --image openfaas/receive-message:latest --fprocess='./handler' --annotation topic="nats-test"
faas-cli deploy --name receive-message --image openfaas/receive-message:latest \
--fprocess='./handler' --annotation topic="nats-test"
```

Deploy `publish-message` function
```
faas-cli deploy --name publish-message --image openfaas/publish-message:latest --fprocess='./handler' --env nats_url=nats://nats.openfaas:4222
faas-cli deploy --name publish-message --image openfaas/publish-message:latest \
--fprocess='./handler' --env nats_url=nats://nats.openfaas:4222
```

> Note: You can also build and deploy this function using the stack.yml and code present in nats-connector repository. But for simplicity, I am using pre built and published images of these functions.
## Verify the installation
Invoke `publish-message` function to publish a test message

```
faas-cli invoke publish-message <<< "test message"
echo "test message" | faas-cli invoke publish-message
```

When `publish-message` was invoked, it would have pushed `test-message` to the `nats-test` topic on NATS, which would have invoked `receive-message`. We can verify that by checking logs of `receive-message` function.

```
faas-cli logs receive-message
ARNING! Communication is not secure, please consider using HTTPS. Letsencrypt.org offers free SSL/TLS certificates.
Handling connection for 8080
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Started logging stderr from function.
2020-05-29T15:41:17Z 2020/05/29 15:41:17 Started logging stdout from function.
2020-05-29T15:41:17Z Forking - ./handler []
Expand All @@ -137,10 +142,11 @@ Handling connection for 8080
2020-05-29T15:42:24Z 2020/05/29 15:42:24 POST / - 200 OK - ContentLength: 28
```

### What Next ?
## What Next ?
* Do you have a cool "event driven" use case you want to share? Let us know and become a guest blogger!

If you are new to OpenFaaS, I would recommend you trying [OpenFaaS workshop](https://github.com/openfaas/workshop) .
* If you are new to OpenFaaS, I would recommend you trying [OpenFaaS workshop](https://github.com/openfaas/workshop) .

If you don't find connector for messaging platform you are using, checkout the [connector-sdk](https://github.com/openfaas-incubator/connector-sdk) which allows you to build event-connectors for OpenFaaS.
* If you don't find connector for messaging platform you are using, checkout the [connector-sdk](https://github.com/openfaas-incubator/connector-sdk) which allows you to build event-connectors for OpenFaaS.

If you are looking to contribute to open source project, please join OpenFaaS community [slack channel](https://docs.openfaas.com/community/) and start contributing.
* If you are looking to contribute to open source project, please join OpenFaaS community [slack channel](https://docs.openfaas.com/community/) and start contributing.

0 comments on commit e4efc7f

Please sign in to comment.