Recently I came across an article on faasd, and I thought I'd give it a try and see how easy it is to use. Server templating is an easy way to create a server with DNS, and following the whole disposability principals, we'll whip something up and see how it goes.
What is faasd?
Functions as a service are a way to easily package up simple functions as an API, with a minimal amount of overhead. Firing up a whole rails project, configuring a server or spinning up a heroku instance, kubernetes cluster, etc may be overkill for a simple proof of concept, and that proof of concept may be enough to last you a long time. AWS Lambda is the more common form of serverless functions, but that means you have to sign up for the whole AWS console, which I can barely understand. faasd is a very stripped down platform that you can run on your VPS or even a RaspberryPI, which will let you throw up something simple without having to, you know, deploy kubernetes.
Set up the server
We first need to setup a clean server. I'm going to use debian since I always do, but the installation instructions say to use Ubuntu.
terraform apply to create it.
Ssh into your server as root and run the following commands:
The last line will print out the auth password which you will need for the ui and to use the cli.
We can run this on the remote server using:
Just visit your domain to see the UI. The username is
admin and the
password was printed out by the previous command.
My server is
faas.willschenk.com so lets first install the cli tool,
and then point it to it.
We can then deploy our first function by
And run it with:
Hostname: localhost Arch: x64 CPUs: 2 Total mem: 1998MB Platform: linux Uptime: 269013
Nice and easy. A list of functions available from the store can be found with
Templates and registries
The basic idea is that we pull down one of the templates, code it up,
and then push it up to our server. The way that it works is that we
build the container, push it to a registry, and then tell our
server to pull that container from a registry and deploy it.
To get a list of available templates:
Writing a node12 function
We'll ceate a new one (I'm taking this code from the serverless for
everyone else book) and we are doing to push it to docker hub. I'm
already logged in as
wschenk so that's what I'm setting the
Lets install the
Then lets change the
Then we deploy the function
And we can run it with:
There are currently 7 astronauts in space.
Command as a cloud function
Lets look at how to package up a command as a function.
Then we edit the
fortune/Dockerfile. Add we are adding here is
apk add fortune and setting the
Then you can build and deploy with
Which now is available as:
When I get real bored, I like to drive downtown and get a great parking spot, then sit in my car and count how many people ask me if I'm leaving. -- Steven Wright
The whole process took me under a minute for the first time that I did it, so that's super cool.
First we setup a ssh tunnel so we can connect to the local prometheus instance.
Once this tunnel is up, we can connect to Prometheus locally.
Now on your faas server, edit
add the grafana service.
We need to create the
grafana directory and restart everything:
Finally on our local machine we need to open up an ssh tunnel to grafana so we can access. In my case:
Add your prometheus datasource, pointing to
you are good to start adding panels.
This makes it really easy to quickly spin something up. Getting logging and monitoring built in "for free" is a huge leg up on spinning up your own solutions. I've haven't jumped too much into the kubernetes bandwagon, but this makes it so simple to expose a tiny bit of functionality in a way that seems so easy to maintain.
If you usecase is about gluing a couple of things together this is really pretty fascinating!