Whisk Deploy — Action, Trigger, and Rule

Priti Desai
Apache OpenWhisk
Published in
3 min readMar 17, 2017

This story is a follow up on Getting Started with Whisk Deploy, in which we learnt how to automate deployment of simple hello world action. Now, lets look at how we can automate:

Passing Parameters to Hello World Action:

cat hello.js
function main(params) {
return {payload: 'Hello, ' + params.name + ' from ' + params.place};
}

Step 1: Create a manifest file (manifest.yaml)

Define parameters by adding “inputs” section under action name helloworld:

packages:
helloworld:
actions:
helloworld:
location: src/hello.js
runtime: nodejs:6
inputs:
name:
type: string
description: name of a person
place:
type: string
description: location of a person
outputs:
payload:
type: string
description: a simple greeting message, Hello World!

Now, we can also specify parameter values in manifest file and skip next step of creating deployment file with:

packages:
helloworld:
actions:
helloworld:
location: src/hello.js
runtime: nodejs:6
inputs:
name: Amy
place: Paris
outputs:
payload:
type: string
description: a simple greeting message, Hello World!

Step 2: Create a deployment file (deployment.yaml)

Set default parameters by adding “inputs” section under action name helloworld:

application:
name: SampleHelloWorld
namespace: _
packages:
helloworld:
actions:
helloworld:
inputs:
name: Amy
place: Paris

Make sure you have the same directory structure as before.

Step 3: Deploy Hello World Action

./wskdeploy -p ~/SampleHelloWorldApp/
____ ___ _ _ _ _ _
/\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __
/\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ /
/ \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ <
\ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\
\___\/ |_|
Packages:
Name: helloworld
* action: helloworld
bindings:
- name: name value: Amy
- name: place value: Paris
Triggers: RulesDo you really want to deploy this? (y/N): y
Deploying pacakge helloworld ... Done!
Deploying action helloworld/helloworld ... Done!
Deployment completed successfully.

Step 4: Verify your deployment

wsk action invoke --blocking --result helloworld/helloworld
{
"payload": "Hello, Amy from Paris"
}

You can override defaults with “ — param” flag:

wsk action invoke --blocking --result helloworld/helloworld --param name Mark --param place Barcelona
{
"payload": "Hello, Mark from Barcelona"
}

Create Trigger and Rule

Step 1: Update the manifest file (manifest.yaml)

Add a section called “triggers” and “rules”:

packages:
helloworld:
actions:
helloworld:
location: src/helloworld.js
runtime: nodejs:6
inputs:
name:
type: string
description: name of a person
place:
type: string
description: location of a person
outputs:
payload:
type: string
description: a simple greeting message, Hello World!
triggers:
locationUpdate:
rules:
helloworldOnLocationUpdate:
action: helloworld
trigger: locationUpdate

Step 2: Deploy Hello World action, trigger, and rule:

./wskdeploy -p ~/SampleHelloWorldApp/
____ ___ _ _ _ _ _
/\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __
/\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ /
/ \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ <
\ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\
\___\/ |_|
Packages:
Name: helloworld
* action: helloworld
bindings:
- name: name value: Amy
- name: place value: Paris
Triggers:
* trigger: locationUpdate
bindings:
Rules
* rule: helloworldOnLocationUpdate
- trigger: locationUpdate
- action: helloworld
Do you really want to deploy this? (y/N): y
Deploying pacakge helloworld ... Done!
Deploying action helloworld/helloworld ... Done!
Deploying trigger locationUpdate ... Done!
Deploying rule helloworldOnLocationUpdate ... Done!
Deployment completed successfully.

Step 3: Verify your deployment

  • Poll for running actions:
wsk activation poll
Enter Ctrl-c to exit.
Polling for activation logs
  • Fire the trigger:

Open one more terminal and fire the trigger:

wsk trigger fire locationUpdate
ok: triggered locationUpdate with id 4c3a8b1792d546a68ac58538c3f5d637
  • Result from polling:
wsk activation poll
Enter Ctrl-c to exit.
Polling for activation logs
Activation: helloworld (d545c458f3d34d6fbf5c29173be3d29e)
[]
Activation: locationUpdate (4c3a8b1792d546a68ac58538c3f5d637)
[]
Activation: helloworldOnLocationUpdate (c099355c1f1f4d6d8d30f54e8dac2b84)
[]
  • Determine activation ID from polling and get the result of that action:
wsk activation get d545c458f3d34d6fbf5c29173be3d29e
ok: got activation d545c458f3d34d6fbf5c29173be3d29e
{
...
"activationId": "d545c458f3d34d6fbf5c29173be3d29e",
"start": 1489444142544,
"end": 1489444142598,
"response": {
"status": "success",
"statusCode": 0,
"success": true,
"result": {
"payload": "Hello, Amy from Paris"
}
},
...
}

Enjoy!

Next, we will look at more complex triggers and action sequence.

--

--

Priti Desai
Apache OpenWhisk

Developer Lead @IBM. Tekton maintainer. Co-founder of License Scanner @Cyclonedx