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.

Dad87927739931f134c5b1242e3d04bc?s=128

James Thomas

April 03, 2018
Tweet

Transcript

  1. SERVERLESS SWIFT WITH OPENWHISK JAMES THOMAS, IBM @THOMASJ

  2. SERVERS KILL YOUR PRODUCTIVITY @THOMASJ

  3. WHAT CAN WE DO? @THOMASJ

  4. Introducing… SERVERLESS PLATFORMS @THOMASJ

  5. “RUNS CODE IN RESPONSE TO EVENTS, MANAGES COMPUTE RESOURCES” …WHY

    IS THIS DIFFERENT? @THOMASJ
  6. FUNCTIONS-AS-A-SERVICE @THOMASJ

  7. SCALING: 0 to N @THOMASJ

  8. PAY-AS-YOU-GO flickr photo by a loves dc https://flickr.com/photos/alovesdc/3466740007 CC (BY)

    license @THOMASJ
  9. ENOUGH TALKING…
 SHOW ME THE CODE. @THOMASJ

  10. PROVIDERS @THOMASJ

  11. @THOMASJ

  12. OPEN SOURCE @THOMASJ

  13. RUNTIMES @THOMASJ

  14. } 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
  15. 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
  16. 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
  17. ACTIONS @THOMASJ

  18. Platform API HTTP Request Microservice @THOMASJ

  19. $ 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
  20. Platform API HTTP Request Microservice @THOMASJ

  21. WHAT ABOUT… API ROUTING? 
 RATE LIMITING? AUTHENTICATION? DEPLOYMENTS? @THOMASJ

  22. Platform API API Gateway Microservice @THOMASJ

  23. TRIGGERS @THOMASJ

  24. External Events Trigger @THOMASJ

  25. EVENT SOURCES @THOMASJ

  26. Rules Rules @THOMASJ

  27. CHALLENGES @THOMASJ

  28. “WARM” vs “COLD” ⛄ @THOMASJ

  29. $ 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
  30. $ 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
  31. $ 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
  32. COMPUTE LIMITS flickr photo by zund https://flickr.com/photos/zund/12202485675 CC (BY-NC-ND) license

    @THOMASJ
  33. @THOMASJ

  34. SERVERS KILL YOUR PRODUCTIVITY @THOMASJ

  35. FIRE YOUR SERVERS + = UPGRADE PRODUCTIVITY @THOMASJ

  36. http://bit.ly/burn_your_servers slack.openwhisk.org #openwhisk @thomasj or @openwhisk

  37. QUESTIONS? @THOMASJ