paint-brush
Build a Serverless Memes Function with OpenFaaSby@mlabouardy
991 reads
991 reads

Build a Serverless Memes Function with OpenFaaS

by Mohamed LabouardyOctober 24th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this quick post, I will show you how to build a <strong>Serverless</strong> function in <strong>Go</strong> to get the latest <strong>9Gag Memes</strong> using <a href="https://github.com/openfaas/faas" target="_blank">OpenFaaS</a>.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Build a Serverless Memes Function with OpenFaaS
Mohamed Labouardy HackerNoon profile picture

In this quick post, I will show you how to build a Serverless function in Go to get the latest 9Gag Memes using OpenFaaS.

This tutorial assume that you have:

  • faas-cli installed — The easiest way to install the faas-cli is through cURL:

curl -sSL https://cli.openfaas.com | sudo sh

  • Swarm or Kubernetes environment configured — See Docs.

1 — Create a function

Create a “handler.go” file with the following content:

The code is self-explanatory, it uses 9Gag Web Crawler to parse the website and fetch memes by their tag.

2 — Docker Image

I wrote a simple Dockerfile using the Multi-stage builds technique to reduce the image size down:

3 — Configuration file

Note: If pushing to a remote registry change the name from _mlabouardy_to your own Hub account.

4 — Build

Issue the following command:

faas-cli build -f ./stack.yml

5 — Deploy

faas-cli push -f ./stack.yml

faas-cli deploy -f ./stack.yml

6 — Tests

Once deployed, you can invoke the function via:

cURL:

curl http://localhost:8080/function/memes-9gag -d “GoT”

FaaS CLI:

echo “GoT” | faas-cli invoke memes-9gag

UI:

JSON Example:

Note: all code used in this demo, is available on my Github

Taking it further ?

You can build an Android application which consumes the function JSON response as below: