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

Focus on your Functions with Serverless Swift

David Okun
October 31, 2017

Focus on your Functions with Serverless Swift

David Okun talks about how to use IBM Cloud Functions in Swift as deployed on IBM Cloud.

David Okun

October 31, 2017
Tweet

More Decks by David Okun

Other Decks in Programming

Transcript

  1. FOCUS ON YOUR
    FUNCTIONS
    WITH SERVERLESS SWIFT
    // BY DAVID OKUN
    // @DOKUN24

    View Slide

  2. I just want to point out this really funny joke that someone made to me on twitter - this
    was a pretty good joke, but it should give you the idea that this talk will involve more than
    just mobile apps.

    View Slide

  3. I do love a good joke though.

    View Slide

  4. I want to tell you a story about myself. I'm fortunate enough to have learned some new things here
    and there, but at the time of this story that i'm going to tell you, I only knew mobile. but, life was
    good! ever since apple forced me to learn Swift (or else), I was happy with functional programming
    !"

    View Slide

  5. I had lots of contracts to work on. I earned a lot of money. things were simple, and I was
    living the dream. However, one day, the server dev went home sick, passing away in a
    bizarre gardening accident.
    !"

    View Slide

  6. and suddenly, I was on my own. I felt the shivering cold abyss of having to figure out how
    to handle servers. what was I going to do now? I hadn't really dont anything with servers,
    so I was a bit nervous about diving into a world I knew nothing about.
    !

    View Slide

  7. Not only that, but how would I figure out how to handle server side code, while
    simultaneously worrying about how to maintain a server? How will I work with a load
    balancer? How do I deploy to a server? WHAT WILL I DO?!?!?!?!?!
    !

    View Slide

  8. Let's say hello to Jenny, and I have to tell you that Jenny
    saved my bacon.
    MEET JENNY,
    EVERYONE

    View Slide

  9. Jenny told me about this concept called Serverless code, and thanks to the industry
    moving forward as quickly as it was, I was able to use this architecture with a language I
    knew quite well already...
    !

    View Slide

  10. Swift! whew, that makes things a bit easier. The concept of serverless can be
    tremendously helpful for me who just needs to "get it done", and this came across as a
    bit of a lifesaver.
    SERVERLESS
    NOW WITH SWIFT!

    View Slide

  11. I slept like a stone that night, because I felt like I'd be able to
    figure this out, if it's really just Swift.
    !

    View Slide

  12. but what does this even mean? I mean...can you really have
    cloud data without a server?
    WHAT EXACTLY DOES
    SERVERLESS MEAN?

    View Slide

  13. I started to get worried again, thinking I'd have to do some black magic
    under the hood, servers without servers. what does this term actually mean?
    !

    View Slide

  14. In a word, there are still servers, but, I had to think about what my responsibility really was
    when it came to "serverless" development. Thankfully, Jenny was able to explain it to me
    with a quote that still sticks with me, and perfectly

    View Slide

  15. just read the quote.
    “Serverless computing refers to a model where the existence of servers
    is simply hidden from developers. I.e. that even though servers still
    exist developers are relieved from the need to care about their
    operation. They are relieved from the need to worry about low-level
    infrastructural and operational details such as scalability, high-
    availability, infrastructure-security, and so forth.”

    View Slide

  16. yes, servers are still involved, but I had to think about the
    service that's provided to me.
    TL;DR
    ▸ servers

    View Slide

  17. the server is maintained for me. Jenny explained to me that
    I just have to worry about what happens with the code
    TL;DR
    ▸ servers
    ▸ without the server maintenance

    View Slide

  18. and in this domain, I can refer to your server side code as
    functions.
    TL;DR
    ▸ servers
    ▸ without the server maintenance
    ▸ only the functions

    View Slide

  19. So what is this package called?

    View Slide

  20. and it's changed the way I think about server side
    development in certain languages.
    APACHE
    OPENWHISK

    View Slide

  21. it makes me cry tears of joy knowing that there is a tool that
    enables me to do server side development this easily.
    !

    View Slide

  22. ohhhhhh and by the way its called IBM Cloud Functions
    now
    JK, IT'S CALLED IBM CLOUD FUNCTIONS NOW

    View Slide

  23. So I asked Jenny, "Hey, why did you change the name?",
    and Jenny told me, "Hey, you ask too many questions."
    !

    View Slide

  24. So Jenny explained, "All you have to do is write a function!" And she showed
    me this, telling me that there are a couple of things to point out here.
    func main(args: [String:Any]) -> [String:Any] {
    guard let name = args["name"] as? String else {
    return ["error" : "No name included"]
    }
    return ["output" : "Sick presentation, \(name)!"]
    }

    View Slide

  25. 1) my function must be called "main"
    func main(args: [String:Any]) -> [String:Any] {
    guard let name = args["name"] as? String else {
    return ["error" : "No name included"]
    }
    return ["output" : "Sick presentation, \(name)!"]
    }

    View Slide

  26. 1) my function must be called "main"
    2) my parameters must be passed in as
    "args"
    func main(args: [String:Any]) -> [String:Any] {
    guard let name = args["name"] as? String else {
    return ["error" : "No name included"]
    }
    return ["output" : "Sick presentation, \(name)!"]
    }

    View Slide

  27. 1) your function must be called "main"
    2) your parameters must be passed in as
    "args"
    3) drink a beer, because I'm all set up now.
    !

    View Slide

  28. But I didn't think I was done yet. I knew I could get this working on my local machine, but I knew
    nothing about deploying to the cloud. I mean, I only work for IBM, so cloud computing doesn't come
    naturally to me. So I asked how I can get this going, and she told me, "look, it's just 5 easy steps."
    !

    View Slide

  29. she showed me these five commands. "Follow these, and
    you'll be ready to go", she told me.
    wsk action create swiftAction function.swift
    wsk action update swiftAction function.swift
    wsk action invoke swiftAction --param name David
    wsk action invoke swiftAction --param name David -b
    wsk action invoke swiftAction --param name David -b -r

    View Slide

  30. first, I have to actually create the action. she told me to think of an action like a file that
    runs a set of functions I can type in. all I have to do is reference the file in this command,
    name my action, and I'm ready to go.
    wsk action create swiftAction function.swift
    wsk action update swiftAction function.swift
    wsk action invoke swiftAction --param name David
    wsk action invoke swiftAction --param name David -b
    wsk action invoke swiftAction --param name David -b -r

    View Slide

  31. ok: created action swiftAction
    !

    View Slide

  32. similarly, whenever I updates my code, I can update the
    way my action works with this command.
    wsk action create swiftAction function.swift
    wsk action update swiftAction function.swift
    wsk action invoke swiftAction --param name David
    wsk action invoke swiftAction --param name David -b
    wsk action invoke swiftAction --param name David -b -r

    View Slide

  33. ok: updated action swiftAction
    !

    View Slide

  34. then she told me that, in order to actually run this action, I'll need
    to invoke it. I can specify as many parameters as I want this way
    wsk action create swiftAction function.swift
    wsk action update swiftAction function.swift
    wsk action invoke swiftAction --param name David
    wsk action invoke swiftAction --param name David -b
    wsk action invoke swiftAction --param name David -b -r

    View Slide

  35. ok: invoked /_/swiftAction with id a97abefa372b401cbabefa372b701ceb
    !

    View Slide

  36. so I did this, and I didn't really see anything returned to me but an ID. she told me that this is because these
    functions are returned asynchronously. I wanted to try them out right in the CLI, so she told me to add the -
    b, which stands for "blocking". This makes it so that I can see all the output returned right in my cli.
    wsk action create swiftAction function.swift
    wsk action update swiftAction function.swift
    wsk action invoke swiftAction --param name David
    wsk action invoke swiftAction --param name David -b
    wsk action invoke swiftAction --param name David -b -r

    View Slide

  37. ok, now this is a lot. It's nice that it does all of this for me,
    but there's only something specific i'm really interested in.
    ok: invoked /_/swiftAction with id b220f14bc15e41cea0f14bc15eb1ce04
    {
    "activationId": "b220f14bc15e41cea0f14bc15eb1ce04",
    "annotations": [
    {
    "key": "limits",
    "value": {
    "logs": 10,
    "memory": 256,
    "timeout": 60000
    }
    },
    {
    "key": "path",
    "value": "david.okun_dev/swiftAction"
    }
    ],
    "duration": 20,
    "end": 1508961870578,
    "logs": [],
    "name": "swiftAction",
    "namespace": "david.okun_dev",
    "publish": false,
    "response": {
    "result": {
    "output": "Sick presentation, David!"
    },
    "status": "success",
    "success": true
    },
    "start": 1508961870558,
    "subject": "[email protected]",
    "version": "0.0.2"
    }
    !

    View Slide

  38. I care most about checking only the output here.
    ok: invoked /_/swiftAction with id b220f14bc15e41cea0f14bc15eb1ce04
    {
    "activationId": "b220f14bc15e41cea0f14bc15eb1ce04",
    "annotations": [
    {
    "key": "limits",
    "value": {
    "logs": 10,
    "memory": 256,
    "timeout": 60000
    }
    },
    {
    "key": "path",
    "value": "david.okun_dev/swiftAction"
    }
    ],
    "duration": 20,
    "end": 1508961870578,
    "logs": [],
    "name": "swiftAction",
    "namespace": "david.okun_dev",
    "publish": false,
    "response": {
    "result": {
    "output": "Sick presentation, David!"
    },
    "status": "success",
    "success": true
    },
    "start": 1508961870558,
    "subject": "[email protected]",
    "version": "0.0.2"
    }
    !

    View Slide

  39. so she told me to add a -r to my call, which stands for
    "result". So i added this to my command, and boom!
    wsk action create swiftAction function.swift
    wsk action update swiftAction function.swift
    wsk action invoke swiftAction --param name David
    wsk action invoke swiftAction --param name David -b
    wsk action invoke swiftAction --param name David -b -r

    View Slide

  40. I had what I needed, and I was ready to get my hands dirty.
    {
    "output": "Sick presentation, David!"
    }
    !

    View Slide

  41. This was deliberately high level, so if you'd like to take a
    deeper dive into this world while you're at swift summit...
    WANNA BE LIKE
    JENNY?
    OF COURSE YOU DO

    View Slide

  42. this lab will go, from start to finish, by setting up a swift action, calling out to an API, using
    Swift you already know to modify the output, and sending it back down in your response.
    Ideally, you'll leave the lab with a live Swift function, ready to go.
    HANG OUT WITH ME FROM
    3:00 - 4:00 PM FOR A HANDS-
    ON LAB WHERE WE'LL GET
    THE PRICE OF BITCOIN!

    View Slide

  43. as a pre-req, go here and sign up for an account, so that
    linking your CLI is easier to do than otherwise. its free!
    https://console.bluemix.net/registration/

    View Slide

  44. thank you so much, and I'll see you at 3!
    !
    @dokun24

    View Slide