Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Serverless Swift with Apache OpenWhisk

Serverless Swift with Apache OpenWhisk

Do you want to build backend applications using Swift but don’t want to manage computing infrastructure to run those applications in the cloud? Enter serverless cloud platforms…
These services allow developers to push code, rather than VMs, into the cloud. The platforms allow you to connect external event sources like API requests or message queues to functions in your code. As events occur, your code is instantiated and executed to process each request. Developers are only billed for the milliseconds needed to process each request.
In this session, developers will learn how to build Swift microservices using modern “serverless” cloud platforms. We’ll look at common challenges (and solutions) to building applications using “serverless” stacks. Exploring emerging “serverless” design patterns will give developers the knowledge to build application architectures using these new platforms.

James Thomas

April 03, 2018
Tweet

More Decks by James Thomas

Other Decks in Technology

Transcript

  1. } guard let amount = args["amount"] as? Int else {

    return [ "error": "Missing mandatory argument: amount" ] } guard let prices = currentBitcoinPricesJson() else { return [ "error": "Unable to retrieve Bitcoin prices" ] } guard let rate = prices["bpi"][currency]["rate_float"].double else { return [ "error": "Currency not listed in Bitcoin prices" ] } let converted = Double(amount) / rate let bitcoins = converted.truncate(places: 6) return ["amount": bitcoins, "label": "\(amount) \(currency) is worth \ (bitcoins) bitcoins."] } my_code.swift @THOMASJ
  2. func main(args: [String:Any]) -> [String:Any] { if let name =

    args["name"] as? String { return [ "greeting" : "Hello \(name)!" ] } else { return [ "greeting" : "Hello anonymous!" ] } } Entry Point Event Parameters Service Response Swift 3
  3. struct Person: Codable { let name: String? } struct Message:

    Codable { let greeting: String } func main(p: Person, callback: (Message?, Error?) -> Void) -> Void { if let name = p.name { callback(Message(greeting: "Hello \(name)"), nil) } else { callback(Message(greeting: "Hello anonymous!"), nil) } } Swift 4 Event Parameters Service Result Callback Response
  4. $ wsk action create —help create a new action Usage:

    wsk action create ACTION_NAME ACTION [flags] Flags: --web string treat ACTION as a web action
 @THOMASJ
  5. $ time wsk action invoke bitcoin -p amount 1000 -p

    currency USD -r { "amount": 0.246287, "label": "1000.0 USD is worth 0.246287 bitcoins." } real 0m4.638s user 0m0.095s sys 0m0.054s
 $ time wsk action invoke bitcoin -p amount 1000 -p currency USD -r { "amount": 0.246287, "label": "1000.0 USD is worth 0.246287 bitcoins." } real 0m1.003s user 0m0.093s sys 0m0.045s @THOMASJ
  6. $ time wsk action invoke … { "amount": 0.246287, "label":

    “...” } real 0m4.638s user 0m0.095s sys 0m0.054s ——> start container ——> inject code ——> swift build ——> call main() ——> pause container $ time wsk action invoke … { "amount": 0.246287, "label": “...” } real 0m1.003s user 0m0.093s sys 0m0.045s ——> resume container ——> inject code ——> swift build ——> call main() ——> pause container @THOMASJ
  7. $ time wsk action invoke … { "amount": 0.246287, "label":

    “...” } real 0m1.438s user 0m0.095s sys 0m0.054s ——> start container ——> inject code ——> swift build ——> call main() ——> pause container $ time wsk action invoke … { "amount": 0.246287, "label": “...” } real 0m1.003s user 0m0.093s sys 0m0.045s ——> resume container ——> inject code ——> swift build ——> call main() ——> pause container @THOMASJ