Back to Openwhisk

Licensed to the Apache Software Foundation (ASF) under one or more

docs/samples.md

2.0.02.4 KB
Original Source
<!-- # # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # -->

OpenWhisk samples

Here is a list of OpenWhisk community resources, which includes more samples, complete OpenWhisk applications, articles, tutorials, podcasts and much more.

<!-- TODO "Complete listing of OpenWhisk samples can be found here." <- need to insert a link to the OpenWhisk samples repo when there is one -->

OpenWhisk Hello World example

To get started with OpenWhisk, try the following JavaScript code example.

/**
 * Hello world as an OpenWhisk action.
 */
function main(params) {
    var name = params.name || 'World';
    return {payload:  'Hello, ' + name + '!'};
}

To use this example, follow these steps:

  1. Save the code to a file. For example, hello.js.

  2. From the OpenWhisk CLI command line, create the action by entering this command:

    $ wsk action create hello hello.js
    
  3. Then, invoke the action by entering the following commands.

    $ wsk action invoke hello --result
    

    This command outputs:

    {
        "payload": "Hello, World!"
    }
    
    $ wsk action invoke hello --result --param name Fred
    

    This command outputs:

    {
        "payload": "Hello, Fred!"
    }
    

You can also use the event-driven capabilities in OpenWhisk to invoke this action in response to events. Follow the alarm service example to configure an event source to invoke the hello action every time a periodic event is generated.

CLI tutorial

If you prefer learning while doing, consider using this OpenWhisk Workshop.