Share
Embed
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
Transcript
44 slides
-
FOCUS ON YOUR FUNCTIONS WITH SERVERLESS SWIFT // BY DAVID
OKUN // @DOKUN24 -
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. -
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 !" -
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. !" -
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. ! -
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?!?!?!?!?! ! -
Let's say hello to Jenny, and I have to tell
you that Jenny saved my bacon. MEET JENNY, EVERYONE -
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... ! -
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! -
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. ! -
but what does this even mean? I mean...can you really
have cloud data without a server? WHAT EXACTLY DOES SERVERLESS MEAN? -
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? ! -
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 -
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.” -
yes, servers are still involved, but I had to think
about the service that's provided to me. TL;DR ▸ servers -
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 -
and in this domain, I can refer to your server
side code as functions. TL;DR ▸ servers ▸ without the server maintenance ▸ only the functions -
and it's changed the way I think about server side
development in certain languages. APACHE OPENWHISK -
it makes me cry tears of joy knowing that there
is a tool that enables me to do server side development this easily. ! -
ohhhhhh and by the way its called IBM Cloud Functions
now JK, IT'S CALLED IBM CLOUD FUNCTIONS NOW -
So I asked Jenny, "Hey, why did you change the
name?", and Jenny told me, "Hey, you ask too many questions." ! -
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)!"] } -
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)!"] } -
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)!"] } -
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. ! -
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." ! -
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 -
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 -
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 -
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 -
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 -
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" } ! -
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" } ! -
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 -
I had what I needed, and I was ready to
get my hands dirty. { "output": "Sick presentation, David!" } ! -
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 -
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! -
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/ -
thank you so much, and I'll see you at 3!
! @dokun24