Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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.

Slide 3

Slide 3 text

I do love a good joke though.

Slide 4

Slide 4 text

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 !"

Slide 5

Slide 5 text

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. !"

Slide 6

Slide 6 text

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. !

Slide 7

Slide 7 text

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?!?!?!?!?! !

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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... !

Slide 10

Slide 10 text

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!

Slide 11

Slide 11 text

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. !

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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? !

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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.”

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

So what is this package called?

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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)!"] }

Slide 25

Slide 25 text

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)!"] }

Slide 26

Slide 26 text

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)!"] }

Slide 27

Slide 27 text

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. !

Slide 28

Slide 28 text

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." !

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

ok: created action swiftAction !

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

ok: updated action swiftAction !

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

ok: invoked /_/swiftAction with id a97abefa372b401cbabefa372b701ceb !

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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" } !

Slide 38

Slide 38 text

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" } !

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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!

Slide 43

Slide 43 text

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/

Slide 44

Slide 44 text

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