Guide

Have questions?

Head over to the forums to search for your questions and issues or post a new one.

Edit on github

#OpenWhisk - Functions

If you are using OpenWhisk as a provider, all functions inside the service are OpenWhisk Actions.

#Configuration

All of the OpenWhisk Actions in your serverless service can be found in serverless.yml under the functions property.

# serverless.yml
service: myService

provider:
  name: openwhisk
  runtime: nodejs:6 # optional, default is nodejs:default
  memory: 512 # optional, default is 256
  timeout: 30 # optional, default is 60

functions:
  hello:
    handler: handler.hello # required, handler set in Apache OpenWhisk
    name: some_custom_name # optional, default is ${service}_${function}
    runtime: nodejs # optional overwrite, default is provider runtime
    memory: 512 # optional overwrite, default is 256
    timeout: 10 # optional overwrite, default is 60
    parameters:
      foo: bar // default parameters

The handler property points to the file and module containing the code you want to run in your function.

// handler.js
exports.handler = function(params) {}

You can add as many functions as you want within this property.

# serverless.yml

service: myService

provider:
  name: openwhisk

functions:
  functionOne:
    handler: handler.functionOne
    description: optional description for your Action
  functionTwo:
    handler: handler.functionTwo
  functionThree:
    handler: handler.functionThree

Your functions can either inherit their settings from the provider property.

# serverless.yml
service: myService

provider:
  name: openwhisk
  runtime: nodejs:6
  memory: 512 # will be inherited by all functions

functions:
  functionOne:
    handler: handler.functionOne

Or you can specify properties at the function level.

# serverless.yml
service: myService

provider:
  name: openwhisk
  runtime: nodejs:6

functions:
  functionOne:
    handler: handler.functionOne
    memory: 512 # function specific
    parameters:
      foo: bar // default parameters

You can specify an array of functions, which is useful if you separate your functions in to different files:

# serverless.yml
...

functions:
  - ${file(../foo-functions.yml)}
  - ${file(../bar-functions.yml)}
# foo-functions.yml
getFoo:
  handler: handler.foo
deleteFoo:
  handler: handler.foo

#Packages

OpenWhisk provides a concept called "packages" to manage related actions. Packages can contain multiple actions under a common identifier in a namespace. Configuration values needed by all actions in a package can be set as default properties on the package, rather than individually on each action.

Packages are identified using the following format: /namespaceName/packageName/actionName.

#Implicit Packages

Functions can be assigned to packages by setting the function name with a package reference.

functions:
  foo:
    handler: handler.foo
    name: "myPackage/foo"
  bar:
    handler: handler.bar
    name: "myPackage/bar"

In this example, two new actions (foo & bar) will be created using the myPackage package.

Packages which do not exist will be automatically created during deployments. When using the remove command, any packages referenced in the serverless.yml will be deleted.

#Explicit Packages

Packages can also be defined explicitly to set shared configuration parameters. Default package parameters are merged into event parameters for each invocation.

functions:
  foo:
    handler: handler.foo
    name: "myPackage/foo"

resources:
  packages:
    myPackage:
      parameters:
        hello: world

Explicit packages support the following properties: parameters, annotations and shared.

#Binding Services (IBM Cloud Functions)

This feature requires the IBM Cloud CLI and IBM Cloud Functions plugin to be installed.

IBM Cloud Functions supports automatic binding of service credentials to actions using the CLI.

Bound service credentials will be passed as the __bx_creds parameter in the invocation parameters.

This feature is also available through the serverless.yaml file using the bind property for each function.

functions:
  my_function:
    handler: file_name.handler    
    bind:
      - service:
          name: cloud-object-storage
          instance: my-cos-storage

The service configuration supports the following properties.

  • name: identifier for the cloud service
  • instance: instance name for service (optional)
  • key: key name for instance and service (optional)

If the instance or key properties are missing, the first available instance and key found will be used.

Binding services removes the need to manually create default parameters for service keys from platform services.

More details on binding service credentials to actions can be found in the official documentation and this blog post.

Packages defined in the resources section can bind services using the same configuration properties.

resources:
  packages:
    myPackage:
      bind:
        - service:
            name: cloud-object-storage
            instance: my-cos-storage

#Runtimes

The OpenWhisk provider plugin supports the following runtimes.

  • Node.js
  • Python
  • Java
  • Php
  • Swift
  • Binary
  • Docker

Please see the following repository for sample projects using those runtimes.

https://github.com/serverless/examples/