Serverless Framework and OpenWhisk — Plugin Update (v0.6)
There is a new version of the OpenWhisk provider plugin for The Serverless Framework. This release adds support for non-Node.js runtimes, including Python, Swift, Docker and Binaries.
The update also includes improved error messages for application errors, invoke local
support for the Python runtime and showing Web Action URLs in serverless info
.
Use the following command to upgrade the provider plugin to the latest version (0.6).
npm update -g serverless-openwhisk
Due to an outstanding issue with provider plugins, the OpenWhisk provider must be installed as a global module.
Here are the new features supported in the latest release….
Python Support
Serverless functions can be written for the Python 2.7 or 3.6 runtimes.
Setting the runtime
parameter in serverless.yaml
will control the environment used.
service: python-serviceprovider:
name: openwhisk
runtime: python // defaults to python 2.7functions:
greeting:
handler: handler.endpoint
greeting_py3:
handler: handler.endpoint
runtime: python:3plugins:
- serverless-openwhisk
Python functions are invoked with a Dictionary containing event parameters. Functions must return a Dictionary with response values.
def endpoint(params):
name = params.get("name", "stranger")
greeting = "Hello " + name + "!"
print(greeting)
return {"greeting": greeting}
The invoke local
plugin has also been extended to support Python functions.
Swift Support
Serverless functions can be written for the Swift 3 runtime.
service: swift-serviceprovider:
name: openwhisk
runtime: swiftfunctions:
greeting:
handler: handler.mainplugins:
- serverless-openwhisk
Swift functions are invoked with a Dictionary containing event parameters. Functions must return a Dictionary with response values.
func main(args: [String:Any]) -> [String:Any] {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let now = formatter.string(from: Date()) if let name = args["name"] as? String {
return [ "greeting" : "Hello \(name)! The time is \(now)" ]
} else {
return [ "greeting" : "Hello stranger! The time is \(now)" ]
}
}
Binaries Support
OpenWhisk supports executing a compiled binary for the serverless function. The binary must be compiled for the correct platform architecture.
OpenWhisk executes the binary file for each request. Event parameters are streamed to stdio
as a JSON object string.
The handler must write a JSON object string with the response parameters to stdout
before exiting.
In the serverless.yaml
file, the handler
property is used to denote the binary file to upload.
functions:
my_function:
handler: bin_file
runtime: binary
Docker Support
OpenWhisk supports custom runtimes using public images on Docker Hub. These images are expected to support the platform API used to instantiate and invoke serverless functions.
All necessary files for execution must be provided within the image. Local source files will not be uploaded to the runtime environment.
In the serverless.yaml
file, the handler
property is used to denote the image label.
functions:
my_function:
handler: repo/image_name
runtime: docker
Web Action URLs
This serverless info
command will now show URLs for Web Actions.
$ serverless info
Service Information
platform: openwhisk.ng.bluemix.net
namespace: _
service: swift-serviceactions:
swift-service-dev-pingendpoints (web actions):
https://openwhisk.ng.bluemix.net/api/v1/web/james.thomas@uk.ibm.com_dev/default/swift-service-dev-ping
Runtime Examples
Sample projects for the new runtimes have been added to the Serverless Framework examples repository.
- Python (Simple)
- Python (HTTP API)
- Python (Scheduled Function)
- Swift (Simple)
- Swift (HTTP API)
- Swift (Scheduled Function)
- Go (Simple)
- Docker & Node.js (Sequence)
Issues & Future Plans
If you find bugs or have feature requests, please open issues in the Github repository.
https://github.com/serverless/serverless-openwhisk
Items planned for the next release are shown in the 0.7 milestone.
https://github.com/serverless/serverless-openwhisk/milestone/3