Serverless “Hello World” with Fn Framework
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.
- 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.
- Docker 17.06 or later.
- Docker Hub account.
- Log into your Docker Hub account (
- Install Fn CLI tool.
brew install fn
- MacOS and Linux
curl -LSs https://raw.githubusercontent.com/fnproject/cli/master/install | sh
- MacOS, Windows and Linux: download latest bin file for your OS.
Start up Fn server:
Make sure Docker is up and running.
Now a Docker container (
fnproject/fnserver) is running on your machine.
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
fn inita 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.
First time you run this command will take longer.
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:
Now you can open your browser and go to http://localhost:8080/r/myapp/hello-serverless.
fn apps list # shows applications registered on your Fn server fn routes list myapp # shows application endpoints fn apps delete myapp # deletes application