Getting Started with Serverless Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi there folks thanks so much for joining me today for the webinar on getting started with serverless the plan for today's session is to go very quickly through what serverless means in a nutshell and then do a very quick demo of how you can get started building your own your first serverless rest api with a couple of endpoints just so you can see how it works and then that will give you the beginning points to you know continue from there so who am i my name is gareth makomsky i'm a developer advocate at serverless inc the creators of the serverless framework and i've been a web developer since about the early 2000s building stuff for the web and in 2016 i discovered the world of serverless and have been down that road ever since and ultimately joined serverless inc back in 2019 to help them with customers help users and so on uh build their service applications and kind of start working out the best practices for folks so i wanted to start off the discussion today with what is serverless and this is just a quick rundown in case there might be folks here today who have heard this word syllabus but don't quite understand how it fits in and what it can do for you so let's just take this uh from the viewpoint of the web application as you might build it today and you've got a web application with many users potentially who want to access your application on the web and traditionally you would then have the the browsers will hit first a load balancer so you have some mechanism to allow load to be balanced whether you use uh alb and aws or your own homegrown load balancer what this load balance is going to do is spread the number of requests across multiple virtual machines traditionally and each of these virtual machines can now process based on the http request that came in from the web from the web this is also fine this also works in the kubernetes world remember kubernetes pods still have to actually run on virtual machines so it basically ends up being the same thing and then your application in the virtual machines has to store data in some way so that's normally done through a database of some type and traditionally this will also be backed up so you'll have two of these machines uh sitting there for your database one is a backup in case the the master or main database goes down but this this structure is kind of cumbersome and time consuming because you may have got all of this infrastructure set up but you haven't even started building an application into actually serving anybody you spend all this time and effort putting vms together databases together installing software and so on so serverless helps in this case because ultimately you're deciding on your infrastructure as you are building your application it's not about building your application and then somebody sets up the infrastructure gets hosted on so what does that look like well let's just replace what we've got here with a serverless equivalent and we've still got our users over here interacting with our with our website but instead of a load balancer that we've had to set up configure and maintain over time we can use a managed service for example api gateway in aws that allows us to receive all of this incoming traffic and api gateway exposes http endpoints for our users and this is really useful this is not something that we have to ever worry about or manage or maintain api gateway is a service and it just provides us enormous amounts of capacity to handle lots of traffic and we don't have to ever manage it ultimately though you need that request to go somewhere to get processed so instead of a cluster of virtual machines with cpus that can then execute our code we use a service like lambda aws lambda and lambda can receive all the http requests from the api gateway side and ultimately process each of those in parallel which is pretty handy again this means i don't need to worry about application software operating systems or anything like that and then of course i need a database still so we can look at a service like dynamodb that's available in aws and dynamodb is another managed service i don't have to set up this database i don't have to worry about scaling it clustering it anything like that dynamodb is just there to be used you send it data it stores it you ask it for data it gives it back to you easy as that there's no management layer involved in dynamodb at all but the service framework is there to help you do all of this because one of the downsides of all of this is that you have all these disparate little services that you might need to then configure yourself going to the console or the api and patch these things together and configure them properly in the permissions and i am and all that all that stuff that can be a tricky to make sure you get set up and there's some downsides to that but the service framework helps you tie all this together nice and neatly and easily what it does is it gives you a single configuration file as you'll see that helps you describe how you put all these pieces together in relation to an application you're building not as a set of discrete pieces necessarily this also lets you then very easily connect these managed services that you that we're using to uh for our application to the code that we're actually writing that ultimately gets executed in lambda it also means that because i have the single single configuration file i can now share this very easily with my team i can hand this this yaml file out and everybody can use that and ultimately everybody can then reliably redeploy this same project in their own aws account if they want to to play around and test with or in multiple different environments it makes it very easy to reliably redeploy things as needed and then in the long term this then allows you to scale out and build really large scale applications that can serve millions of users as lots of serverless framework users have done because of the scalability of the services used in aws you don't have to worry about managing that scale and because the because you have a way to manage your application and build it reliably you also can then make sure you're building this application to scale for millions of users so pretty useful in that way but that's enough for the intro on on service framework and server's development i want to actually get into the demo so let's go ahead and jump right into that so first of all i'm going to come into my cli here my terminal and what i'm going to do is i'm going to get started as you can see i'm getting started and i want to spin up a very quick uh template i'm just going to use the serverless command to do that and then let me choose a template to start with i'm going to choose http api as a way to get started i'm going to give this a name that's my api this is now going to download this template for me and there we go now it's asking me i am currently logged into the dashboard the dashboard is an optional tool that you can add on top of your serverless framework development and there's a lot of really great features that can make life a lot easier for you as a developer and ultimately when you're in production as well and because i'm automatically connected it's asking which organization i want to associate my new service with so i'm just going to choose the only one there and it's also then going to create an app which is essentially like a folder using the same name as my service which i can change later if i wanted to it's if also want to deploy now and that's that's one of the nice things i already have a fully functional uh serverless framework service i can deploy into my aws account so i'm just going to say yes to that to get us started while we wait for that i'm also just going to go into our new project and let's take a look at the code all right so we're in vs code here and i'm going to take a look at some of these files that we've got waiting for me so the server is the java file here this is the single configuration file that i was talking about and here i can see i've got i was talking about the organ app this is where my organ app settings are added into my servers the demo file here's the name of the service that i gave it i'm going to be writing my lambda functions using node 14. and this section here is where i'm now describing the relationship between my code and how they get triggered so i'm going to have a file called handler and you can see there's a radio a file called handler here the function inside inside called hello if i open that you can see there's the hello function right there we're not actually going to be using this file i'm going to be replacing this in a minute so what we want to do is let's build our own and i'm going to build a customer api let's just assume i'm i need a way to store customers and retrieve customers so let's go ahead and do that so first thing i want to do is i'm going to change this function definition here and i'm going to first we're going to end up creating our customer because we can't retrieve anything if we haven't created it first so let's go ahead and do that and just for time i'm going to just copy and paste something here just so we can get this in quickly the only thing i've changed is the file name is now going to create customer.js and the function is also going to be create customer and my method is going to be a post because with a rest api if i'm creating i'm using a post um and if i'm going to be creating a user i also need a way to store data for my customers sorry not users customers i need a way to store the data for my customers so what the service framework allows me to do is use configuration details uh that aws allows uh called cloudformation and what this means is i can describe my my my dynamodb table that i want to create as a part of the service that i'm building so here i've said i'm going to create some resources the resource name is called customer table the type of the of the resource i'm creating is a dynamodb table because dynamodb is a nosql database i don't need to define the entire schema of the table i can just define my access points how i'm going to retrieve data so here i'm just defining a single primary key and that's all i'm doing and then giving my table a name now i could have left the name out uh the syllabus framework would have automatically generated a name for me but i've decided to hard code my i'll put my own name in here all right now that i've got a database to store this with i now need to provide the lambda function the ability to retrieve the name of the table when we're running our code so the way to do that is to use environment variables and the service framework allows me to very easily add in a couple of lines yeah so ultimately what i'm saying is here i want is there's a section of environment variables and i want to create one called dynamodb customer table and i want this dynamodb customer table to be equal to this value because he's the same as the one down there and this means that now when my lambda function executes i can retrieve that environment variable uh in my code as we'll see in a minute last but not least i also need to set up my permissions and one of the nice things about serverless development is that by default using aws your permissions are your lambda functions have no permission to access anything in your aws account and you need to explicitly go and add those permissions it seems a little annoying at first that that it happens but there is a reason for that because you don't necessarily want this code to be executed that can have full access to everything in your entire aws account so here i'm giving my lambda functions permission to access certain parts of my aws account in this case dynamodb and this is the specific dynamodb table that it's accessing the exact same table as we have here for example all right that means we're pretty much set on the serverless.java page but i also still need to add create my file for create customer so let's go and add a new file i'm going to call this create customer.js and again i'm going to paste some stuff done earlier just to make things a little bit quicker and all i've done here is i'm saying let's let's let's export the create customer function here is the sort of format that lambda functions are defined in using an event parameter here and this code essentially just reads data from the body of the post request then sets up my dynamodb uh database connection with the uh specific data that i want to put into the table here is where i'm using the environment variable to get the the table name then i'm using my dynamodb object to put that data into dynamodb and returning it 201 fantastic now that's pretty much done the only thing that's missing right now is i've got here this const to require the aws sdk npm module now i don't have that installed at the moment so what i'm going to do is do an npmi [Music] save aws sdk we need to install the aws sdk and that's now done so we should be good there as well and now with all of this done i've got my configuration done i've got my create customer table the function then i'm just going to do an sls deploy and now this is going to deploy the changes i've made into my syllabus framework service that i'm deploying and while it does that i'm going to go switch to the dashboard so we can take a quick look around there to get to the dashboard i'm going to go app.serverless.com you can see here is my service that i've added my my api has the dev instance that we deployed earlier when we got started and i wanted to point some things out you may have noticed that by aws connection i was able to deploy to aws without a problem typically what you'll end up doing is using the dashboard you can go to the org section and set up a provider here to connect to aws so i could for example add a provider to connect to my aws account click connect aws provider it's going to open up my aws account and once i'm happy with these details there's nothing that really needs to be changed here i just need to confirm the box at the bottom to create stack and then just wait for this to complete as this stack is creating i'm just going to refresh here to keep an eye on it and when this has finished creating it's going to end up um updating automatically in the provider section for me let's just wait for that to finish let's just check on our deployment we can see our deployment is busy going on here actually might be finished shortly all right so that setup is complete let's go back to providers there we go our provider has been added so we could use this connection to aws um instead of the local one on my local machine and this is really useful to manage all my aws connections together all right we've got our api endpoint is now ready so let's just do a quick test of that i'm going to make a quick curl on it and want to grab this and i need to add the url on the end which is going to be this one really what i'm doing is i'm using curl to push data at it using post also no error returned so that means that everything worked as intended now i want to be able to retrieve this data so let's do a quick get customer i'm going to create a get customer file here close the cli and let's create some code to retrieve our retrieve our data from dynamodb and again this is relatively simple here i'm setting up a an object to scan the database my dynamic db table again i'm telling aws to create my dynamite to be dynamodb object i'm using the dynamodb object to scan my database using these parameters that i've created here yeah we're just checking if we did actually have some data if not we're going to return a 404 status and if we did we're going to return to 200 and return the data as you can see here so i'm going to save that file but we also need to just edit our server as the demo file here to update this so let's go add the get customers function and i'm going to [Music] just quickly grab here and really this here's the file name here's the function name and i just realized i need to rename this let's just get customers and there's the function name get customers and this is a get request so now with that saved and with get customers saved we can once again do a deploy and while that completes one of the things that i want to just test out right now is if i come [Music] to all right all right so we sent this code request before and i want to let's just say i remove name from there so i have something that's missing that is required this should throw an area we can see their internal server error has been thrown i just wanted to confirm that that was the same one so if i go one of the things is now i had that error but i want to actually go check what that what happened with that error so coming back to the dashboard the dashboard is more than just managing the aws account if i go to the dev instance here this allows me to actually look at the requests that have happened on this api actually i've done a bit of testing in the past year what this is going to do is i can now see immediately that i have some errors that i could take a look at here so i'm going to filter this by 15 minutes you can see here's this area that i just created so now i'm going to take a look at that error i'm going to click through to there he has that error in create customer and if i go back to that's the one i wanted [Music] there we go and i scroll through and i can see oh yeah he has one or more parameter values we missed so my primary key just was missing at that point so there we go and if i wanted to i can go see the entire log that was generated with the entire exception tell me debug that so the dashboard actually becomes a very useful tool for debugging when you're busy building applications but also becomes really useful in production when you're neat when you need a more site like seven days worth of data you can go back and see what the traffic looks like what errors look like and so on really useful uh feature to add to your serverless function all right so i've got my create my get customer uh function created so let's go ahead and test that out and that should be as easy as pearl whoops girl finish this url let's grab the url again oh and we've got an error there i know what the problem is i did not notice but i actually uh where is it yeah this is the the environment variable is incorrect so let's just fix that right up and now i just want to redeploy that one function so i'm using dash f to save that function and i just want to confirm let's get customers it is so i can deploy just that function because i've just made a code change to the function not the server.java file so i'm just going to deploy the word function give it a couple of seconds let it deploy that there we go let's test that one more time there we go we've now retrieved the data that are stored in the database and so that is a very basic rundown with some debugging some live debugging as it happened which is pretty handy to see and with the dashboard available to me if i wanted to go and investigate these details a bit more so thank you so much for the time today and we're going to i'm going to look at answering some questions now in a minute if you do if your question is not answered or if you think of something later please feel free to get in contact at gareth at serverless.com you can even hop on to serverless.com where there's a whole community of folks using serverless that might be able to answer any questions you have so yeah thanks so much let's go look at some questions
Info
Channel: Serverless
Views: 27,682
Rating: undefined out of 5
Keywords:
Id: KQRGM9_eqIw
Channel Id: undefined
Length: 20min 52sec (1252 seconds)
Published: Tue Mar 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.