Apache OpenWhisk Runtime Swift supports Apache OpenWhisk functions written in Swift

Clone this repo:
  1. 027bb83 Support array result include sequence action (#150) by ningyougang · 5 hours ago master
  2. 6f827ba Bumping to gradle 6 (#151) by Seonghyun Oh · 7 days ago
  3. 60a6022 build actionloop from 1.16@1.19.0 (#148) by David Grove · 3 months ago
  4. 493656d fix travis url (#147) by David Grove · 10 months ago
  5. af55b15 roll credentials (#146) by David Grove · 11 months ago

Apache OpenWhisk runtimes for swift

License Build Status


Quick Swift Action

Simple swift action hello.swift

The traditional support for the dictionary still works:

import Foundation

func main(args: Any) -> Any {
    let dict = args as! [String:Any]
    if let name = dict["name"] as? String {
        return [ "greeting" : "Hello \(name)!" ]
    } else {
        return [ "greeting" : "Hello stranger!" ]

For the return result, not only support dictionary, but also support array

So a very simple hello array function woule be:

func main(args: Any) -> Any {
    var arr = ["a", "b"]
    return arr

And support array result for sequence action as well, the first action‘s array result can be used as next action’s input parameter.

So the function can be:

 func main(args: Any) -> Any {
     return args

When invokes above action, we can pass an array object as the input parameter.

Swift 5.x support

Some examples of using Codable In and Out

Codable style function signature

Create file helloCodableAsync.swift

import Foundation // Domain model/entity struct Employee: Codable { let id: Int? let name: String? } // codable main function func main(input: Employee, respondWith: (Employee?, Error?) -> Void) -> Void { // For simplicity, just passing same Employee instance forward respondWith(input, nil) }
wsk action update helloCodableAsync helloCodableAsync.swift swift:5.1
ok: updated action helloCodableAsync
wsk action invoke helloCodableAsync -r -p id 73 -p name Andrea
    "id": 73,
    "name": "Andrea"

Codable Error Handling

Create file helloCodableAsync.swift

import Foundation struct Employee: Codable { let id: Int? let name: String? } enum VendingMachineError: Error { case invalidSelection case insufficientFunds(coinsNeeded: Int) case outOfStock } func main(input: Employee, respondWith: (Employee?, Error?) -> Void) -> Void { // Return real error do { throw VendingMachineError.insufficientFunds(coinsNeeded: 5) } catch { respondWith(nil, error) } }
wsk action update helloCodableError helloCodableError.swift swift:5.1
ok: updated action helloCodableError
wsk action invoke helloCodableError -b -p id 51 -p name Carlos
  "name": "helloCodableError",
  "response": {
    "result": {
      "error": "insufficientFunds(5)"
    "status": "application error",
    "success": false

Packaging an action as a Swift executable using Swift 5.x

When you create an OpenWhisk Swift action with a Swift source file, it has to be compiled into a binary before the action is run. Once done, subsequent calls to the action are much faster until the container holding your action is purged. This delay is known as the cold-start delay.

To avoid the cold-start delay, you can compile your Swift file into a binary and then upload to OpenWhisk in a zip file. As you need the OpenWhisk scaffolding, the easiest way to create the binary is to build it within the same environment as it will be run in.

Compiling Swift 5.x

Compiling Swift 5.x single file

Use the docker container and pass the single source file as stdin. Pass the name of the method to the flag -compile

docker run -i openwhisk/action-swift-v5.1 -compile main <main.swift >../action.zip

Compiling Swift 5.1 multiple files with dependencies

Use the docker container and pass a zip archive containing a Package.swift and source files a main source file in the location Sources/main.swift.

zip - -r * | docker run -i openwhisk/action-swift-v5.1 -compile main >../action.zip

For more build examples see here