|
| 1 | +# Create new functions |
| 2 | + |
| 3 | +The OpenFaaS CLI has a template engine built-in which can create new functions in a given programming language. The way this works is by reading a list of templates from the `./template` location in your current working folder. |
| 4 | + |
| 5 | +Before creating a new function make sure you pull in the official OpenFaaS language templates from GitHub via the [templates repository](https://github.com/openfaas/templates). |
| 6 | + |
| 7 | +```bash |
| 8 | +$ faas-cli template pull |
| 9 | +``` |
| 10 | + |
| 11 | +This page shows how to generate functions in three of the most common languages and explains how to manage their dependencies. |
| 12 | + |
| 13 | +## 1.0 Go |
| 14 | + |
| 15 | +To create a new function named `go-fn` in Go type in the following: |
| 16 | + |
| 17 | +```bash |
| 18 | +$ faas-cli new go-fn --lang go |
| 19 | +``` |
| 20 | + |
| 21 | +You will now see two files generate: |
| 22 | + |
| 23 | +``` |
| 24 | +go-fn.yml |
| 25 | +./go-fn/ |
| 26 | +./go-fn/handler.go |
| 27 | +``` |
| 28 | + |
| 29 | +You can now edit `handler.go` and use the `faas-cli` to `build` and `deploy` your function. |
| 30 | + |
| 31 | +### 1.1 Go: dependencies |
| 32 | + |
| 33 | +Dependencies should be managed with a Go vendoring tool such as dep. |
| 34 | + |
| 35 | +* Get [dep](https://github.com/golang/dep) |
| 36 | + |
| 37 | +``` |
| 38 | +$ go get -u github.com/golang/dep/cmd/dep |
| 39 | +``` |
| 40 | + |
| 41 | +* Initialise the dependencies |
| 42 | + |
| 43 | +``` |
| 44 | +$ $GOPATH/bin/dep init |
| 45 | +``` |
| 46 | + |
| 47 | +* Now vendor a library |
| 48 | + |
| 49 | +Make sure you're in the `go-fn` folder, now use `dep ensure -add` and the name of the library you want. In this example we are vendoring the `github.com/cnf/structhash` package for use in our function. |
| 50 | + |
| 51 | +``` |
| 52 | +$ dep ensure -add github.com/cnf/structhash |
| 53 | +``` |
| 54 | + |
| 55 | +* Reference the package from function |
| 56 | + |
| 57 | +You can now edit your function and add an import statement in `handler.go` to `github.com/cnf/structhash`. |
| 58 | + |
| 59 | +## 2.0 Python |
| 60 | + |
| 61 | +To create a Python function named `pycon` type in: |
| 62 | + |
| 63 | +``` |
| 64 | +$ faas-cli new pycon --lang python3 |
| 65 | +``` |
| 66 | + |
| 67 | +You'll see: |
| 68 | + |
| 69 | +``` |
| 70 | +pycon.yml |
| 71 | +pycon/handler.py |
| 72 | +pycon/requirements.txt |
| 73 | +``` |
| 74 | + |
| 75 | +### 2.1 Python: dependencies |
| 76 | + |
| 77 | +You should edit `pycon/requirements.txt` and add any pip modules you want with each one on a new line, for instance `requests`. |
| 78 | + |
| 79 | +The primary Python template uses Alpine Linux as a runtime environment due to its minimal size, but if you need a Debian environment so that you can compile `numpy` or other modules then read on to the next section. |
| 80 | + |
| 81 | +### 2.2 Python: advanced dependencies |
| 82 | + |
| 83 | +If you need to use pip modules that require compilation then you should try the python3-debian template then add your pip modules to the `requirements.txt` file. |
| 84 | + |
| 85 | +``` |
| 86 | +$ faas-cli template pull https://github.com/openfaas-incubator/python3-debian |
| 87 | +$ faas-cli new numpy-function --lang python3-debian |
| 88 | +$ echo "numpy" > ./numpy-function/requirements.txt |
| 89 | +$ faas-build -f ./numpy-function.yml |
| 90 | +... |
| 91 | +
|
| 92 | +Step 11/17 : RUN pip install -r requirements.txt |
| 93 | + ---> Running in d0ff430a607e |
| 94 | +Collecting numpy (from -r requirements.txt (line 1)) |
| 95 | + Downloading https://files.pythonhosted.org/packages/6e/dc/92c0f670e7b986829fc92c4c0208edb9d72908149da38ecda50d816ea057/numpy-1.14.2-cp36-cp36m-manylinux1_x86_64.whl (12.2MB) |
| 96 | +Installing collected packages: numpy |
| 97 | +Successfully installed numpy-1.14.2 |
| 98 | +
|
| 99 | +... |
| 100 | +``` |
| 101 | + |
| 102 | +## 3.0 Node.js |
| 103 | + |
| 104 | +Generate a function named `js-fn`: |
| 105 | + |
| 106 | +```bash |
| 107 | +$ faas-cli new js-fn --lang node |
| 108 | +``` |
| 109 | + |
| 110 | +You'll see: |
| 111 | + |
| 112 | +```bash |
| 113 | +./js-fn.yml |
| 114 | +./js-fn/ |
| 115 | +./js-fn/handler.js |
| 116 | +./js-fn/package.json |
| 117 | +``` |
| 118 | + |
| 119 | +### 3.1 Node.js dependencies |
| 120 | + |
| 121 | +Node.js dependencies are managed with `npm` and the `package.json` file which was generated for you. |
| 122 | + |
| 123 | +To add the `cheerio` module type in: |
| 124 | + |
| 125 | +``` |
| 126 | +cd js-fn |
| 127 | +npm i --save cheerio |
| 128 | +``` |
| 129 | + |
| 130 | +You can now add a `require('cheerio')` statement into your function and make use of this library. |
| 131 | + |
| 132 | +## 4.0 Customise a template |
| 133 | + |
| 134 | +It is recommended that you use the official templates as they are provided and if there is a short-coming that you raise a GitHub issue so we can improve the templates for everyone. |
| 135 | + |
| 136 | +All templates are driven by a Dockerfile and can be customised by editing the files found in the ./template folder. |
| 137 | + |
| 138 | +### 4.1 Update the Dockerfile |
| 139 | + |
| 140 | +There are several reasons why you may want to update your Dockerfile, just edit `./template/<language_name>/Dockerfile`. |
| 141 | + |
| 142 | +* New base image - some companies prefer to use their own base images for Docker images for compliance, support or licensing reasons |
| 143 | + |
| 144 | +* Add native package - sometimes you may want to add a native package from the Alpine Linux repository or the Debian package repository - just add a step into the Dockerfile |
| 145 | + |
| 146 | +* Try a new version of a base-image - it may be that the project is showing support for Node.js LTS, but you want the cutting-edge version, you can do that too |
| 147 | + |
| 148 | +### 4.2 Update a template's configuration |
| 149 | + |
| 150 | +The name of a template is read from a "template.yml" file kept within the template folder: `./template/<language_name>/template.yml` |
| 151 | + |
| 152 | +For `csharp` we have the following: |
| 153 | + |
| 154 | +``` |
| 155 | +language: csharp |
| 156 | +fprocess: dotnet ./root.dll |
| 157 | +``` |
| 158 | + |
| 159 | +* `language` is the display name used for `faas-cli new --list`. |
| 160 | +* `fprocess` provides the process to run for each invocation - i.e. your function |
| 161 | + |
| 162 | +### 4.3 Use your own templates |
| 163 | + |
| 164 | +You can use your own Git repository for a custom or forked set of templates. This can be public or private. |
| 165 | + |
| 166 | +See `faas-cli template pull` for more information. |
| 167 | + |
| 168 | +### 5.0 ARM / Raspberry Pi |
| 169 | + |
| 170 | +Templates for ARM and Raspberry Pi are provided on a best-effort basis. If you can help with maintenance please let the project contributors know. |
| 171 | + |
| 172 | +* ARMv7 / Raspberry Pi |
| 173 | + |
| 174 | +Type in `faas-cli new --list` and look for any languages ending in `-armhf`. You can use any of these for your functions. |
| 175 | + |
| 176 | +* ARM64 / Packet.net / Scaleway ARM 64-bit |
| 177 | + |
| 178 | +For these platforms do the same as above and look for the `-arm64` suffix. |
| 179 | + |
| 180 | +> It is easy to make your own templates so if you need to use this platform please convert one of the "regular" templates for your platform. |
| 181 | +
|
0 commit comments