Skip to content

Instantly share code, notes, and snippets.

@alexellis alexellis/README.md
Last active Nov 25, 2018

Embed
What would you like to do?
OpenFaaS Redis example

Redis & OpenFaaS micro-tutorial

After you've completed this micro-tutorial you'll be making requests to your Redis cache from serverless functions with OpenFaaS. From there it's up to you to build something awesome.

Pre-reqs:

Deploy OpenFaaS and the faas-cli.

This guide is for OpenFaaS on Kubernetes, but if you're using Swarm that's OK - you'll just have to adapt some of the commands for setting up Redis. The OpenFaaS code will be the same.

Deploy Redis with helm

helm install stable/redis --name openfaas-redis --namespace openfaas-fn --set usePassword=false --set master.persistence.enabled=false

Build / deploy function:

$ faas-cli new --lang node redis-fn --prefix docker-hub-name

# copy handler.js into ./redis-fn/

# copy package.json into ./redis-fn/

# Append to stack.yml

    environment:
      redis: openfaas-redis-master

$ faas-cli up

Invoke

$ echo | faas-cli invoke redis-fn

Check Redis for the value

$ kubectl port-forward --namespace openfaas-fn svc/openfaas-redis-master 6379:6379 &redis-cli -h 127.0.0.1 -p 6379

127.0.0.1:6379> GET mytime
"Thu Nov 01 2018 08:10:46 GMT+0000 (UTC)"
127.0.0.1:6379> 

Wrapping up

Now you can profit from using Redis from your functions.

You can read the docs about the Redis npm module here or join the OpenFaaS Slack community to boost your serverless journey.

Did you find that useful? Please share on Twitter or Tweet to @alexellisuk

"use strict"
const redis = require("redis");
module.exports = (body, callback) => {
const client = redis.createClient(6379, process.env.redis);
client.set('mytime', new Date().toString(), () => {
client.quit();
callback(null, { status: 'set done' });
});
}
{
"name": "function",
"version": "1.0.0",
"description": "",
"main": "handler.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"redis": "^2.8.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.