The process of creating Swift actions is similar to that of other actions. The following sections guide you through creating and invoking a single Swift action, and demonstrate how to bundle multiple Swift files and third party dependencies.
Tip: You can use the Online Swift Playground to test your Swift code without having to install Xcode on your machine.
Note: Swift actions run in a Linux environment. Swift on Linux is still in development, and OpenWhisk usually uses the latest available release, which is not necessarily stable. In addition, the version of Swift that is used with OpenWhisk might be inconsistent with versions of Swift from stable releases of Xcode on MacOS.
An action is simply a top-level Swift function. For example, create a file called
hello.swift with the following content:
In this example the Swift action consumes a dictionary and produces a dictionary.
You can create an OpenWhisk action called
helloSwift from this function as
New in Swift 4 in addition of the above main function signature there are two more signatures out of the box taking advantage of the Codable type. You can learn more about data types encodable and decodable for compatibility with external representations such as JSON here.
The following takes as input parameter a Codable Input with field
name, and returns a Codable output with a field
In this example the Swift action consumes a Codable and produces a Codable type. If you don't need to handle any input you can use the function signature that doesn't take any input, only Codable output.
You can create a OpenWhisk action called
helloSwift from this function as
See the Swift reference for more information about the Swift runtime.
Find out more about parameters in the Working with parameters section.
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.
The docker runtime includes a compiler to help users compile and package Swift 4.2 actions.
To compile a single source file that doesn't depend on external libraries you can use the following command:
The docker container reads from stdin the content of the file, and writes to stdout a zip archive with the compiled swift executable.
Use the flag
-compile with the name of the main method.
The zip archive is ready for deployment and invocation using the kind
To compile multiple files and include external dependencies create the following directory structure.
Sources/ should contain a file named
Package.swift should start with a comment specifying version
4.2 for the Swift tooling:
Create a zip archive with the content of the directory:
Pass the zip archive to the docker container over stdin, and the stdout will be a new zip archive with the compiled executable. The docker container reads from stdin the content of the zip archive, and writes to stdout a new zip archive with the compiled swift executable.
In a Linux based system you can combined the
docker run steps in a single command:
action-bin.zip archive is ready for deployment and invocation using the kind
With the new Codable completion handler, you can pass an Error to indicate a failure in your Action.
Error handling in Swift resembles exception handling in other languages, with the use of the
try, catch and
The following example shows a an example on handling an error
Swift 4 actions are executed using Swift 4.2 using
--kind swift:4.2 respectively.
--kind swift:default is Swift 4.2.
Swift 4.x action runtimes don't embed any packages, follow the instructions for packaged swift actions to include dependencies using a Package.swift.
Large portions of this page is copied from the Apache OpenWhisk documentation on April 23rd 2021 - where there have been customisations to match Hypi's deployment this has been noted. Apache OpenWhisk and the Apache name are the property of the Apache Foundation and licensed under the Apache V2 license .