This is a series of translated notes by Priti Desai . Since the notes themselves are quite short, several publications will be collected at once in one publication. Details of the deployment of applications for OpenWhisk with examples will be considered, and at the end of the cycle, the architecture and code of the application using serverless computing based on OpenWhisk will be given.
Automate application deployment for OpenWhisk
Have you already installed OpenWhisk and tested the simple hello.js function in work?
cat hello.js function main() { return {payload: 'Hello World'}; }
Do you want to automate the deployment of this simplest function? If so, continue reading this note. I will show how to use openwhisk-wskdeploy to automate the deployment of a simple function.
Required Components
Step one
Create a manifest file (manifest.yaml) with the following contents:
packages: helloworld: actions: helloworld: location: src/hello.js runtime: nodejs:6 outputs: payload: type: string description: a simple greeting message, Hello World.
Second step
Create a file for deployment (deployment.yaml):
application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld:
In fact, this file is not required for the delivery of helloworld
, just the manifest from the first step is enough.
Step three
Create a directory structure. It looks something like this:
# ls -1R ~/SampleHelloWorldApp/ deployment.yaml manifest.yaml src/ ./src: hello.js
Fourth step
Deploying the HelloWorld function:
./wskdeploy -p ~/SampleHelloWorldApp/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \___\/ |_| Packages: Name: helloworld * action: helloworld bindings: Triggers: Rules Do you really want to deploy this? (y/N): y Deploying pacakge helloworld ... Done! Deploying action helloworld/helloworld ... Done! Deployment completed successfully.
Fifth step
Check performance:
# wsk action invoke --blocking --result helloworld/helloworld { "payload": "Hello World" }
Sixth step
Rejoice!
Deployment: Function, Condition, and Rule
Let's look at the additional things that are needed in automation:
- passing function parameters
- setting default values ββfor a function
- setting conditions for operation
- creating a rule for binding conditions and functions
Passing parameters to a function
Modify the code:
function main(params) { return {payload: 'Hello, ' + params.name + ' from ' + params.place}; }
Step one
Create a manifest file:
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!
You can specify parameter values ββin the manifest file and skip creating the file for deployment:
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!
Second step
Create a file for deployment:
We set the default values ββby adding the "inputs" section nested in the "helloworld" function:
application: name: SampleHelloWorld namespace: _ packages: helloworld: actions: helloworld: inputs: name: Amy place: Paris
We check that the directory structure has not changed from the last time.
Step three
Expand the function:
# ./wskdeploy -p ~/SampleHelloWorldApp/ ____ ___ _ _ _ _ _ /\ \ / _ \ _ __ ___ _ __ | | | | |__ (_)___| | __ /\ /__\ \ | | | | '_ \ / _ \ '_ \| | | | '_ \| / __| |/ / / \____ \ / | |_| | |_) | __/ | | | |/\| | | | | \__ \ < \ \ / \/ \___/| .__/ \___|_| |_|__/\__|_| |_|_|___/_|\_\ \___\/ |_| Packages: Name: helloworld * action: helloworld bindings: - name: name value: Amy - name: place value: Paris Triggers: Rules Do you really want to deploy this? (y/N): y Deploying pacakge helloworld ... Done! Deploying action helloworld/helloworld ... Done! Deployment completed successfully.
Fourth step
Check performance:
wsk action invoke --blocking --result helloworld/helloworld { "payload": "Hello, Amy from Paris" }
Override the default values ββby passing values ββusing the --param parameter:
wsk action invoke --blocking --result helloworld/helloworld --param name Mark --param place Barcelona { "payload": "Hello, Mark from Barcelona" }
Creating a condition and binding rules
Step one
Add two sections to the manifest:
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
Second step
Expand the function, condition and binding 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 three
Check performance:
- We look at the working functions:
wsk activation poll Enter Ctrl-c to exit. Polling for activation logs
- open another terminal and enter the command to activate the conditional response:
wsk trigger fire locationUpdate ok: triggered locationUpdate with id 4c3a8b1792d546a68ac58538c3f5d637
- look at the results in the first terminal:
Activation: helloworld (d545c458f3d34d6fbf5c29173be3d29e) [] Activation: locationUpdate (4c3a8b1792d546a68ac58538c3f5d637) [] Activation: helloworldOnLocationUpdate (c099355c1f1f4d6d8d30f54e8dac2b84) []
- we determine the id of the conditional response and check the results of the function:
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" } }, ... }
Announcement
Serverless computing based on OpenWhisk, part 1
Serverless computing based on OpenWhisk, part 2
OpenWhisk Serverless Computing, Part 3
Serverless computing based on OpenWhisk, part 4