Action basics
Creating Actions - Helloworld
Create a JavaScript file named 'hello.js' with these contents:
function main(params) { return {payload: 'Hello, ' + params.name + ' from ' + params.place}; }The JavaScript file might contain additional functions. However, by convention, a function called
mainis the default entry point for the action.Create an action from the 'hello.js' JavaScript function naming it
hello:ibmcloud fn action create hello hello.jsList all actions; it should show the
helloaction you just created:ibmcloud fn action listactions <NAMESPACE>/hello private nodejs10You can see the
helloaction you just created under your default NAMESPACE with implied access control provided by the platform.
Takeaways
No special code needed, developer just the codes their favorite language
Convention: "Main" entrypoint assumed (can alias "main" to any function)
No build step: Runtimes for all supported languages already resident in "invoker" clusters for immediate function "injection".
NodeJS inferred (latest runtime version); can explicitly set with
--kindflagNamespaced: (default) installed into; allows underlying platform to apply IAM access control to any entity (Package, Action, Trigger, etc.)
Note: "update" action subcommand will update internal version of source code (allowing existing actions to complete with source code version in-flight)
Invoking Actions
You may invoke your action in one of two modes:
blocking - which will wait for the result (i.e., request/response style) by specifying the
blockingflag on the command-line.non-blocking - which will invoke the action immediately, but not wait for a response.
Regardless, invocations always provide an Activation ID which can be used later to lookup the action's response.
Blocking Invocations
A blocking invocation request will wait for the activation result to be available.
Invoke the
helloaction using the command-line as a blocking activation.Notes
The wait period is the lesser of 60 seconds or the action's configured time limit.
The Activation ID can always be used later to lookup the response.
Non-blocking invocations (Async)
A non-blocking invocation will invoke the action immediately, but not wait for a response.
If you don't need the action result right away, you can omit the --blocking flag to make a non-blocking invocation. You can get the result later by using the Activation ID.
Invoke the
helloAction using the command-line as a non-blocking activation.Retrieve the activation result
Alternatively, retrieve the
--lastor-lactivation resultRetrieve the full activation record
Retrieve activation list
Takeaways
Actions can be invoked directly using CLI
asynchronously: by default
synchronously:
--blockflag
System tracks each action invocation with as an "Activation" (record)
Lots of information about execution time/params, etc.
Passing Action Parameters
Event parameters can be passed to the action when it is invoked. Let's look at a sample action which uses the parameters to calculate the return values.
From command line using the --param flag
--param flagInvoke the
helloaction using explicit command-line parameters using the--paramflag.or using the
-pshort form:
Note We used the --result option above. It implies a blocking invocation where the CLI waits for the activation to complete and then displays only the function's output as the payload value.
From parameter file using the --param-file flag
--param-file flagCreate a file named
parameters.jsoncontaining the following JSON.Invoke the
helloaction using parameters from a JSON file.
"Binding" parameter defaults to Action
Rather than pass all the parameters to an action every time, you can bind default parameters. Default parameters are stored in the platform and automatically passed in during each invocation.
Let's use the hello action from our previous example and bind a default value for the place parameter.
From command line using --param
--paramUpdate the action by using the --param option to bind default parameter values.
From parameter file using --param-file
--param-filePassing parameters from a file requires the creation of a file containing the desired content in JSON format. The filename must then be passed to the --param-file flag:
Example: parameter file parameters2.json:
parameters2.json:Invoke the action, passing only the name parameter this time.
Notice that you did not need to specify the place parameter when you invoked the action.
Bound parameters can still be overwritten by specifying the parameter value at invocation time.
Command Line overrides defaults (binding values)
Invoke the action, passing both name and place values. The latter overwrites the value that is bound to the action.
Actions calling other Actions
Proxy example
Let's look an example of creating a "proxy" action which invokes another action (i.e, our hello action) if a "password" is present in the input parameters.
Create the following new action named
proxyfrom the following source files.Invoke the proxy with an incorrect password.
Note On the invoke call above, we used the short form for the --result flag which is -r.
Invoke the proxy with the correct password.
Review the activations list to show both actions were invoked.
Tip On the invoke call above, we used the short form for the -l with a parameter to only list the last 2 activations.
Note The Start values for each indicate if each Action was "cold" (slower, function reloaded) or "warm" (already loaded) started.
Takeaways
Platform functions simplified:
Simple import of NPM package (NodeJS developer familiar)
NPM Apache OpenWhisk JavaScript library
Actions call other actions
Last updated