Serverless “Hello World” with Fn Framework


04 Apr 2018  Sergio Martin Rubio  3 mins read.

The hype about serverless applications has been growing lately, and AWS is the leader at the moment, however there are other alternatives such as Fn Project by Oracle or OpenWhisk by IBM.

I found out about Fn Project during Democratizing Serverless Talk at London QCon 2018 and could not wait to try this platform since you can run your own serverless architecture on your own laptop.

Features

  • Open source.
  • Docker-based platform.
  • Allows you to run it locally.
  • Helm chart available at https://github.com/fnproject/fn-helm
  • Metrics/Monitoring: Prometheus, syslog…
  • Supports many languages: Java, Golang, Ruby, Python, PHP, and Node.js
  • Easy to use.
Fn Project Architecture
Fn Project Architecture

Requirements

  1. Docker 17.06 or later.
  2. Docker Hub account.
  3. Log into your Docker Hub account (docker login).
  4. Install Fn CLI tool.
    • MacOS:
brew install fn
  • MacOS and Linux
curl -LSs https://raw.githubusercontent.com/fnproject/cli/master/install | sh
  1. MacOS, Windows and Linux: download latest bin file for your OS.

Getting Started

  1. Start up Fn server:

    Make sure Docker is up and running.

     fn start
    

    Now a Docker container (fnproject/fnserver) is running on your machine.

  2. Use Fn CLI tool to create a project with the language of your choice:

     fn init --runtime go --timeout 10 --idle-timeout 10 hello-serverless
    

    Golang was my choice for this hello world serverless application since it is a lightweight and easy to use programming language.

    –timeout -> for how long the code will be running

    –idle-timeout 10 -> time container will wait for another call

    When running fn init a Dockerfile is generated by Fn:

     FROM fnproject/go:dev as build-stage
     WORKDIR /function
     RUN go get -u github.com/golang/dep/cmd/dep
     ADD . /go/src/func/
     RUN cd /go/src/func/ && dep ensure
     RUN cd /go/src/func/ && go build -o func
     FROM fnproject/go
     WORKDIR /function
     COPY --from=build-stage /go/src/func/func /function/
     ENTRYPOINT ["./func"]
    

    After running the init command all the boilerplate has been created:

    func.go -> Go application func.yaml -> Fn configuration file test.json -> test file for the application Gopkg.toml -> file for Goland dependency management.

  3. Run application:

     fn run
    

    First time you run this command will take longer.

     {"message":"Hello World"}
    
  4. Deploy the function to your local Fn server to add the application to the Fn list:

     fn deploy --app myapp --local
    

    The local flag is to avoid pushing the image to your Docker Hub. In case you want to use the Docker repository, first you have to set the Docker Hub username: export FN_REGISTRY=DOCKER_HUB_USERNAME.

    Now you can open your browser and go to http://localhost:8080/r/myapp/hello-serverless.

Useful commands

fn apps list # shows applications registered on your Fn server
fn routes list myapp # shows application endpoints
fn apps delete myapp # deletes application

Image by Bruno /Germany from Pixabay