@@ -4,7 +4,7 @@ your laptop, server, or cloud. In this introductory tutorial we'll walk through
4
4
developing a function using the JavaScript programming language and Node.js
5
5
(without installing any Node.js tools!) and deploying that function to a local
6
6
Fn server. We'll also learn about the core Fn concepts like applications and
7
- triggers .
7
+ invoke endpoints .
8
8
9
9
### Before you Begin
10
10
* Set aside about 15 minutes to complete this tutorial.
@@ -29,7 +29,7 @@ In the terminal type the following.
29
29
30
30
![ user input] ( images/userinput.png )
31
31
> ```
32
- > fn init --runtime node --trigger http nodefn
32
+ > fn init --runtime node nodefn
33
33
>```
34
34
35
35
The output will be
@@ -43,9 +43,8 @@ func.yaml created.
43
43
The ` fn init ` command creates a simple function with a bit of boilerplate to get
44
44
you started. The ` --runtime ` option is used to indicate that the function we're
45
45
going to develop will be written in Node. A number of other runtimes are also
46
- supported. The ` --trigger ` option creates an HTTP trigger for the function
47
- allowing you to invoke the function from a URL. Fn creates the simple function
48
- along with several supporting files in the ` /nodefn ` directory.
46
+ supported. Fn creates the simple function along with several supporting files
47
+ in the ` /nodefn ` directory.
49
48
50
49
### Review your Function File
51
50
@@ -107,10 +106,6 @@ name: nodefn
107
106
version: 0.0.1
108
107
runtime: node
109
108
entrypoint: node func.js
110
- triggers:
111
- - name: nodefn
112
- type: http
113
- source: /nodefn
114
109
```
115
110
116
111
The generated `func.yaml` file contains metadata about your function and
@@ -123,7 +118,6 @@ declares a number of properties including:
123
118
in `--runtime`.
124
119
* entrypoint--the name of the Docker execution command to invoke when your function is called,
125
120
in this case `node func.js`.
126
- * triggers--identifies the automatically generated trigger name and source. For example, this function would be requested from the URL `http://localhost:8080/t/appname/nodefn`. Where `appname` is the name of the app chosen for your function when it is deployed.
127
121
128
122
There are other user specifiable properties but these will suffice for
129
123
this example. Note that the name of your function is taken from the containing
@@ -202,57 +196,34 @@ FN_REGISTRY: fndemouser
202
196
Current Context: default
203
197
Sending build context to Docker daemon 5.12kB
204
198
Step 1/9 : FROM fnproject/node:dev as build-stage
205
- dev: Pulling from fnproject/node
206
- bdf0201b3a05: Already exists
207
- ed514f6061af: Already exists
208
- bc8940a76c7f: Already exists
209
- Digest: sha256:c77966d42f46328662978a68ecd5b3b3b26cf9e4c29a91c6064f07f59f97621a
210
- Status: Downloaded newer image for fnproject/node:dev
211
199
---> b557a05fec78
212
200
Step 2/9 : WORKDIR /function
213
- ---> Running in 62a348f4d39b
214
- Removing intermediate container 62a348f4d39b
201
+ ---> Using cache
215
202
---> ea3ae80dbec2
216
203
Step 3/9 : ADD package.json /function/
204
+ ---> Using cache
217
205
---> e7d319e022d2
218
206
Step 4/9 : RUN npm install
219
- ---> Running in c963c8b1f699
220
- npm notice created a lockfile as package-lock.json. You should commit this file.
221
- npm WARN [email protected] No repository field.
222
-
223
- added 1 package from 2 contributors and audited 1 package in 0.935s
224
- found 0 vulnerabilities
225
-
226
- Removing intermediate container c963c8b1f699
207
+ ---> Using cache
227
208
---> bf5b6313c055
228
209
Step 5/9 : FROM fnproject/node
229
- latest: Pulling from fnproject/node
230
- bdf0201b3a05: Already exists
231
- ed514f6061af: Already exists
232
- bc8940a76c7f: Already exists
233
- f93406d580f9: Already exists
234
- Digest: sha256:2ee8e9e4b1de3a29cde6fa957e3d7aaf6d2486f46926863e42af92c9d4a02404
235
- Status: Downloaded newer image for fnproject/node:latest
236
210
---> c8da69259495
237
211
Step 6/9 : WORKDIR /function
238
- ---> Running in 128681aed76f
239
- Removing intermediate container 128681aed76f
212
+ ---> Using cache
240
213
---> 1fe19c6ca66c
241
214
Step 7/9 : ADD . /function/
242
- ---> 3ca3886160d0
215
+ ---> 229045a70217
243
216
Step 8/9 : COPY --from=build-stage /function/node_modules/ /function/node_modules/
244
- ---> 48940744e184
217
+ ---> 59b17163fe37
245
218
Step 9/9 : ENTRYPOINT [" node" , " func.js" ]
246
- ---> Running in 50ac03eeaec7
247
- Removing intermediate container 50ac03eeaec7
248
- ---> 82e19d55d014
249
- Successfully built 82e19d55d014
219
+ ---> Running in b0d793dd39bb
220
+ Removing intermediate container b0d793dd39bb
221
+ ---> 7160364dec30
222
+ Successfully built 7160364dec30
250
223
Successfully tagged fndemouser/nodefn:0.0.2
251
224
252
225
Updating function nodefn using image fndemouser/nodefn:0.0.2...
253
226
Successfully created function: nodefn with fndemouser/nodefn:0.0.2
254
- Successfully created trigger: nodefn
255
- Trigger Endpoint: http://localhost:8080/t/nodeapp/nodefn
256
227
` ` `
257
228
258
229
All the steps to load the current language Docker image are displayed.
@@ -270,64 +241,26 @@ let's us know that the function is packaged in the image
270
241
271
242
Note that the containing folder name ' nodefn' was used as the name of the
272
243
generated Docker container and used as the name of the function that
273
- container was bound to. By convention it is also used for the trigger name.
244
+ container was bound to.
274
245
275
246
Normally you deploy an application without the `--verbose` option. If you rerun the command a new image and version is created and loaded.
276
247
277
-
278
- ## Invoke your Deployed Function
279
-
280
- There are two ways to call your deployed function.
281
-
282
- ### Invoke with the CLI
283
-
284
- The first is using the `fn` CLI which makes invoking your function relatively
285
- easy. Type the following:
286
-
287
- 
288
- >```sh
289
- > fn invoke nodeapp nodefn
290
- >```
291
-
292
- which results in:
293
-
294
- ```js
295
- {"message":"Hello World"}
296
- ```
297
-
298
- When you invoked "nodeapp nodefn" the fn server looked up the
299
- "nodeapp" application and then looked for the Docker container image
300
- bound to the "nodefn" function and executed the code.
301
-
302
- You can also pass data to the run command. Note that you set the content type for the data passed. For example:
303
-
304
- 
305
- >```sh
306
- > echo -n ' {" name" :" Bob" }' | fn invoke nodeapp nodefn --content-type application/json
307
- >```
308
-
309
- ```js
310
- {"message":"Hello Bob"}
311
- ```
312
-
313
- The JSON data was parsed and since `name` was set to "Bob", that value is passed
314
- in the output.
315
-
316
248
### Understand fn deploy
249
+
317
250
If you have used Docker before the output of `fn --verbose deploy` should look
318
251
familiar--it looks like the output you see when running `docker build`
319
252
with a Dockerfile. Of course this is exactly what' s happening! When
320
253
you deploy a function like this Fn is dynamically generating a Dockerfile
321
254
for your function, building a container, and then loading it for execution.
322
255
323
- > __NOTE__: Fn is actually using two images. The first contains the language compiler
324
- and all the necessary build tools. The second image packages all dependencies
325
- and any necessary language runtime components. Using this strategy, the final
326
- function image size can be kept as small as possible. Smaller Docker images are
327
- naturally faster to push and pull from a repository which improves overall
328
- performance. For more details on this technique see [Multi-Stage Docker Builds
329
- for Creating Tiny Go
330
- Images](https://medium.com/travis-on-docker/multi-stage-docker-builds-for-creating-tiny-go-images-e0e1867efe5a).
256
+ > __NOTE__: Fn is actually using two images. The first contains the language
257
+ > compiler and all the necessary build tools. The second image packages all
258
+ > dependencies and any necessary language runtime components. Using this
259
+ > strategy, the final function image size can be kept as small as possible.
260
+ > Smaller Docker images are naturally faster to push and pull from a repository
261
+ > which improves overall performance. For more details on this technique see
262
+ > [Multi-Stage Docker Builds for Creating Tiny Go
263
+ > Images](https://medium.com/travis-on-docker/multi-stage-docker-builds-for-creating-tiny-go-images-e0e1867efe5a).
331
264
332
265
When using ` fn deploy --local` , fn server builds and packages your function
333
266
into a container image which resides on your local machine.
@@ -365,33 +298,103 @@ NAME ID
365
298
nodeapp 01DHHSP6X3NG8G00GZJ0000001
366
299
```
367
300
368
- We can also see the functions that are defined by an application. Since
369
- functions are exposed via triggers, the `fn list triggers <appname>` command
370
- is used. To list the functions included in "nodeapp" we can type:
301
+ The `fn list functions <app-name>` command lists all the functions associated with and app.
371
302
372
303

