Skip to content

Commit 195eb50

Browse files
committed
Add two tutorials
Adds ClI / node tutorial Adds First Python Function tutorial Signed-off-by: Alex Ellis <[email protected]>
1 parent 28f2865 commit 195eb50

File tree

3 files changed

+539
-0
lines changed

3 files changed

+539
-0
lines changed

docs/tutorials/CLI-with-node.md

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
# Tutorial for the OpenFaaS CLI with Node.js
2+
3+
We'll explore how to create, build, deploy and invoke a brand new function with one of the supported language templates. We'll use the CLI for every part of the workflow.
4+
5+
**What is OpenFaas?**
6+
7+
![](https://blog.alexellis.io/content/images/2017/08/small.png)
8+
9+
OpenFaaS is a framework for packaging code, binaries or containers as Serverless functions on any platform - Windows or Linux. [Visit website](https://www.openfaas.com/)
10+
11+
In the next few minutes we'll:
12+
13+
* Create a function from a code template
14+
* Build the function as a Docker image
15+
* Push the image to a Docker registry
16+
* Deploy the function
17+
* Invoke the function
18+
19+
..and more
20+
21+
### Pre-requisites
22+
23+
Before starting you should setup OpenFaaS on your laptop or cluster using a deployment guide:
24+
25+
[Deployment guide](http://docs.openfaas.com/deployment/)
26+
27+
If you're not sure which to pick - you can deploy and setup Docker Swarm in around 60 seconds.
28+
29+
### Get the CLI
30+
31+
For the latest version of the CLI type in the following:
32+
33+
```
34+
$ curl -sL https://cli.openfaas.com | sudo sh
35+
```
36+
37+
> This is also available via `brew install faas-cli` on MacOS.
38+
39+
### Find the help page
40+
41+
All the commands have a `--help` flag available which provides documentation on usage:
42+
43+
```
44+
$ faas-cli --help
45+
46+
Manage your OpenFaaS functions from the command line
47+
48+
Usage:
49+
faas-cli [flags]
50+
faas-cli [command]
51+
52+
Available Commands:
53+
build Builds OpenFaaS function containers
54+
deploy Deploy OpenFaaS functions
55+
help Help about any command
56+
push Push OpenFaaS functions to remote registry (Docker Hub)
57+
remove Remove deployed OpenFaaS functions
58+
version Display the clients version information
59+
60+
Flags:
61+
-h, --help help for faas-cli
62+
-f, --yaml string Path to YAML file describing function(s)
63+
64+
Use "faas-cli [command] --help" for more information about a command.
65+
```
66+
67+
### Create a new Node.js function
68+
69+
We'll create a brand new Node.js template and a YAML file at the same time which is used by the CLI to save you on typing.
70+
71+
```
72+
$ faas-cli new callme --lang node
73+
Folder: callme created.
74+
Function created in folder: callme
75+
Stack file written: callme.yml
76+
```
77+
78+
You'll see the following was created:
79+
80+
```
81+
$ find . | grep callme
82+
./callme
83+
./callme/handler.js
84+
./callme/package.json
85+
./callme.yml
86+
```
87+
88+
Here's the YAML file which was generated:
89+
90+
```
91+
$ cat callme.yml
92+
provider:
93+
name: faas
94+
gateway: http://localhost:8080
95+
96+
functions:
97+
callme:
98+
lang: node
99+
handler: ./callme
100+
image: callme
101+
```
102+
103+
The contents of `callme.yml` can now be used with the CLI to save on typing and build, push, deploy and invoke your function.
104+
105+
> If your cluster is remote or not running on port 8080 - then edit this in the YAML file before continuing.
106+
107+
A handler.js file was generated for your function which looks like this:
108+
109+
```
110+
"use strict"
111+
112+
module.exports = (context, callback) => {
113+
callback(undefined, {status: "done"});
114+
}
115+
```
116+
117+
It will just send back a status of "done" when called.
118+
119+
> Tip: You can also create your `handler.js` file manually
120+
121+
*What about `npm` modules etc?*
122+
123+
You can edit the `package.json` file and your dependencies will be installed during the "build" step. The same works for the other language templates with a `requirements.txt` file for Python etc.
124+
125+
### Build the function
126+
127+
The local Docker client is used to build your function into a Docker image.
128+
129+
```
130+
$ faas-cli build -f callme.yml
131+
Building: callme.
132+
Clearing temporary build folder: ./build/callme/
133+
Preparing ./callme/ ./build/callme/function
134+
Building: callme with node template. Please wait..
135+
docker build -t callme .
136+
Sending build context to Docker daemon 8.704kB
137+
Step 1/19 : FROM node:6.11.2-alpine
138+
---> 16566b7ed19e
139+
...
140+
141+
Step 19/19 : CMD fwatchdog
142+
---> Running in 53d04c1631aa
143+
---> f5e1266b0d32
144+
Removing intermediate container 53d04c1631aa
145+
Successfully built f5e1266b0d32
146+
Successfully tagged callme:latest
147+
Image: callme built.
148+
```
149+
150+
### Push your function
151+
152+
> If you are using a single-node Docker cluster on your laptop then you can skip this step.
153+
154+
Now edit the `callme.yml` YAML file and set the "image" line to your username on the Docker Hub such as: `alexellis/callme`. Then build the function again.
155+
156+
```
157+
$ faas-cli push -f callme.yml
158+
Pushing: callme to remote repository.
159+
The push refers to a repository [docker.io/alexellis/callme]
160+
...
161+
```
162+
163+
Once the image is pushed up to the Docker Hub or another remote Docker registry we can deploy and run the function.
164+
165+
### Deploy the function
166+
167+
```
168+
$ faas-cli deploy -f callme.yml
169+
Deploying: callme.
170+
No existing service to remove
171+
Deployed.
172+
200 OK
173+
URL: http://localhost:8080/function/callme
174+
```
175+
176+
If an existing / old function was already deployed then it will be removed first.
177+
178+
> If you get an error at this point then please make sure you followed the pre-requisites.
179+
180+
### Invoke the function
181+
182+
We can now invoke the function:
183+
184+
```
185+
$ faas-cli invoke -f callme.yml callme
186+
Reading from STDIN - hit (Control + D) to stop.
187+
This is my message
188+
189+
{"status":"done"}
190+
```
191+
192+
You can also pipe a command into the function.
193+
194+
```
195+
$ date | faas-cli invoke -f callme.yml callme
196+
{"status":"done"}
197+
```
198+
199+
**Task:** Can you edit the function so that it returns the input along with "status": "done"? Just edit the code, run "build", "push" and "deploy" - then you're good to invoke it again.
200+
201+
### List the functions
202+
203+
You can list your functions and find out how many replicas they have and also the invocation count like this:
204+
205+
```
206+
$ faas-cli list
207+
Function Invocations Replicas
208+
inception 0 1
209+
callme 2 1
210+
func_hubstats 0 1
211+
tensorflow 0 1
212+
func_echoit 0 1
213+
func_nodeinfo 0 1
214+
```
215+
216+
> Tip: If you pass `--verbose` then you'll also see the image name
217+
218+
### Remove the function
219+
220+
You can remove functions by using the CLI. If you specify a YAML file the CLI will remove all the deployed functions listed in the file.
221+
222+
```
223+
$ faas-cli rm -f callme.yml
224+
Deleting: callme.
225+
Removing old service.
226+
```
227+
228+
Type in `faas-cli rm --help` for more information.
229+
230+
## Wrapping up
231+
232+
> Did you know we also have a UI and Prometheus metrics built-into the stack? Head over to the [GitHub repo](https://github.com/alexellis/faas) to read more and to **Star** the repository.
233+
234+
That concludes the coffee break - we just built and deployed our first Serverless Node.js OpenFaaS function - "Look Ma! No UI!" You can find help for any of the commands by passing in the `--help` parameter.

0 commit comments

Comments
 (0)