Serverless Framework with AWS Lambda Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody how's it going welcome back to the channel a few hours ago i bought a diet coke from a vending machine and that really got me thinking as to how vending machines work at least from a software perspective and i came up with the conclusion that most vending machines follow a serverless architecture and after research i realized that that was true and that kind of prompted me to create this course about serverless and explain to you what service serverless is and how it differs from a prototypical client server model so to explain what server list is let's just talk about exactly how typical applications work so let's say that you have an application and it's running in the browser and some user went on this application so when they go on this application they're going to be making a bunch of requests to your server now in a typical client server model you have a client and you have a server that is always running that is going to be listening to any http requests so let's say for instance that this is a to do application and a user goes on to it and wants to add a to-do they're going to make an http request probably some sort of post request to the server the server is always going to be running it's going to catch that run some sort of code and then it is going to send back a response now if no request is being made the server is still going to be up and running now if another request was made let's say a get request to get all of the to do's was going to make a get request to that same exact server that server is going to process it and then it's going to go ahead and send back a response but one thing to note is this server is always running it's consistently running in the background whether it's being used or not it is always going to be running so now let's talk about serverless so as you can imagine with serverless we don't have a consistent running server it's actually what happens is let's say we have the exact same to do application and instead now we're using a serverless architecture now once the user goes on to our web and let's say they make that same add to do post request well what happens at that point is you make the request and whatever cloud provider you're using is going to create a function and a function is just a block of functionality that contains a little bit of code and that block of functionality that code is going to deal with adding a post so whether that's you know validating the post and then adding it to your database and then sending back a response and that's pretty much it but one thing that's great about this is that function wasn't existing before so it was never running in the background it was actually created when this http request was made so once this http request was made this function was created it basically did all of the execution save it to our database and send back a response to our client and then when it's not used when no more http requests are made then it actually disappears it gets destroyed so there's no consistent running server in the background you only create something you only create this function this this code executable if you are actually using it if you need to use it now let's say you go ahead and you make maybe not a post request maybe you do a get request well that is going to spin up a different function that has a different block of functionality and that is going to deal with all of the code needed to get the to-do's in to the browser so whether that's you know going to the database fetching them all and then sending it back but again this function will never exist if you never trigger it with this http request and that right there in a nutshell is serverless you only create what you are using you never have this consistent running server now what triggers this the creation of a function well what triggers the creation of a function in this case are http requests so if i were to make a post request i create a function that deals with that logic if i were to make a get request i create a function that deals with that logic but there is many many more triggers than simple http requests so we can have for instance so over here we have http requests and that can trigger a function that saves it to the database but we can have other things for instance we can have a trigger that when we save an image to s3 it spins up a function that has all of the code to resize and optimize that image and then re-save that into s3 similarly we could have another trigger let's say we want to run a job every single day at 12 o'clock well that could be a trigger once we hit 12 o'clock we can have some sort of trigger that triggers this spin up of a function and that is going to go ahead and run a certain job for us maybe it's going to clear the database or it's going to do some sort of analytics at the end of the day is some sort of cron job but we can basically trigger a lambda function or some sort of function to do that through a certain time of day another thing we can do is hey once the payment is complete that could be a trigger for some sort of function to spin up and execute some sort of code involving sending an email to that particular user so we can have multiple multiple triggers out there these are just four examples but we can have a lot of different triggers and they're going to trigger the spin up of a function which again is a block of execution that goes this is going to execute some code that we want all right so that right there in a nutshell is serverless now let's start talking about the pros and cons of serverless so one big pro of serverless is that it's very very cheap compared to having a server and let's just talk about exactly why that is so when having a server you're always running that server so no matter whether you're using it or not you're inquiring cost so you could not be using it at all maybe you have no no people using your application but because it's always going to be running you're always inquiring cost so that is a very very big con now with serverless you only pay for what you use because if nobody is using your application there's nothing up and running but if somebody is using your application well okay that's fine you go ahead and you spin up a you spin up a function and yes you do end up paying for this function but now if nobody is using your application and making that request well that function goes away and then you stop paying so you only pay for what you use and i know for aws you get your first 1 million functions for free or something like that so it's very very very cheap all right cool so let's talk about another con it is fully or another pro rather it is fully managed and this is really really great so whether your application is making one request or a million requests it doesn't matter the cloud provider is going to manage it for you so you don't even have to worry about the devops so when it with a typical server you actually do have to worry about the devops so if if if uh your application is making one request okay it's maybe it's fine the way that it is but if all of a sudden it makes it spikes up to a hundred thousand requests an hour well you really have to think about your architecture you're gonna have to think about horizontal scaling you're gonna have to kind of really do these things manually whereas with serverless if you're making if your application is making one request per hour okay that's fine just gonna go ahead and spin up one function but if it's making a million requests per hour okay that's also fine you don't even have to worry about it all that it's going to do is just spin up more functions to handle those requests and you really don't have to worry about the architecture whether your application is making one request per hour or a million requests per hour which is really insane and i'll show you an example of a company that only has a team of one devops engineer and they're making thousands of requests an hour and that's because they're using the serverless architecture all right and then the last thing is that reduces complexity so the the perk of having it being fully managed is that you don't have to start worrying about scaling and all these things and maybe start thinking about horizontal scaling and maybe involving it with kubernetes uh you don't have to worry about that stuff and that actually significantly reduces the complexity that really reduces that complexity all right so now let's talk about some of the cons of serverless so one big con of serverless is cold starts and that can actually significantly decrease the performance of your application and let me explain to you exactly what cold starts are so once let's say you don't have a function so you don't have a function and now somebody came in and made an http request and this is of course a trigger for a function to create but at the moment you don't have a function so your cloud provider is going to have to create a function and this creation actually takes quite a bit of time not an insane amount of time in the grand scheme of things but if if you want something to be extremely performant well this is going to decrease your performance because this is going to take some time to spin up and then of course it has to execute the code and send back a response and so when you actually now don't have a function in the first place the spin up of a function is known as a cold start and that could take quite a bit longer than usual so that is a con but if your function is already spinned up and somebody else is making a request to it then any subsequent requests are going to be very very fast and those are actually known as warm starts so again if you need high high performance all the time then cold starts is something that you have to worry about another thing is you're locked in so you're really locked in with the vendor that you're using whether it's aws azure or google cloud because you're really relying on their serverless framework and what they are doing and you can't really kind of manage it yourself so that if that is the con to you then maybe serverless is not something that you want to use and the last thing is that serverless functions are not really suitable for long term tasks so these functions they should not be executing for 30 minutes or an hour they really should be executing some small piece of code like we have over here again save something to the database resize an image run a quick cron job send an email there shouldn't be some doing something insane like uploading a video or saving a video to your database it should be some sort of small code executable so if you're if you intend to run some long executable code maybe it's better to use some sort of server for that rather than a lambda function that should be something very very quick and i think actually lambda functions time out after uh 15 minutes and lambda functions are basically functions that aws uses but there's there's many different functions out there like there's there's functions for google cloud platform as well as azer all right so that is pretty much the pros and cons and now let's talk about some examples of serverless so as i illustrated a vending machines are a great example of serverless and actually coke you used to use the server model and then they used the serverless model and they saved almost half their money so so the cost actually went down by 50 which is pretty insane and the reason for this i mean this makes sense the vending machine you're not consistently using the vending machine the vending machine is not consistently being used so essentially only a customer comes and then they're going to make some sort of payment and then you're going to have to process that payment right away of course and then send back uh whatever coke that uh or whatever drink that they want however you don't want a running server to be running consistently because again this is very very event driven so you might have a period of time maybe an hour two hours where nobody is using this vending machine and so you don't want to have a consistent running server that is inquiring costs or even maybe it's night time and the cl the store is closed down and nobody even has access to this vending machine you really don't want to have a running server that is just continually continuously inquiring costs so coke actually realized this and then they were like hey you know what we're going to just use a serverless architecture so we're going to only being only pay for when we are being used so now what ends up happening is somebody comes in they're going to make a payment that's going to spin up by lambda function and then that lambda function is going to process that payment and then send back a response to the software inside of the uh coke or the vending machine and then that's going to basically send back whatever drink that they want but again that really saved them a lot of money because now they're only paying for what they use okay let's talk look at another example over here so another example is codepen so if you have if you're a developer you probably know what codepen is but codepen makes almost 200 000 requests an hour and they have a devops team of one and that just goes to show how fully managed a serverless architecture can be you really don't have to worry about the infrastructure at all and the last example is netflix just to show you a company that really a huge company that really adopts the serverless architecture netflix has been a huge proponent of serverless and they use it throughout their software and it just goes to show how powerful it can be and why it's such a necessary skill to learn alright so that is serverless i hope that got you excited and i'll see you guys in the next sections now that we talked about what serverless is let's actually create an environment where we can start spinning up some serverless functions now for this we are going to be relying on aws again there's many cloud providers out there like azure and google cloud platform that have their own serverless functions but i think by far the best ones exist in aws they are the absolute best ones and they're used very very commonly now in the aws they're called lambda functions and this is essentially what we are going to be using and you can see here it says run code without thinking about servers or clusters only pay for what you use so this is exactly what we want to use now what's great about these is that these serverless functions they can run in multiple different languages so let me zoom in here a little bit you can see here that it can run in node it can run in python and go or java so it doesn't really matter what you want to use and i and i believe they also support ruby and you can see here it says more so it's really supporting more and more types of languages now in this crash course we're going to be using node.js because i'm primarily a javascript developer and so that is what we are going to be using but the rules that we learn will apply to whatever language you ultimately want to use and so we're going to be using aws lambda functions and we're going to be using node.js now one thing that is very important to note is that you're going to need an aws account to do this and unfortunately you can't create a aws account without inserting your credit card information now one thing is with using this architecture using aws lambda you can see here that you get 1 million requests for free per month when you're using aws free tier which you will get for one year if using this so you're going to inquire very little if not no costs i i can pretty much guarantee you you're well i won't guarantee you but i i can pretty much guarantee that if you follow this you're not going to acquire a lot of costs you'll probably inquire nothing at all so i just want you to be careful but if you want to follow along go ahead and create a aws account but you will have to put in your credentials so please please keep that in mind and i'll show you how once we're done this crash course how to delete everything so that you don't inquire any more costs but again it should be very very minimal if not anything at all all right so again you're gonna have to create an aws account i already have one so i already have an aws account and it should look something like this now i'm not going to show you the steps of creating an aws account because well it's pretty easy you just create one put in your credentials and then put in your credit card information so i already have an aws account and now you can actually come here and you can actually search for lambda so let me quickly just search for lambda and you can see here that hey we can actually search for some lambda functions and you can see here that i actually have a bunch of different lambda functions and that's because i actually i worked on this course quite a long time ago i was just preparing it but let's actually just zoom in here a little bit you can see that i created this to do application so you can see here that i have an update to do and this is its own lambda function similarly over here we have another to do this is an add to do and its own lambda function is this way and then similarly over here we have a fetch to do and a fetch to do's and so these are their own lambda functions and let me just click on maybe fetch to do and so you can see here that hey we have some sort of function right over here now how are we triggering this well we're triggering this function with http requests however how is this http request going to this lambda function and the thing is it's not what ends up happening is we have this other service called api gateway and through api gateway all of our http requests are going to be made to api gateway and then api gateway is going to relay those requests to whatever lambda function we ultimately want to send it to so again this is how this works so we send alfa requests to api gateway and then api gateway then triggers the creation of a lambda function so that's ultimately what we're going to be doing and so if we want to essentially configure lambda functions that listen to http requests we also have to configure api gateway and we'll get to that a little bit later but don't worry this is just a quick example of how a lambda function will get up and running now i'm going to actually go ahead you might be in a different region i'm going to go ahead and just go to us west 2 and if i go to that you can see that i'm gonna have absolutely no lambda functions in here because the lambda functions are actually region specific so i'm gonna pick a brand new region and that's because again i want to uh have kind of a fresh start so right now there's absolutely no lambda functions and there's absolutely no um api gateway or any other configuration so we're gonna set this up from scratch now there's few approaches of setting this up we could set it up in the console so the this is basically the aws website we could set it up manually we could just create a function if we want to we can use some sort of blueprint or create it from scratch and so you can see here the blueprints are you know just some prototypical functions that have some code so we can we can do this uh from the console if we want to and then set up api gateway ourselves or we can do it through code and what actually happens is if you do it through code and you have it communicate with aws it's just going to do all these configurations for you so you don't have to do it manually inside of the browser and this is actually known as infrastructure as code and this is a very very popular approach of taking it and that's why we're going to be using the serverless framework so this serverless framework essentially what it does is allows us to specify all of our functions and all of our configuration in code and then it is going to create the functions for us it is going to create an api gateway for us all in aws so we don't have to worry about that manual configuration and this again is a very popular approach and there's other frameworks like terraform that allows us to specify our whole infrastructure as code so this is what we are going to be doing now in order to allow uh in order to allow the aws or sorry the serverless framework to actually communicate with our aws account we have to create an iam account and this is just an identity a management service which allows us to essentially manage who gets access to our aws account and so we need to allow the serverless framework and our local computer to have access to our aws account so essentially now what we have to do is we have to create this iam account now the first thing we also have to do is create the aws cli and the cli is just a command line interface that interacts with the aws client or our aws account and then we want to give that aws cli access to what we ultimately want to do all right so let's let's first deal with installing the cli so the cli is very very easy to install so just go ahead and then install aws cli version 2. go here and then install whatever whatever version you need whether it's linux mac or windows and i'm not going to go through because it's a very easy install and ultimately just to double check that you installed it you can go to your terminal and then you can do aws and then you should see a command that looks something like this so if if the command looks like this then that's great if if the command looks like hey aws not found then okay you didn't install it properly all right so then the next step is we want to give the aws cli access to our our aws console this is where we use the identity and access management so to do this what we're going to do is we're going to create a new user i actually already created a user so i'm not going to do this but just to kind of go ahead i'm just going to show you the steps of doing this so i'm going to add a user and you can call this whatever you want i'm just going to call this serverless and then we're going to give this programmatic access so there's two different types of access you can give them you can give them programmatic access or console access console access is you know you give them a password and a user and a username and a password and they're able to log into the console programmatic access is essentially something like a cli or sdk that just needs programmatic access they don't they're not a person that needs to log into the console so we're going to give a programmatic access and then what we're going to do is we're going to attach existing policies now i know this is not best practice and i know a lot of the aws nerds are going to yell at me um but this is not an iam course so we're going to give our serverless a user administrator access but really in a production application you only want to give access for what this is ultimately using so for instance you're probably just going to want to give if your user is only dealing with maybe s3 then you only want to give maybe s3 full access and even then you might want to fine tune it maybe s3 outpost read-only access or something like that but for the sake of this course just to make things very very simple i'm just going to use administrator access which means hey you have access to absolutely everything all right so go ahead and then click on next and then next and then create this user so we're going to create this user and then you should see an access key id as well as the secret so now what we have to do is we have to go to our aws cli and say aws configure so aws configure so go over here aws configure and then it's going to ask you for the access key so it's going to ask you for this access key so just copy this access key and then put it in here and then similarly i'm going to show you guys this i have this so this is really dangerous and i'm showing you guys this but i'm going to 100 delete this user uh after um after i upload this video so you guys i mean you guys can try but uh um but but if i left this user on then you guys would have pretty much administrative access of my aws account and that's that's really dangerous but for now i'm just gonna show you guys the next steps that i'm gonna do so over here you can see this and then default region i'm just going to leave it as none none and then okay so now our aws cli is actually configured with our app or with our actual aws console through this user so this is pretty much all we need to do and the thing is is that you're never going to see these credentials again so if you want to keep these credentials highly suggest just download an sv rcsv all right cool so we can go ahead and close this and you can see that we have this admin user i'm gonna go ahead and just just delete it right there right in front of you so i can 100 prove to you that hey this guy doesn't exist anymore i'm just gonna consistently use the serverless admin all right so that is setting up the cli and i'm gonna have to of course change these credentials a little bit later which is fine because it's not gonna work anymore but now what we want to do is we want to set up serverless now if we want to set up serverless and we want to go ahead and install it then let's go over here to the documentation and let's go to get started and you can see that we all we have to do is an npm install dash uh g for global and then serverless and this installs the serverless cli for us and this is going to spin up again a serverless project now there's many different ways that you can do it if you want you can do it like this with server with with mac or with windows i i mean i suggest going this route uh especially being that we're going to be creating a node application so i suggest going this route but again you can go whatever route that you decide to but once you're done that and i already installed this so uh of course you're gonna run this in your terminal so once you're done that now what you can do is you can do a serverless and in a little bit you can see you can see basically hey what kind of application do you want to make do you want to make a aws node starter application or do you want to make an aws a node rest api application or do you want to create a python application and and you can see here that it's it's just different little things that we can create you can see here we have even others and we can actually specify our own template so now that we have all that set up you can actually start creating our first serverless aws application now that we did all of the setup let's go ahead and create our serverless application now remember we're going to be following an infrastructure as code approach so we're going to be relying on this serverless framework to create the infrastructure for us we're only going to provide it with the code so let's actually go ahead and spin up a serverless project through this serverless cli that we have already installed let me quickly just zoom in here so to do this and i think i already showed you this just go to your terminal and then cd into whatever directory you want your serverless application to lie in and then in here just say serverless so just say serverless and then in a few moments you can see a few templates that it will create this serverless application for so we can have a node application it could be a starter a rest api a scheduled task yada yada or python there's many other ones there's like java ones as well as ruby but we're going to rely with node and we're going to be working with the rest api because that's essentially what we're going to be making we're going to be doing a rest api so we're going to go ahead and just say enter and then it's going to say hey what do you want to call this project i'm going to call this the default project aws node rest project sure that's works fine with me now it's going to go ahead and it's downloading that template do you want to use the serverless dashboard no that's okay and now what we can do is we can actually cd into that aws node rest api and then we can open this up inside of our uh code editor i'm using i'm going to be using visual studio code and for me to open that up i have a neat extension called code dot so once you cd to the directory you want to open you can just do code dot and it's going to open up vs code so you can see here it opens up vs code and we get this kind of neat starter project so we have a git ignore we have some handlers as well as the serverless.yaml file so let's just quickly explore this right over here so inside of the serverless.yaml file this is essentially where we're going to provide all of the configuration that is going to spin up our aws architecture it is going to be dealt with over here our infrastructure is going to be dealt with over here so you can see here that we have our providers we have you know what we're calling our service our provider in this case is aws but we can change this if we want to to um microsoft azure or or google cloud platform you know we have our runtime so what are we using for our functions we're using node.js and then over here we have some other things like lambda hash whatever another thing that i'm going to specify is the region and for me specifically i'm going to use a region that i haven't used before and that is going to be a u.s west one you can really pick whatever region that you want i'm just going to use that region so us and this should be region not regions us west i'm going to do two not one sorry so i'm going to provide that in here and then over here we have this other block of code and you can see here that this is the functions so over here we're specifying all of our functions and you can see here that we have our functions and then here we have hello and this is what we are going to call our function so this is like some sort of hello world function and then we have the handler and that's the function itself so that is the function itself and you can see that it's linked to well handler.hello so you can see here that we have our function right over here and essentially all that this function is doing is just returning a status code of 200 and then returning something in its body as well so ultimately it's just going to execute this piece of code obviously we're probably ultimately in our function is going to have some real logic going on i'm just gonna not return something right away but this is just an example and now what triggers this function so what triggers this well what triggers this function is events certain events can trigger this function and we can have multiple different events but over here we're saying that hey the event that is really going to trigger this function is an http event and this is path well this path so path nothing and the method is get so this is a get request so that is pretty much it and this is a valid serverless application now this isn't in the cloud yet so what we have to do to actually get it into the cloud is and get it into our serverless app is to do serverless deploy dash v and essentially what this is ultimately going to do is create the infrastructure for us inside of our aws application so quickly before everything deploys let's just go to lambda and you can see here that initially there should be nothing in lambda and also there's nothing in api gateway there should be nothing in api gateway as well uh well actually i guess there is something but that's from previous ago this that was very long time ago actually actually not really a long time ago those maybe a few days ago um but it's not for today so you can see here that it's still going to be creating it so let's just wait and it's creating not just um it's creating multiple different resources so cloud formation you can see here we got s3 so there's a lot of different things that we don't even have to worry about when dealing with infrastructure as code so just wait a little bit this might take some time this initial deploy will take some time because um because we're spinning up a lot of different servers you can see here that we created a lambda function called um hello lambda function you can see that it has been completed we have created api gateway and now i think if we go back over here maybe if i did a refresh you can see here that actually spinned up this so you can see the api gateway at work so essentially what's happening here let me just quickly zoom in so when you make a request right over here to slash get then what happens is that it's going to execute some sort of lambda function so it's going to execute this lambda function the hello lambda function and then that's going to go ahead and send back a response to the client so the client they're going to be making a request to this lam this api gateway which is going to delegate it to this lambda function and then that's going to send back a response to the client now let's actually go ahead and let us you can see here that this is the endpoint that has created so this is just an https endpoint so to test this out let's just go to our browser and then let's just execute this get request and you can see that we get back our data so you can see here that we have go server lists and then we have a bunch of other code and this is basically the 200 response that we get back so that's pretty much it and you can see how easy it is doing it through infrastructure as code in the next sections we're actually going to be coding out our real application before we start creating our lambda functions let's just do a little bit of cleanup inside of our directories most notably what i want to do is i want to create a folder where i can put my functions in so i want to have a file for each particular function and i want to store it inside of a folder right now basically all our functions lie in this handler.js file and i really don't like that so let's go about fixing that right now so the first thing that i want to do is i want to create a source directory so i'm going to have a source directory and in here all of our functions are going to live so let's actually go ahead and let's just copy this in right over here and let's just move it in there and just paste it in there so now what we have to do is just a few little things so now what we have to do is let's just go ahead and let's change this to an arrow function so let's do const hello is equal to this particular function over here and then in the very bottom all we're gonna do is a module dot exports we're gonna export an object where the handler is equal to that particular function so now what we can do is we can go ahead and say that the handler is the hello function and we can actually change this file to be now a little bit more descriptive now this is the hello function all right so of course now what we have to do is inside of this block of code we have to change where our handler is actually living because right now it's living inside of the source directory inside of this hello.js file so this is very very easy and simple now all we have to do is specify that we want you to go to the source directory and then inside of the hello.js file we want you to take out this handler which corresponds to the hello function so we're just going to say hello dot handler and that should pretty much be it and that should work perfectly fine and this is going to be the way that we organize our structure now one thing that you can do is inside of the source directory you could create a handler directory and put all of the functions in there and that way you can actually have other things as well and that's actually probably the best approach but since we are only going to have functions inside of our source directory i'm just going to stick them in there but again with larger applications it's actually probably better to organize your functions in a handler a directory inside of the source directory and then inside of the source directory you can have other things like tests or maybe a lib or utilities folder okay but but the the way that you would do it is very very similar exactly the way that i did it right now so let's go ahead and save this and let's just go ahead and sls deploy this again so let's just do sls deploy dot dash v and this essentially if everything works out i should deploy and we should ultimately get these same results so hopefully this doesn't take too too long it seems like it it is kind of taking a long time but it should uh speed up i do not want to pause the video so hopefully this uh this speeds up a little bit so it's uh recreating aws cloud formation which we'll talk about a little bit later and it seems like everything is fine this one was a lot faster actually because we already deployed a lot of our infrastructure already so now what we can do is you can see that this is our endpoint right over here and we should also have a slash hello or actually no we don't the the path is uh just the regular directory so now what we can do is we can basically just copy this endpoint and we can either use our browser or we can use postman i'm going to use postman and what i'm going to do actually is i'm going to go ahead and create a serverless folder so i'm going to rename this to serverless and i'm just going to create a new request and so let's call this request well this is going to be the hello request which will change a little bit later we're going to of course remove that hello all right and then this is a get request and we're just going to go ahead and make this request and it works perfectly fine and so you can see here that we get our data back so this is how we are going to be organizing our structure now the next section is let's actually go ahead and start creating our functions our to-do functions so let's start talking about the functions that we are going to be creating so these are the three functions that we are going to be creating we're going to be creating the add to do function there's responsible for adding a to do inside of our database and then we have a fetch to do so this specific this only touches one specific to do based on the to-do's id this one over here fetches all of this dues and this one over here updates the to-do's and specifically what this does is it just updates the to-do from completed to incomplete or vice versa inside of our database now the thing is is we actually need a database for this to happen because again remember how our functions work we can't really store data in here because if we do decide to store data in here eventually this is going to go away completely and plus again if we decide to store data in here another function that is responsible for fetching data has no communication to this function so we need to store our data inside of a database now there's a very popular nosql database inside of aws called dynamodb and so if you go to your aws console and then you search up dynamodb you should be able to see it right here and you should be able to see something that looks like this i'm not in the correct region let me go to the correct region where i know i don't have an instance and you should be able to see something that looks like this where you can create your own table now i you could do this through the console but i highly suggest doing it through an infrastructure as code approach and we can do that through the serverless framework so inside of this serverless.yml we can actually define outside resources that we ultimately want to provision and this doesn't have to be just dynamodb this could be s3 or this could be maybe an rds instance or something else we can all define that in here in this yaml file and we define all the outside resources as you can imagine in the resources column so this is just basic yaml and maybe i'll have a crash course on the ammo a little bit later but over here we're going to have resources and then we're going to have basically colon and then anything basically under here we're going to have a bunch of indentations symbolizing that this is going to be scoped to this resource over here this is where we're going to define all of our resources now anything below here for aws is going to be cloud formation and don't worry all too much about cloud formation uh maybe i'll cover in a different crash course but we're going to be using cloud formation to define the resources that we are going to be using inside of our aws uh application so the first thing we have to do is with cloud formation the capitalization is very important we're gonna have to also then define resources again and what we want to ultimately define is some sort of resource now this resource we're going to have to give it a name and i'm going to call this resource the to do table so need to do table now this resource right over here well we're going to have to now assign it to a specific type whether it's an s3 or it's a dynamodb or it's again rds or whatever we're going to have to assign this particular resource to a type so i'm going to say that hey this type is aws colon colon dynamo db dynamodb and then colon colon from dynamodb we want a table so that is the very very first thing and then we're going to have to define some properties for that resource so the first property and this property is going to be the properties are going to be very very different depending on the type so for diamond mode db specifically we have a property that we need to assign called table name let's just call this to do table similar to the name of our resource this does not have to be a one-to-one match this is the name of the resource this is the name of the table name all right and then we also want to define the billing mode and we're gonna say that the billing mode is pay per request so that's how we want to be billed don't worry you shouldn't be able to inquire that much costs and later on we'll talk about how we can just completely get rid of everything all right and now one thing that we have to do is for each record we have to define a an id a primary key so something that will uniquely identify each record so what we have to do is define a certain attribute we have to tell our table that this this attribute will always be our primary key and it will always exist so to do that we first have to define the attribute so we can do that through the attribute definition so attribute definitions and then over here this is going to be an array entry so you do that with the ammo through this minus sign and then we're going to say specify what the attribute name is and this is going to be id and then we also have to say for that specific attribute what the attribute type is and for this that is going to be a string so that just defines that hey we're going to have an attribute of id and string now what we have to do is define that hey this is going to be a primary key so we do that through key schema key schema and then what we're gonna say is we're gonna say okay well which attribute is the key schema well that is going to be the attribute name and the attribute name is the id so here we're just defining the attributes and here we're telling it that hey this attribute is going to be a key and then we're also going to have to define that the key type is hash and this basically ensures that hey this is going to be our id all right so that is pretty much all we have to do let me just quickly double check attributes is a very easy word to misspell as well as definitions so everything here seems fine i highly encourage that you double check as well so now what we can do is we can go ahead and save this and we can go ahead and now just do an sls deploy dash v so if we do this again what's going to happen this is going to create that table for us and actually in the meanwhile we're gonna have to do a little bit of installation so we're gonna actually have to install some new packages so let me go ahead and open up a new terminal and now what we want to do is we want to install a few packages so we're going to do an mpm install or actually before we do that let's do an npm init dash y to generate a package.json this is all assuming that we have npm installed or node installed on our local machine and then what we can do is we can do npm install and what i want to install is i want to install a uuid and i also want to install the aws sdk and the aws sdk is going to allow us to have a lot of different functions that we can use as well as dynamodb to perform some sort of action so let's go ahead and let's just make these installations and now let's go back over here see if we got any errors it doesn't seem like it everything seems fine but now you can see that we actually created a table so if we go back here and we refresh we should see a table so we see some some new uh kind of uh ui and that's because we actually created our table successfully through an infrastructure as code approach all right cool so um that's pretty much it so i'll see you guys in the next section where we're actually going to work on for real this time our application okay finally let's go ahead and let's just create our functions ultimately these functions are just going to be regular javascript so if you're familiar with rest apis this should be relatively simple but these this this those functions and those block of code are going to be executed by the functions that we eventually invoke rather than our server that will listen to a particular route so that's really the only difference so let's create the add to do function first so let's go ahead over here to our hello handler and the first thing i'm going to do is i'm just going to go ahead and just rename this to add to do so we're going to rename that to add to do so the first thing in here is we're going to go ahead and let's just rename this also to add to do and you can see that it returns something that looks a little like this so we have the status code which is 200 and then we also have our body that we json.stringify of course so let's go ahead and let's get our data from the event because once the user is making a request they're going to send the information inside of the request body and that's going to be living inside of the event dot body now for this case we're going to create a to do that has three attributes the id the to do itself when it was created and then the completed status so that's really all we're gonna do now for the user they really just have to provide the to do so whatever they wanted to do we're going to generate the id we're going to generate the created at which is going to be at the moment that the uh that the object was created at and then also the completed initially we're gonna set that to false so from the rec.body what we want is const we're gonna go ahead here from the event body we want to get back just the um just the to do itself now one thing that you note is this is going to be um stringified so to actually get access to it like a regular object you have to json.parse it now this can get annoying so what i'm going to do later on is apply some sort of middleware that will automatically parse the event body so we don't have to do this manually but for now we'll just do that all right and now let's go ahead and let's create the created act so we're going to create the created ad and that's going to be just new date and this just creates the the date at the current moment of when this variable was created and then what we're going to do is we're going to want to also create the id and that's why we installed that uuid package so now over here what we can do is we can do const and we can require something from that uuid package and uuid has different methods that we can use v1 v3 v4 these basically create different types of uuids v4 is by far the most common so what i'll just say is v4 and we can go ahead and invoke it and that's just going to create a id a uuid for us all right so that pretty much sums that up so now what we want to do is we want to save that into our dynamodb database so that is why we installed the sdk so now what we can do is we can do const aws and we can require the aws sdk and then from the aws sdk we can get access to dynamodb so we can do const dynamodb is equal to aws dynamo db dot document client and now what we can do is with dynamodb let's actually just cut this and put it right at the top because it just seems a little bit cleaner now with dynamodb what we can simply do very very easily is dynamo dynamo db so dynamodb dot put so dynamodb dot put and essentially what now what we can do is we can basically say that hey we want to put inside of the table name and this table name is the to do table we want to put a item so we want to put an item now what is that item well it's this whole object so we can basically define the whole object in here and actually what i think might be better is to just say const new to do and that's going to be equal to an object we're going to have the id in here we're also going to have the to do itself we're also going to have the created at and we're also going to have the completed status which we will initially set to false so now what we can do is we can say that this new item is the new to do now this is going to be asynchronous so we can await this if we want to so we can go ahead and await it and then over here what we can basically do is let's get rid of all of this let's actually get rid of all this right here and then essentially all we're going to do is we're inside of our body we're going to send back the new to do that we have created so we're going to save this now this is not going to work and now one reason why it's not going to work is because we have to do add to do and then also we have to change it over here to add to do and might as well also change the function to add to do and also let's say you also want to do some sort of debugging so let's just say console.log you know this is an id this is just how we can show you how we can actually view logs this is an id an id now currently this is not going to work and the reason why this is not going to work is because that lambda function that we invoke actually doesn't have permissions to use the aws dynamodb and actually put data into it so we actually have to give our functions permissions to do that so how do we go about doing that well we can do that very very easily inside of the serverless configuration so inside of provider what we can basically say is iam role statements and we can say that all of our all of our um all of our uh um functions have access we're gonna basically have an allow access and then we're gonna say that hey they are able to perform all of the dynamo db actions so i'm going to say every single thing now this is probably not the best practice for iam you probably want to say that our functions maybe only have put access or get access but for simplicity i'm just going to say absolutely everything and then what we have to say is okay well they have access to put a literally to do anything with dynamodb but we also have to specify exactly which table they have access for so to do that what we have to do is we have to say resources and then we have to have a unique identifier for that specific resource now in dynamodb that unique identifier if you go to tables you can see the table itself so you can click on the table and that unique identifier is the a-r-n so now what we can do is we can just paste the arn in there and now basically what we're saying is all of our lambda functions can do whatever action to the dynamodb to do table so that is pretty much what we are doing right now let's do an sls deploy and let's try to see that in action so let's do sls deploy dash v so let's hope that this works seems like everything is fine we did i had quite a bit so i'm hoping we didn't really break anything but it doesn't seem like we did so let's just wait so actually what we should be able to see is if we go to our lambda functions we should be able to see maybe a little bit later on a new lambda function it's still being created but we should be able to see a new lambda function that is not hello we actually deprecated this a little so we could if you want completely get rid of it but i'll keep it for now uh so we can see one thing the reason why this is so slow is because we're redeploying our whole infrastructure now if we just made a change to our function itself we don't have to actually go through this whole process there's actually a shortcut that we can use to um to basically just deploy that function and that's a lot faster of course than than uh dealing with this whole whole process as you guys can see but you can see here that actually which is really cool it actually deleted the hello function because that doesn't exist anymore so if i were to refresh this i actually shouldn't be able to see this anymore and you can see it actually got converted to the act to do function all right so that is cool one issue is i just realized that this should be a post request which is unfortunate so we should actually be changing this to post so let's actually go ahead and let's redeploy that but as this redeploys let's actually create some of the other functions so let's go ahead over here and let's now create the fetch to do's where we fetch every single to do so let's go here and let's say fetch to do's so where we fetch every single to do this one should be a relatively easy function let's actually just copy this format and we're probably not going to use the uuid we're going to call this fetch to do's now we're all going to need the dynamodb and we're not going to need any of this stuff we're not going to need any of this stuff dynamodb we are going to use but a little bit differently other than that i think that should be fine over here let's just change this to fetch to do's and then um okay it seems like this has deployed so what i'll do is i'll show you guys now that this post request is working perfectly fine and another thing that i actually noticed is that okay this is a post request but maybe we want to change the path as well and we can change the path if we want to by appending a slash over here now i'm completely fine with having just this root path because this this works out perfectly fine with me so now what we can do is we can just create a new request and we can go ahead and make a post request and uh inside of the body remember we have to provide the to do inside of the body this is a little bit too zoomed in so we have to provide the to do we have to go raw json we have to say to do let's say our to do is make a youtube video so if i were to make this request it should give us an internal server error and this is actually good that it gave us an internal server error because now will allow us to do to actually debug what's going on so it might be really hard to debug things if um with serverless because we're not really ever doing anything locally now in a typical application we could run it locally but it's actually better to have maybe a dev staging and then production environment here we're just really deploying to i guess production even though this isn't really a production application but we could also specify a dev staging or production environment but now let's actually try to investigate exactly what is going on here now typically when you're running things locally you can see the logs right away and you can see the errors that happened right away now this is kind of problematic with uh this approach because everything is inside of aws now to see the logs what we can do is we can actually use another service called cloudwatch so we can go to cloudwatch and this might not be the prettiest way of doing it but this is a pretty good way of doing it then you go to log groups and then you can see here that we have our functions and essentially what was what happened is when we created our function this was automatically configured for us so now what we can do is we can click on this and we can see that we have i just made this request today at this time which is eight o'clock eight pm and what we can do is i can click on this and i can see all of the logs so now i can click on this log and then click on this log and i see here okay we have some sort of error and it says type error so self.configure is not a function so i'm not sure actually why that's happening so let's let's open up some other logs over here but it says here again yes self.configure is not a function and that is the error that is happening so let's let me see if i can quickly diagnose it right now if not i'll pause the video and figure out exactly why that's happening because we don't really have a self dot configure here i'll just pause the video and i'll come back with a solution so i figured out what the error was it was a very simple error over here in the dynamodb i should have put the new keyword to instantiate dyno mode tb because we have to instantiate this particular object so just add new in here and what you can do now actually is instead of doing sls deploy dash v because we only changed this particular function we could do something like sls deploy dash f and specify the new function that we want to redeploy and this essentially is a lot better because it is a lot faster so this is really really cool and now if you go to postman and you make that same request you should be seeing this id right over here and now what we can do actually is we can go to cloudwatch and notice this console.log that i put in here this is just in case we need to console.log to debug things so now what we can do is we can see here that we have another log group and we can basically open these up and you can see here that hey this is the uuid so if we ever need to to debug in console.log things we can just go to cloudwatch and do that so now let's go to dynamodb where we actually inserted our uh our uh record so let's go to tables let's go to the to-do table and then let's go to items and you can see that we don't have any items for some reason let's go over here i but i would have thought we added an item in there i guess we didn't um that's interesting that's another thing that we are probably gonna have to debug am i in the right uh i am in the right what i'll do is let's actually create the uh fetch to do and then let's see if that results in the items because sometimes uh this is actually slow for it to appear but you know i do i would have expected it but i guess it didn't happen but let let's let's create this and then if this returns back an empty array then we know that we have something wrong over here so the first thing that we're going to do is we're going to have the new in there because that's definitely important and let's actually say that hey let to do's be we're just going to declare the to-do's in here and then what we're going to do is we're going to say const results is equal to a weight and then we're going to say dynamo db and we're going to do e dot scan to basically get all the records inside of the particular table that we're specifying so we're going to say here table name and then we're going to say well this is going to be the to do table and ultimately this is going to return a promise so this is going to return a promise now this could fail so what we are going to do is just put this in a try catch block so we're going to put it in a try cash block if it is successful then what we're going to say is we're going to say to do's is equal to results dot items which is basically every single item if it's wrong then what we'll do is we'll just console.log the error so we can view the error in cloudwatch now what we also ultimately also would want to do is return some sort of error to the server but i'll leave that up to you guys later so now what we can do in here is just say that hey we want to send back the to do's so that is our function very very very simple so now inside of the server list what we have to do is create a new uh function so let's actually go ahead and let's copy this down now let's paste it in there and we're going to call this function fetch to do's we're going to say that this is in the fetch to do's dot handler and let's actually for just splice it up and let's just say slash to do's and over here this is going to be a get request so let's go ahead and this time we have to do sls deploy dash v and deploy our whole infrastructure and the reason for this is because we have manipulated the serverless.yaml file so let's uh go ahead and do that hopefully this doesn't take too too long i keep saying that but it always takes the same amount of time so while that's happening let me i'm so bothered that the data wasn't actually saved let me quickly do a refresh i'm sure the data wasn't saved yeah it's not saved i'm not really sure why but we'll debug it don't worry we will debug it for sure uh so let's actually see as this is deploying good time to kind of debug so we have over here we're putting this item in there hmm that's interesting because it seems like this should theoretically work uh and we'll be able to test it out exactly why it doesn't work uh when we actually have this get function which at this point we do i i think i think the reason why it's not working is we actually have to send back a promise even though i think this is part of the response uh i'll have to i will double check now let's let's go ahead and let's actually redeploy this function specifically and so this should be a very quick redeploy because we're only redeploying the function so it shouldn't take as long and then what i'm going to do is let's go ahead and let's get this right over here this url and let's create a new request and this is a get request and we're just going to go ahead and make this request so you can see that this actually returns back an empty array which is which is great because that's exactly what our database is holding right now now we so we know that this route works but we know that this route doesn't work so uh hopefully maybe that fixed it the promise fixed it so now let's go ahead and let's make this request again and it actually was that so you can see here that it works perfectly fine and now if i were to refresh i should be able to see the record in there and i do which is cool you can see here my id false and then created that that seems to be an empty object which is kind of strange uh but you know what whatever i'm just gonna leave it like that i don't really care and then we have our uh make a youtube video the reason why it's probably a object is because we have new data over here i don't know if we have to do like some two iso string maybe that will fix it up and maybe we'll go ahead and just well we'll redeploy it with the other one so now let's actually create another one and this is going to be the fetch to do function so fetch to do.js it's going to be very very similar to the fetch to do so i'm just going to go ahead and just copy this except here we're only going to be fetching one specific i one specific to do based on the id that was provided in the parameter let me just show you exactly what i mean by that so over here we have here getting all the to do's but this route is going to be a little bit different it's going to be slash to do and then slash the id of that to do and that should only get us one to do instead of every single to do so over here it gives us an array of to-do's this route should give us just that one object so let's go about doing that right now oops let's just move that back to the to do's okay so now what we want to do is we want to create this and i think it might be worthwhile to just create the function over here in the handler so let's go over here we're going to say that this is the um fetch to do function and this is going to go over here to the fetch to do and this over here is going to be to do and then a dynamic id and to define that in serverless we do these curly braces and then id okay so that is how we define it here and there and then now what we can basically do is we'll use dynamodb to extract everything so that that dynamic id is going to be living inside of the event dot path params so what we can basically do is we can say okay the id is equal to event dot path for ram so we can basically destructure that id out from the event dot path params and then what we can basically do is we can have another to try catch block but instead this time it's going to be dynamodb dot get so this is going to be a get request because we're getting basically for a specific a specific key so we're going to say that the key is the id so we only want to get one item where this is the key so we can even space this out just to make it look a little bit better and now what we can do is we can say that the result is going to be results or maybe this is more promptly named result result and then this is going to be result.item because we're only getting one item back and over here we can probably change this also to to do to do okay we can change that back to over here to to do and over here we can change this to fetch to do and over here similarly fetch to do so i think that's it for that um i don't think anything is wrong with that so let's go ahead and save it and let's redeploy our whole infrastructure so sls dot uh v and by the way uh if you're confused by sls this is exactly the same as serverless i probably should have mentioned that but it's just a shortcut because you know typing serverless can get pretty annoying now in the meanwhile let's create our absolute last function and our last function is the update to do so let's go ahead and let's create a new file we're going to call this update to do dot js and we're going to go ahead and just copy the add to do because it's going to be relatively similar let's just change this to update to do and over here to update to do and then everything else we're probably going to be getting rid of let's let's uh let's actually create the server list for this first so let's go over here and this is going to be also a little bit interesting or it's going to be kind of a combination of this post request as well as this get request because we want to update a specific to do based on the id so we're going to have this exact same path with the id but we're also going to be injecting essentially what we want to update inside of the post request but now that that's done let's actually just test out the get so to test out the get what we can do is we can basically get this id and we can just do slash id and it should work and see it doesn't give us an array of objects it gives us only that specific object so it works perfectly fine so the last thing that we have to do is just create that update so so the handler is going to be updates to do and then over here updates to do and i think that is pretty much it we can also change the uh the request to a put request or a patch request whatever ultimately you think is best and now now let's actually go ahead and let's create this uh to do so the new to do we can probably get rid of the console.log was only for for for our sake now in terms of the event.body we want to get the completed status so we want to get the completed status from our client and so whether this is true or false so that's really the only thing that we want to update but we also want to get the id and we get the id from the per the the param so remember inside of the fetch to do if actually specific to do this is how we get our id we can get it in this very similar way in the serverless function all right so that is pretty much it and now the last thing is we have to go ahead and update it so instead of put we're going to say update we're going to say that we're going to update this particular table and we're going to provide a key and this is going to identify exactly what object we want to update so over here we have the key and then over here we're going to have another field called update expression and then over here this might be a little bit funny syntax but but it might make sense at the end of the day we're going to say that hey we want to update completed we're going to set completed to complete it colon completed and essentially what we have to do is we actually have to inject this completed value in here so now we can say if we want to set the attribute completed to this completed value that we basically injected from here to actually inject it what we would have to say is expression expression attribute values and we can basically say that hey the colon completed is equal to the completed object right over here so that's basically this is basically saying set completed equal to this we're just mapping this to that so that is pretty much all we have to do and then lastly we have to define what we ultimately want to return because we can update a bunch of things we're going to say that hey we want to update or we want to return all all objects that have been modified so all new objects all right so ultimately we want to return the promise so that is pretty pretty good and then for the stringify well we updated it so what i'm going to say is just provide a message telling us hey this to do updated we could return the actual to do but i'm just i don't i don't yeah it's fine i don't i don't want to do that all right cool so that is pretty much it so now let's go ahead and let us do an sls deploy this might take some time now in the meanwhile let me actually tell you what we're going to be doing next you can see here that this json.parse is pretty annoying and this is uh same over here so what we can do is we can actually add the middleware so if you don't know what middleware is it's basically something that is invoked before the actual execution of our function so it might do is it might do some sort of processing middleware are actually very important for authentication so they're often sometimes run for uh determining if the user is logged in and they have authorization to this particular handler so what we can do is we can actually apply a middleware that parses the json for us beforehand so we don't have to do it manually and so that is what we are going to be doing next after we test out this function so let's go ahead and it seems like everything is fine so what we can do now is we have a put request so we have this put request so let's go ahead and let's actually make this call let's add another let's add another request and we're going to make this put a request and what we're going to do is inside of the body we're going to say raw and then json and we're going to say that the completed status is true and we're going to say that the completed status is true for this object right over here so let's get rid of that let's paste that in there and now we're going to say that this is a put request right over here and so now we can go ahead and send this off it says updated to do and we can actually test this by making this request again and it's getting rid of that let's make this request oh yeah of course we have to make this request again with that particular id that's why we're missing that token so we can make this request and now you can see that it was updated to true if i were to change this to false now what we can do is make that exact same request and you can see that that works perfectly fine so that right there is lambda functions i hope that makes a lot of sense now let's just apply middleware and then we'll be done with this project okay so the last thing that we want to do is apply middleware and for serverless framework there's actually a really great package that we can install that will allow us to use middleware very very easily and that's called midi so what we can do is we can do a mpm install and then this is going to be at midi and then slash core now within the midi uh package there's many other packages that we can install that are middlewares that we can use for instance we can use at midi and then this middleware is http json body parser and you can imagine what this middleware is going to be doing so let's go ahead and let's install this and if you don't know what it's doing well it's what it's going to do is actually parse our json body now sorry apologies it's midi not with um without the l so let's get rid of that i've alwa i always make that mistake so let's go ahead and now let's actually use that midi middleware so inside of the add to do what we're gonna do is we are going to do const and midi and then we're gonna do we're gonna require in midi from act midi core and then we're also going to get const http json body parser so we can do http json body parser and we can do require and we can do at midi json body parser so simply to use the middleware what we will do is basically wrap this handler in the midi method so we can say midi and then we can start appending whatever middleware that we want to use so we can say that hey we want to use the http body parser so now what we can basically do is let's get rid of this console.log you can basically get rid of this json.parse and that's because we already parsed it inside of the middleware so if i were to do a sls deploy dash v and i'm doing a sls deploy dash v because um we i guess we would change the function but we also changed the package.json and the node modules i really don't know if that matters or not but it could matter so i'm just going to deploy the whole thing just in case now you can do the exact same thing with the update as well so we can basically say let's actually just copy this and then in here you can say midi and then over here we can get rid of the parse and then over here we can just simply wrap this in midi and we can say dot use and we can say http json body parser so let's actually go ahead and let's add another to do and this time we're not parsing the body so we should keep that in mind and let's say um walk the fish so let's go ahead and it works perfectly fine and that's because of the middleware and we can actually append any of our custom middlewares as well if we want to so the middleware as you can see this is kind of this is the interface of the middleware but you can have a custom function and you can basically have the dot used in there if you want so you can really do whatever it is that you want so that pretty much sums it up i hope you enjoyed the crash course and learned a lot about serverless
Info
Channel: Laith Harb
Views: 4,423
Rating: 5 out of 5
Keywords:
Id: woqLi6NEW58
Channel Id: undefined
Length: 89min 11sec (5351 seconds)
Published: Wed Jun 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.