Skip to content

Instantly share code, notes, and snippets.

@alexellis
Last active May 19, 2022 02:17
Show Gist options
  • Save alexellis/e05a7b573ae22b209f0214d5766ff07e to your computer and use it in GitHub Desktop.
Save alexellis/e05a7b573ae22b209f0214d5766ff07e to your computer and use it in GitHub Desktop.
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"
}
}
@ferdinandosimonetti
Copy link

Hi Alex,

Just checking, I think this tutorial is not intended for Arm-based architectures as the Redis helm chart cannot deploy Redis on arm machines if I am not mistaken. Also, I see stable/redis repository is deprecated and bitnami/redis is encouraged.

Thanks, Mohammad

Waaay late in this thread, but for ARM64 (ARMv8) you can use these custom values to deploy the basic Redis Helm chart

image:
  registry: docker.io
  repository: arm64v8/redis
  tag: latest

Of course it's Redis-only (no sentinel or other goodies), 1 master + 3 worker nodes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment