Skip to content

Commit

Permalink
Adding new OpenWhisk examples.
Browse files Browse the repository at this point in the history
Added HTTP events, schedule events and function chaining examples.
  • Loading branch information
James Thomas committed Jan 25, 2017
1 parent 8e5ddc5 commit fe713a8
Show file tree
Hide file tree
Showing 16 changed files with 361 additions and 9 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ Have an example? Submit a PR or [open an issue](https://github.com/serverless/ex
| [Aws Alexa Skill](https://github.com/serverless/examples/tree/master/aws-python-alexa-skill) <br/> This example demonstrates how to use an AWS Lambdas for your custom Alexa skill. | python |
| [Aws Scheduled Cron](https://github.com/serverless/examples/tree/master/aws-python-scheduled-cron) <br/> Example of creating a function that runs as a cron job using the serverless `schedule` event | python |
| [Aws Simple Http Endpoint](https://github.com/serverless/examples/tree/master/aws-python-simple-http-endpoint) <br/> Example demonstrates how to setup a simple HTTP GET endpoint with python | python |
| [Openwhisk Node Simple](https://github.com/serverless/examples/tree/master/openwhisk-node-simple) <br/> Boilerplate project repository for OpenWhisk provider with Serverless Framework. | nodeJS |
| [Openwhisk Node Chaining Functions](https://github.com/serverless/examples/tree/master/openwhisk-node-chaining-functions) <br/> Example of chaining function calls using sequences and the sdk. | nodeJS |
| [Openwhisk Node Scheduled Cron](https://github.com/serverless/examples/tree/master/openwhisk-node-scheduled-cron) <br/> Example of creating a function that runs as a cron job using the serverless schedule event. | nodeJS |
| [Openwhisk Node Simple Http](https://github.com/serverless/examples/tree/master/openwhisk-node-simple-http-endpoint) <br/> Example demonstrates how to setup a simple HTTP GET endpoint with OpenWhisk. | nodeJS |
| [Openwhisk Node Simple](https://github.com/serverless/examples/tree/master/openwhisk-node-simple) <br/> Simple example demonstrating OpenWhisk provider support. | nodeJS |

<!-- AUTO-GENERATED-CONTENT:END -->

Expand All @@ -67,7 +70,9 @@ Have an example? Submit a PR or [open an issue](https://github.com/serverless/ex
| **[Keboola Developer Portal](https://github.com/keboola/developer-portal)** <br/> Keboola developer portal built with Serverless | [keboola](http://github.com/keboola) |
| **[Pfs Email Serverless](https://github.com/SCPR/pfs-email-serverless)** <br/> This is a lambda function created by the serverless framework. It searches through members in our mongodb who have not been sent emails and sends them an email with their custom token to unlock the pledge free stream. It then marks those members off as already receiving the email. | [SCPR](http://github.com/SCPR) |
| **[Plaid Cashburndown Service](https://github.com/cplee/cashburndown-service)** <br/> Service for calculating cash burndown with plaid. Frontend code can be found here: https://github.com/cplee/cashburndown-site | [cplee](http://github.com/cplee) |
| **[Sc5 Serverless Boilerplate](https://github.com/SC5/sc5-serverless-boilerplate)** <br/> A boilerplate that contains setup for test-driven development | [SC5](http://github.com/SC5) |
| **[Serverless Aws Rekognition Finpics](https://github.com/rgfindl/finpics)** <br/> Use AWS Rekognition to provide a faces search of finpics.com | [rgfindl](http://github.com/rgfindl) |
| **[Serverless Blog To Podcast](https://github.com/SC5/serverless-blog-to-podcast)** <br/> Service that reads RSS feed and converts the entries to a podcast feed and audio files using Amazon Polly | [SC5](http://github.com/SC5) |
| **[Serverless Cloudwatch Rds Custom Metrics](https://github.com/AndrewFarley/serverless-cloudwatch-rds-custom-metrics)** <br/> A NodeJS-based MySQL RDS Data Collection script to push Custom Metrics to Cloudwatch with Serverless | [AndrewFarley](http://github.com/AndrewFarley) |
| **[Serverless Delivery Framework](https://github.com/99xt/serverless-delivery-framework)** <br/> This is a boilerplate for version release pipeline with serverless framework | [99xt](http://github.com/99xt) |
| **[Serverless Facebook Quotebot](https://github.com/pmuens/quotebot)** <br/> 100% Serverless Facebook messenger chatbot which will respond with inspiring quotes | [pmuens](http://github.com/pmuens) |
Expand Down
48 changes: 48 additions & 0 deletions openwhisk-node-chaining-functions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Serverless Boilerplate - OpenWhisk - Node.js

Make sure `serverless` is installed. [See installation guide](https://serverless.com/framework/docs/providers/openwhisk/guide/installation/).

You will also need to set up your OpenWhisk account credentials using environment variables or a configuration file. Please see the [this guide for more information](https://serverless.com/framework/docs/providers/aws/guide/credentials/).

## 1. Install Provider Plugin
`npm install -g serverless-openwhisk`

## 2. Install Service Dependencies
`npm install` in this directory to download the modules from `package.json`.

## 3. Deploy
`serverless deploy` or `sls deploy`. `sls` is shorthand for the Serverless CLI command

Make a note of the API endpoint that is logged to the console during deployment.

```
Serverless: Configured API endpoint: https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service
```

## 4. Invoke sequence function
`serverless invoke --function chained_seq --data '{"message": "a b c d e"}'`

`-f` is also shorthand for `--function`

In your terminal window you should see the response from Apache OpenWhisk

```bash
{
"message": "e d c b a"
}
```

## 5. Invoke chained function
`serverless invoke --function manual_seq --data '{"message": "a b c d e"}'`

`-f` is also shorthand for `--function`

In your terminal window you should see the response from Apache OpenWhisk

```bash
{
"message": "e d c b a"
}
```

**For more information on the Serverless OpenWhisk plugin, please see the project repository: [https://serverless.com/framework/docs/providers/aws/guide/credentials/](https://serverless.com/framework/docs/providers/aws/guide/credentials/).**
14 changes: 14 additions & 0 deletions openwhisk-node-chaining-functions/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

function chain(parameters) {
// eslint-disable-next-line global-require, import/no-extraneous-dependencies
const ow = require('openwhisk')();

const invoke = (actionName, params) => ow.actions.invoke({ actionName, params, blocking: true });
return invoke('my_service-dev-split', parameters)
.then(res => invoke('my_service-dev-reverse', res.response.result))
.then(res => invoke('my_service-dev-join', res.response.result))
.then(res => res.response.result);
}

module.exports.chain = chain;
9 changes: 9 additions & 0 deletions openwhisk-node-chaining-functions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "openwhisk-node-chaining-functions",
"version": "0.1.0",
"description": "Example of chaining function calls using sequences and the sdk.",
"scripts": {
"postinstall": "npm link serverless-openwhisk",
"test": "echo \"Error: no test specified\" && exit 1"
}
}
36 changes: 36 additions & 0 deletions openwhisk-node-chaining-functions/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
#
# Happy Coding!

service: my_service # NOTE: update this with your service name

provider:
name: openwhisk

functions:
split:
handler: utils.split
reverse:
handler: utils.reverse
join:
handler: utils.join
chained_seq:
sequence:
- split
- reverse
- join
manual_seq:
handler: handler.chain

# remember to run npm install to download the provider plugin.
plugins:
- serverless-openwhisk
17 changes: 17 additions & 0 deletions openwhisk-node-chaining-functions/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

function split(params) {
return { message: params.message.split(' ') };
}

function join(params) {
return { message: params.message.join(' ') };
}

function reverse(params) {
return { message: params.message.reverse() };
}

module.exports.split = split;
module.exports.join = join;
module.exports.reverse = reverse;
52 changes: 52 additions & 0 deletions openwhisk-node-scheduled-cron/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Serverless Boilerplate - OpenWhisk - Node.js

Make sure `serverless` is installed. [See installation guide](https://serverless.com/framework/docs/providers/openwhisk/guide/installation/).

You will also need to set up your OpenWhisk account credentials using environment variables or a configuration file. Please see the [this guide for more information](https://serverless.com/framework/docs/providers/aws/guide/credentials/).

## 1. Install Provider Plugin
`npm install -g serverless-openwhisk`

## 2. Install Service Dependencies
`npm install` in this directory to download the modules from `package.json`.

## 3. Deploy
`serverless deploy` or `sls deploy`. `sls` is shorthand for the Serverless CLI command

Make a note of the API endpoint that is logged to the console during deployment.

```
Serverless: Configured API endpoint: https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service
```

## 4. Invoke deployed function
`serverless invoke --function time` or `serverless invoke -f time`

`-f` is shorthand for `--function`

In your terminal window you should see the response from Apache OpenWhisk

```bash
{
"payload": "The time in Europe/London is: 16:01:14."
}
```

## 5. Test HTTP endpoint

Use a HTTP client to access the endpoint for your function. The endpoint will
be the API gateway root path, logged during deployment, and your configured
function path.

```
$ http get https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service/time
{
"payload": "The time in Europe/London is: 16:01:07."
}
$ http get https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service/time?timezone=Europe/Berlin
{
"payload": "The time in Europe/Berlin is: 17:01:11."
}
```

**For more information on the Serverless OpenWhisk plugin, please see the project repository: [https://serverless.com/framework/docs/providers/aws/guide/credentials/](https://serverless.com/framework/docs/providers/aws/guide/credentials/).**
9 changes: 9 additions & 0 deletions openwhisk-node-scheduled-cron/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

function cron() {
const time = new Date();
const name = '__OW_ACTION_NAME';
console.log(`Your cron function "${process.env[name]}" ran at ${time}`); // eslint-disable-line no-console
}

module.exports.cron = cron;
9 changes: 9 additions & 0 deletions openwhisk-node-scheduled-cron/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "openwhisk-node-scheduled-cron",
"version": "0.1.0",
"description": "Example of creating a function that runs as a cron job using the serverless schedule event.",
"scripts": {
"postinstall": "npm link serverless-openwhisk",
"test": "echo \"Error: no test specified\" && exit 1"
}
}
27 changes: 27 additions & 0 deletions openwhisk-node-scheduled-cron/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
#
# Happy Coding!

service: my_service # NOTE: update this with your service name

provider:
name: openwhisk

functions:
cron:
handler: handler.cron
events:
- schedule: cron(* * * * *)

# remember to run npm install to download the provider plugin.
plugins:
- serverless-openwhisk
52 changes: 52 additions & 0 deletions openwhisk-node-simple-http-endpoint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Serverless Boilerplate - OpenWhisk - Node.js

Make sure `serverless` is installed. [See installation guide](https://serverless.com/framework/docs/providers/openwhisk/guide/installation/).

You will also need to set up your OpenWhisk account credentials using environment variables or a configuration file. Please see the [this guide for more information](https://serverless.com/framework/docs/providers/aws/guide/credentials/).

## 1. Install Provider Plugin
`npm install -g serverless-openwhisk`

## 2. Install Service Dependencies
`npm install` in this directory to download the modules from `package.json`.

## 3. Deploy
`serverless deploy` or `sls deploy`. `sls` is shorthand for the Serverless CLI command

Make a note of the API endpoint that is logged to the console during deployment.

```
Serverless: Configured API endpoint: https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service
```

## 4. Invoke deployed function
`serverless invoke --function time` or `serverless invoke -f time`

`-f` is shorthand for `--function`

In your terminal window you should see the response from Apache OpenWhisk

```bash
{
"payload": "The time in Europe/London is: 16:01:14."
}
```

## 5. Test HTTP endpoint

Use a HTTP client to access the endpoint for your function. The endpoint will
be the API gateway root path, logged during deployment, and your configured
function path.

```
$ http get https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service/time
{
"payload": "The time in Europe/London is: 16:01:07."
}
$ http get https://xxx-yyy-zzz-gws.api-gw.mybluemix.net/my_service/time?timezone=Europe/Berlin
{
"payload": "The time in Europe/Berlin is: 17:01:11."
}
```

**For more information on the Serverless OpenWhisk plugin, please see the project repository: [https://serverless.com/framework/docs/providers/aws/guide/credentials/](https://serverless.com/framework/docs/providers/aws/guide/credentials/).**
12 changes: 12 additions & 0 deletions openwhisk-node-simple-http-endpoint/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

const moment = require('moment-timezone');

function time(params) {
const timezone = params.timezone || 'Europe/London';
const timestr = moment().tz(timezone).format('HH:MM:ss');

return { payload: `The time in ${timezone} is: ${timestr}.` };
}

module.exports.time = time;
12 changes: 12 additions & 0 deletions openwhisk-node-simple-http-endpoint/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "openwhisk-node-simple-http",
"version": "0.1.0",
"description": "Example demonstrates how to setup a simple HTTP GET endpoint with OpenWhisk.",
"scripts": {
"postinstall": "npm link serverless-openwhisk",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"moment-timezone": "^0.5.11"
}
}
27 changes: 27 additions & 0 deletions openwhisk-node-simple-http-endpoint/serverless.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
#
# Happy Coding!

service: my_service # NOTE: update this with your service name

provider:
name: openwhisk

functions:
time:
handler: handler.time
events:
- http: GET time

# remember to run npm install to download the provider plugin.
plugins:
- serverless-openwhisk
31 changes: 28 additions & 3 deletions openwhisk-node-simple/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
# Serverless Boilerplate - OpenWhisk - Node.js

A Serverless Framework Boilerplate for OpenWhisk support with Node.js
Make sure `serverless` is installed. [See installation guide](https://serverless.com/framework/docs/providers/openwhisk/guide/installation/).

**Please see [instructions](https://github.com/serverless/serverless-openwhisk) for getting starting with the OpenWhisk plugin for The Serverless Framework. The README walks you through setting up this project.**
You will also need to set up your OpenWhisk account credentials using environment variables or a configuration file. Please see the [this guide for more information](https://serverless.com/framework/docs/providers/aws/guide/credentials/).

## 1. Install Provider Plugin
`npm install -g serverless-openwhisk`

## 2. Install Service Dependencies
`npm install` in this directory to download the modules from `package.json`.

## 3. Deploy
`serverless deploy` or `sls deploy`. `sls` is shorthand for the Serverless CLI command

## 4. Invoke deployed function
`serverless invoke --function hello_world` or `serverless invoke -f hello_world`

`-f` is shorthand for `--function`

In your terminal window you should see the response from Apache OpenWhisk

```bash
{
"payload": "Hello, World!"
}
```

Congrats you have just deployed and run your Hello World function!

**For more information on the Serverless OpenWhisk plugin, please see the project repository: [https://serverless.com/framework/docs/providers/aws/guide/credentials/](https://serverless.com/framework/docs/providers/aws/guide/credentials/).**

https://github.com/serverless/serverless-openwhisk
Loading

0 comments on commit fe713a8

Please sign in to comment.