Defining a new function

To create a function written in the Go language, we use the faas-cli new --lang=go hello command:

$ faas-cli new --lang=go hello

2017/11/15 18:42:28 No templates found in current directory.
2017/11/15 18:42:28 HTTP GET https://github.com/openfaas/faas-cli/archive/master.zip
2017/11/15 18:42:38 Writing 287Kb to master.zip

2017/11/15 18:42:38 Attempting to expand templates from master.zip
2017/11/15 18:42:38 Fetched 10 template(s) : [csharp go-armhf go node-arm64 node-armhf node python-armhf python python3 ruby] from https://github.com/openfaas/faas-cli
2017/11/15 18:42:38 Cleaning up zip file...
Folder: hello created.
___ _____ ____
/ _ _ __ ___ _ __ | ___|_ _ __ _/ ___|
| | | | '_ / _ '_ | |_ / _` |/ _` \___
| |_| | |_) | __/ | | | _| (_| | (_| |___) |
\___/| .__/ \___|_| |_|_| \__,_|\__,_|____/
|_|


Function created in folder: hello
Stack file written: hello.yml

After the function is created, we can check the structure of the function directory by running the tree -L 2 . command. It shows the directory at two levels of depth, as follows:

$ tree -L 2 .
.
├── hello
│ └── handler.go
├── hello.yml
└── template
├── csharp
├── go
├── go-armhf
├── node
├── node-arm64
├── node-armhf
├── python
├── python3
├── python-armhf
└── ruby

First, we will look at the function definition in the hello.yml file. From hello.yml , there are two top-levels,  provider and functions.

The provider block tells us that its provider's name is faas, the default OpenFaaS implementation in Docker Swarm. Also, it tells us that the gateway endpoint is at http://localhost:8080, where an instance of the API gateway is running. In a production environment, this URL could be changed to point to the real IP address.  

The functions block lists all defined functions. In the example, there is only the hello function there. This block tells us this function is written in the Go programming language (lang: go). The function's handler specified by handler: ./hello points to the directory containing the source file of the real working function (./hello/handler.go). In the example, the output image's name is specified by image: hello. Before building the function, we would change the image name to <your Docker ID>/hello:v1 as it is a best practice to not use the :latest tag:

############
# hello.yml
############
provider:
name: faas
gateway: http://localhost:8080

functions:
hello:
lang: go
handler: ./hello
image: hello # change this line to <your Docker ID>/hello:v1
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset