Swift AWS Lambda Tutorial | Serverless Swift | SLaM

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on everybody it's your boy kiloloco and today i got something real special for you so we're gonna be working with aws lambda and we're gonna actually be writing our lambda in swift which is really cool now if you're not familiar with aws lambda you can think of it as like a cloud function where you are able to trigger some type of event and then your code runs and then whatever your code does you know it just goes so i'm going to show you an example of that today and the reason why you would want to work with something like a lambda is because there are a lot of times where you actually need a back end and lambda can actually replace a back end if you want to and i'm going to kind of show you how you do that but essentially anybody that was ever interested in writing you know server side swift whether it be you know vapor or katura or perfect or whatever you want this is kind of like another option except you don't have to worry about spinning up a server you only have to worry about creating a lambda now i made a cli that is going to make it so much easier for you to create that lambda and we're going to actually go through those steps today trigger our lambda from an ios app and you're going to see the whole entire process it's actually extremely easy now that we have the cli so if you're curious why you would use a lambda there are a lot of times where you you are almost required to use a back end and one of those examples would be like if you wanted to verify a receipt from an in-app purchase or if you wanted to send push notifications you need to have some type of back end and this lambda can act as that back end so like i said we're going to cover everything here today we won't be covering those specific examples that i just named if you're interested in seeing videos on that make sure you leave a like on this video so that i know that this is a topic that you guys want to go further in if we get over 100 likes in the first week then i will definitely make a um a video on one of those topics whatever you guys leave in the comments so let's go ahead and jump right on in now the very first thing that we want to do is make sure that we have slam insult installed on our computer and slam is this cli that i created um this past week that's called uh swift lambda maker and all it's going to do is it's going to essentially make it so that you can easily create a swift lambda project which it uses spm and it allows you to package that part project into a docker file now don't worry i don't know a whole lot about docker you won't really need to know anything about docker to get started but there are some pre-requisites you're going to need swift 5.3 or higher you could probably get away with using 5.0 and higher but i recommend 5.3 and then um you're gonna need docker so you're gonna want to actually install um docker right here you go over here to docker desktop it's free you know just get docker desktop make sure that you go through the the things to get it all set up and you're gonna be good to go you don't really need to know anything about docker that's what this does for you so first things first you just get it installed you clone the repo you change directory into that repo that you just cloned and then you run make it's gonna install it you can take a look at the make file nothing too crazy going on in there simply running swift build and install um into your local bin then what you're going to want to do is you're going to actually want to make a directory where you're going to keep this project in the terminal so let's go over to the terminal and as you can see we're in a directory but i want to make a new one i'll just go ahead and follow the doc the documentation copy and paste make directory my first lambda i'll hit enter i'm going to change directory into my first lambda and then i'm going to run slam new so this is going to create the actual project the swift package for us and we can open that package with zed period and it's going to open up our project in xcode so once our project's all opened up you're going to see that it's just your basic swift package but there are a couple of added files and we made sure that the type is executable so you'll be able to see some of the differences right here that we're using specifically 5.3 that's why i recommended 5.3 and you're going to see that we do have the swift lambda runtime right here added as a dependency and then we have the the aws lambda runtime right here and the aws lambda events right here at it under the dependencies of the target so those are that's those are some of the differences platform also is mac os so that's another difference and then we have this package dot sh this is a script that you know does some docker stuff and then um you know we have this docker file as well so these are like all the dependencies that are needed in order to create the docker file so i'm showing i'm showing all this stuff to you just so that you're aware of it because you can technically just make this um from a swift package by itself and then you can you know just copy and paste it or you can type them out or whatever you want nothing special is really going on it just kind of eliminates that step for you now back over here in main you can see that we have our lambda if you were to actually run this then you would be able to get a hello world so what we want to do is we'll go ahead and edit our scheme our schema and then we're going to add an environment variable right here and this is so that we can actually test out this lambda locally so go ahead and enter in the keyword local lambda server enabled and you're going to set the value to true so you want it to look like that and once you have it set like that we can go ahead and close it we can run and then we get an output that looks something like this so i know this may be new to a lot of the ios developers that are watching so i'm going to just walk through it what it's saying is that we're running a local lambda server and you can actually interact with that server on port 127001 or port 7000 on you know this whatever and you can access it by calling slash invoke now if you wanted to test this out you can do it in the terminal with curl but i think more people are going to be more familiar with postman so that's what i'll show you with all right so if you have postman you can try it out right here um and what we want to do is we want to do a post request and then we want to pass in this address right here which is 127.001 7000 and we're going to just pass that in right here we want to do the slash invoke so that we can trigger the lambda and then over here in the body we need to make sure that we send some type of data so i'll just go ahead and since this is a hello world right this is basically a hello world it's going to say hello and then whatever the payload that comes from the data that we're sending right here so in this case i'm going to say hello and then my name is kyle so let's go ahead and send that and then as you can see we get the response right here hello kyle and if we wanted to just make sure that this was updated we can just go ahead and do oh hi and we can run it again all right we're up and running and if we just do send we should see that it's updated so that's pretty much all you need to do in order to get a hello world running with this um you know with slam so i think that's really cool we're going to do something a little bit slightly more complex just so you can get an idea of of some of the things that you can do so what i'm going to do is i'm going to create a movie class and that movie is just going to have a title on it so let's go ahead and add that struct in now alright so as you can see here we have our simple movie struct it's codable and it just has a title right so our payload can be anything that is decodable so we are going to be sending some type of payload up to our lambda just like any api request right and the payload has to be decodable so string is obviously decodable but we want to actually accept our movie so if you do codable encodable and decodable then obviously it conforms to decodable so now we're going to be receiving a movie and our payload what we want to do is let's go ahead and return back a list of movies so what we'll do is we'll send up a movie and then we'll return back a list of movies with our new movie added to it alright so as you can see i simply change the type of payload to be of type movie and then we're just going to create a variable called movies and then we'll just simply append um our payload to that movie all kinds of ways that you could have done this but i thought that this would illustrate it the best and then now what we want to do is return our movies right so what we have to do is we have to change the completion and we have to essentially change this to an array of movies because that's what's going to go back to the um the caller so the caller is going to make this request it's going to send up a movie and we have to return something back we don't want to return a string we want to return an array of movies so now that we have that this is our result and we want to just simply say success and we'll pass in the movies as the success so if we go ahead and run this and after it finishes building what we can do is we can go back over to postman and now we have to change our um our payload because if we try to send this this isn't gonna work you try to send it you get an error right we have to send up a movie essentially so we do that by sending the json that essentially represents a movie so we need to send some json up that has a title and a value all right so as you can see we have our title which we're calling it that because that's what it has to be right and then we have the value for the title so let's go ahead and press send and as you can see it returns back an array of json where we have greenland we have hunter hunter and we have wonder woman 84. saw those first two didn't get to see wonder woman 84 but i want to see it in the near future but anyway okay back over here so now we have our our lambda all set up and this is everything that we want to do to it right so once you're done editing your lambda you can either keep this open or close it i'm just going to go ahead and close it make sure i stop the task and also what i want to do is i want to make sure that i uncheck the um the schema i just want to make sure that that's unchecked i don't think it makes a difference but it's better to be safe than sorry so i'm going to uncheck that right there and what we'll do is we'll head back over to the terminal and now that i have my lambda ready i am just going to call slam uh setup image so this is going to set up a docker image for us so give the image whatever name that you want to give it i'm just going to call it my first lamb the image like that i would follow the lowercase naming convention i'm not sure what the naming convention rules are for docker images but i'd follow this convention right here so i'm going to call it my first lambda image and this may take a while depending on your system but after it's done you should get you know some type of pop-up that says that it's done so if you wanted to check if the the lambda image has been created you can just simply run docker images and that will show you all your images as you can see mine is right here right there so what we're gonna do is i'm going to kill this and i'm going to simply run slam export and once you run slam export you need to enter in the same name that you just entered in to create the image so whatever you called the image you just pass that in right here so i called mine my first lambda image i'm going to go ahead and hit enter and it's going to start building the package now this will take a long time um it has to go through and build it it has to download dependencies and all kinds of craziness so you may want to take a step away from your computer it could take like three five three to five minutes and then come back and it should be done all right so mine took about two to three minutes and you can see like after it was able to successfully do a process it will spit out like all the logs that um of everything that went down in the background so as you can see we have the lambda package and it also lets you know where you can find that zip file that we're going to actually be using shortly so if you want to know exactly where that zip file that you're going to be using to upload to the lambda you can find it right here so that's pretty much all you have to do in regards to the local part right but now we actually need to take this docker image that we just zipped up and we need to put that on aws so let's go ahead and go over to aws.com you're going to go ahead and sign in and once you're signed in you're going to look for lambda you can find it by just searching up here land like that go ahead and click on it you'll go ahead and go up to create function you're gonna do uh author from scratch and then you call it whatever you want so i'll call it my first lambda and the run time what you really need to do you need to make sure that you do this right and let me just move this over here so that i'm not blocking anything you really want to get this right you want to go to provide your own bootstrap on amazon linux2 so you select that and then i think that's all you really need to do just make sure that create new role with basic lambda permissions is selected and you should be good to go don't need any advanced settings you just create the function from there all right that took about like 20 seconds to to finish creating it but we have my first lambda here and then down right here where you see function code you would go to actions and you're going to go to upload a zip file now it's going to recommend that you uh you know upload it to s3 amazon s3 and then upload it here just simply because it's going to be bigger than 10 megabytes but we're not obeying rules today so what we're gonna do is we're going to go to our project and you can see that's one of my uh my other ones and just go to where the lambda was stored all right so i'm finally at my root directory i'm going to go to my lambda directory and if you don't see the dot build scripts then what you need to do is you need to hit command shift and then period and then that's going to show you your secret folders anything that's prefixed with a period so we're going to go to dot build you're going to go to lambda you're going to go to the only folder in there and you're going to select the lambda.zip file so select that one and you're going to go to save all right and that's pretty much it your lambda has been uploaded to lambda aws lambda so you're pretty much done after this point this is where depending on what you're trying to accomplish everything will deviate now what i'm going to do is i'm going to show you the easiest way that i know how to trigger this lambda to actually get it up and working with a real world url so the easiest way that i know how to do it is to actually use api gateway you don't need to follow these steps but i'm going to show you how to work with work with api gateway to trigger the lambda so we can use it in an ios app so we'll just open up aws.com again have to sign in again and what we're going to do is we're going to be working with api gateway and then what we're going to do is we're going to create an api so what we want to do is we want to create a new rest api or this one will build and you could call it whatever you want and make sure that new api is selected you call it whatever you want um we'll just say regional and we'll do create api from here what we're going to do is we're going to go to actions we're going to do create resource no create method maybe yeah method and then we'll do post we want to do a post method then we'll hit the check mark it will create a new one we'll do integrated type lambda function and we're going to specify the lambda function that we want to use so we can just give it the name that we created with our lambda function like this we'll paste it here and we're going to save it it's going to tell you what the arn is and you just want to verify that that's right i know that it is and then what we're going to do is we're going to simply test this so we can test this locally before we deploy and actually have a real world url so we can test and remember in the request body just like we did with postman we need to create um a new movie so we're gonna send in a movie object deadpool was the first thing that came to mind so uh as you can see we have the title we have the value and we're just going to go ahead and test that when we run it we can see that we get our array back with our three movies so the first two that are automatically hard coded and then whatever we sent is in their last so we have greenland hunter hunter and then deadpool so really cool stuff now that we see that our api is working with our lambda we simply go over here we go to deploy api we're going to enter in a new stage we call it whatever you want i'll call mine dev and you just simply go to deploy once it's deployed it will give you the url which you can invoke and this is a live working url i'm going to delete this afterwards so if you try it it's not going to work just because you know i don't want you guys to be blowing it up but essentially you'll take this url and you can open up an ios app and start using it there so i'm going to go to my ios app and open that xcode up alright so i opened up my ios app which i already had set up and ready to start working with whatever um whatever back-end we ended up with so as you can see i have our movie object pretty similar to what we have over in our lambda already um ready to go uh we have just a basic list and then i'm gonna have and i have a basic h stack where i'm just simply having a field where i can enter in something and then press send and send uh that value up to our lambda so over here we're just gonna do a simple url uh url request and we're gonna make sure that it's a post method and then we create our movie we create that data put it into the http body and then we're gonna do a data task and you know if we're able to do that then we should get back an array of movies in which case we'll just set that to movies and we'll remove the text and you know test out resume you can't you can't forget that one right so for the path all we need to do is we need to just simply take this url right here and we'll paste it right there so assuming that all works let's go ahead and run our app alright so as you can see it's all running and i have a cursor right here i can start typing so another movie is let's say black widow something that never made it out uh black widow and we hit send and as you can see we get back greenland hunter hunter and then whatever we just entered which is black widow so that is probably the simplest example that i can show you on how to use lambda and get up and running i think this is as easy as it's going to get in order to essentially create a backend with you know using serverless swift and we were able to do this extremely quickly like i said this is going to be super helpful for those of you that need to have a back end in order to do things like verify receipts for subscriptions if you have in-app purchases and things like that or if you wanted to send push notifications which is a video that i'll be covering very soon and you know just a lot of different back-end requirements so this is this is the thing to go to you're writing swift all the way through it's really easy you have slam to help you get up and running and hopefully this video is super helpful to you in the future so that's going to be it for today if you have any other recommendations for videos that you want to have covered then make sure you leave them in the description if i get more than 100 likes on this video within the first week then what i'll do is i'll make a video covering how to do like verifying in-app purchases or something like that so thanks again for your time now go out there and keep coding passionately
Info
Channel: Kilo Loco
Views: 1,903
Rating: 5 out of 5
Keywords: serverless swift, serverside swift, swift on the server, swift lambda, aws lambda, aws lambda swift, lambda swift aws, lambda slam, swift slam, aws lambda tutorial, swift lambda tutorial, swift aws lambda tutorial, swift lambda example, serverless swift tutorial, serverless swift example, swift cloud function, lambda cloud function, swift firebase, swift backend, swift push notifications, swift subscription, ios lambda, xcode lambda, spm lambda, spm lambda tutorial
Id: OVsIASkVj14
Channel Id: undefined
Length: 21min 45sec (1305 seconds)
Published: Fri Jan 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.