Serverless computing based on OpenWhisk, part 1







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 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:









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








All Articles