373
304
>```sh
374
- > fn list triggers nodeapp
305
+ > fn list functions nodeapp
375
306
>```
376
307
308
+ The returns all the functions associated with the `nodeapp`.
309
+
310
+ ```
311
+ NAME IMAGE ID
312
+ nodefn fndemouser/nodefn:0.0.2 01DJ900WFKNG8G00GZJ0000002
377
313
```
378
- FUNCTION NAME ID TYPE SOURCE ENDPOINT
379
- nodefn nodefn 01DHHSPYT9NG8G00GZJ0000003 http /nodefn http://localhost:8080/t/nodeapp/nodefn
314
+
315
+ ## Invoke your Deployed Function
316
+
317
+ There are two ways to call your deployed function.
318
+
319
+ ### Invoke with the CLI
320
+
321
+ The first is using the `fn` CLI which makes invoking your function relatively
322
+ easy. Type the following:
323
+
324
+ 
325
+ >```sh
326
+ > fn invoke nodeapp nodefn
327
+ >```
328
+
329
+ which results in:
330
+
331
+ ```js
332
+ {"message":"Hello World"}
333
+ ```
334
+
335
+ When you invoked "nodeapp nodefn" the fn server looked up the
336
+ "nodeapp" application and then looked for the Docker container image
337
+ bound to the "nodefn" function and executed the code.
338
+
339
+ You can also pass data to the run command. Note that you set the content type for the data passed. For example:
340
+
341
+ 
342
+ >```sh
343
+ > echo -n ' {" name" :" Bob" }' | fn invoke nodeapp nodefn --content-type application/json
344
+ >```
345
+
346
+ ```js
347
+ {"message":"Hello Bob"}
348
+ ```
349
+
350
+ The JSON data was parsed and since `name` was set to "Bob", that value is passed
351
+ in the output.
352
+
353
+
354
+ ### Getting a Function' s Invoke Endpoint
355
+
356
+ In addition to using the Fn ` invoke` command, we can call a function by using a
357
+ URL. To do this, we must get the function' s invoke endpoint. Use the command
358
+ `fn inspect function <appname> <function-name>`. To list the `nodefn` function' s
359
+ invoke endpoint we can type:
360
+
361
+ ! [user input](images/userinput.png)
362
+ > ` ` ` sh
363
+ > fn inspect function nodeapp nodefn
364
+ > ` ` `
365
+
366
+ ` ` ` js
367
+ {
368
+ " annotations" : {
369
+ " fnproject.io/fn/invokeEndpoint" : " http://localhost:8080/invoke/01DJ900WFKNG8G00GZJ0000002"
370
+ },
371
+ " app_id" : " 01DJ90077CNG8G00GZJ0000001" ,
372
+ " created_at" : " 2019-08-14T21:19:58.451Z" ,
373
+ " id" : " 01DJ900WFKNG8G00GZJ0000002" ,
374
+ " idle_timeout" : 30,
375
+ " image" : " fndemouser/nodefn:0.0.3" ,
376
+ " memory" : 128,
377
+ " name" : " nodefn" ,
378
+ " timeout" : 30,
379
+ " updated_at" : " 2019-08-14T21:21:09.868Z"
380
+ }
380
381
` ` `
381
382
382
- The output confirms that nodeapp contains a `nodefn` function that can be called via this URL.
383
+ The output confirms that ` nodefn` functions invoke endpoint is:
384
+ ` http://localhost:8080/invoke/01DJ900WFKNG8G00GZJ0000002` . We can use this URL
385
+ to call the function.
383
386
384
387
# ## Invoke with Curl
385
388
386
- The other way to invoke your function is via HTTP. The Fn server exposes our
387
- deployed function at `http://localhost:8080/t/nodeapp/nodefn`, a URL
388
- that incorporates our application and function trigger as path elements .
389
+ Once we have the invoke endpoint, the second method for invoking our function
390
+ can be used, HTTP. The Fn server exposes our deployed function at
391
+ ` http://localhost:8080/invoke/01DJ900WFKNG8G00GZJ0000002 ` .
389
392
390
393
Use curl to invoke the function:
391
394
392
395
! [user input](images/userinput.png)
393
396
> ` ` ` sh
394
- > curl -H "Content-Type: application/json" http://localhost:8080/t/nodeapp/nodefn
397
+ > curl -X " POST " - H " Content-Type: application/json" http://localhost:8080/invoke/01DJ900WFKNG8G00GZJ0000002
395
398
> ` ` `
396
399
397
400
The result is once again the same.
@@ -405,7 +408,7 @@ function back.
405
408
406
409
! [user input](images/userinput.png)
407
410
> ` ` ` sh
408
- > curl -H "Content-Type: application/json" -d ' {" name" :" Bob" }' http://localhost:8080/t/nodeapp/nodefn
411
+ > curl -X " POST " - H " Content-Type: application/json" -d ' {"name":"Bob"}' http://localhost:8080/invoke/01DJ900WFKNG8G00GZJ0000002
409
412
> ` ` `
410
413
411
414
The result is once again the same.
0 commit comments