Getting started with AWS Lambda and Serverless Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone i'm thomas and welcome to my channel where i educate people on iot and web development by showing how to build projects step by step today you're gonna learn how to create and deploy an aws lambda function and how to put it behind aws api gateway http endpoint all of this is going to be simple and easy thanks to the serverless framework that i'm planning to use before we get started please make sure you subscribe to my channel so there will be more content like this in the future and now let me show you how to deploy that function okay right so here is my browser opened i've got two tabs in here the first step is aws.amazon.com i'm gonna i'm gonna go back to this in a few seconds the second one is the blog post on how to create aws lambda function how to create and deploy it using serverless framework the subject of this video the reason i opened this blog post is basically because we've got a few requirements that you have to meet in order to do all of these operations and these requirements are you need aws account aws admin im user aws cli installed and configured on your machine and finally latest node.js also installed on your local machine okay and most of these steps i am going to cover on this video and that is going to be creation of aws admin imuse installing and configuring aws cli and just a quick one on the node.js however aws account is something that i'm not really gonna cover i'm gonna assume that you've already have it created but if you don't don't worry have a look at the description of the video you will find these two links in there the first one is aws official article on how to do it just tutorial how to create an account pretty straightforward you just need to provide your details your debit or credit card information and this is this is really it don't worry about the the the the card the credit card because they're not really gonna charge you uh you will have access to most of aws products for free for one year um but you can find everything in this article in case you have some issues with creating the account from that article you can have a look at the second url and that points to official video youtube video on how to create account that's also from aws from the official channel so um they can they will take you step by step how to do it okay so yeah as i said i'm gonna assume you have this done already right and the rest of the steps we're gonna cover i'm gonna cover on this video and that's what that's something i'm gonna do just now right so we're going to go back to aws.amazon.com and i'm going to go to my account aws management console and from here first of all i'm going to log in as a root user right so that's gonna be the user that you've created during a registration process okay so i've got my email provided here that's something you need to do as well then click on next you need to solve the captcha click on submit click on submit then it to provide your password sign in and there we go aws management console i'm logged in with the root email account and what i would like to do just now is to type in i am in the search box click on i am this is to create our admin im user then menu on the left we want to click on users and then add user button the username in the form here should be something like administrator or admin but it's entirely up to you then access type you would like to check programmatic access and also aws management console access for the console password we would like to pick a custom password and because i just you know checked that option i'm gonna have to provide it right now and that's what i'm gonna do it is quite important to remember that password because you're gonna have to use it to login to aws management console this dashboard as an im user right so remember this password require password reset we want to uncheck this and then click on next permissions right in the next view you probably won't see anything no groups just create group button that's the one you want to click on for the group name you want to put something like admins or administrators and from the policies there is only one policy that you need to check which is administrator access right so just click on the check box and then create group button right that's gonna auto check auto check it here in this view right in the add user to group view so the only thing that you can do right now is to click on next tax there is no tax to be added so next review and then create user that's going to create a new user right and here is the success screen right from the screen there are a few very important values the first one is that url right note it down save it somewhere because you're gonna need to to access aws console right this dashboard with im user im admin user credentials there is gonna be a user admin right in my case if you call the user some something else should be here so that's gonna be your username and password is gonna be the password that you provided in a custom password field so that's that's one thing to note down and save the second thing to note though not note down and save is access key id and secret access key these are needed for aws cli for the serverless framework to be able to talk to abe to aws to deploy the code to aws and the easiest way is to just download csv file it's gonna contain all the credentials that you're gonna need in the awcli configure process um however if you don't want to download it you can just copy access key id and then click on show in here and just copy secret access key but here make sure you do it right now because you won't see this screen anymore and you won't be able to access this data from the console anymore either right the reason is is because of security but yeah don't worry you can regenerate them but still um it's it's a good idea to download it as a csv anyway i've just done it so i'm just gonna close it and then i'm gonna sign out from management console because i'm not gonna be using it today anymore um that is because serverless framework just uses programmatic access okay cool okay so that's done now the next step right so that is covered aws cli so aws cli i'm just gonna type in in google aws cli install and then click on the first link which is installing updating and uninstalling the aws cli cool okay and what we would like to click on is aws cli version 2 this is the one that we want to install and now we have a choice right we've got docker linux mac os or windows and um this is this really depends on your operating system right if you work on mac like me you want to click on mac os i'm gonna cover the others don't worry and what you want to click on is install and update the aws cli version 2 using the mac os user interface right that's the simplest way you can do and what you're gonna need to do is to just click on that link download the package file double click on it and go through the installation process not something i'm gonna do here on this video because it's pretty straightforward it's just you know about clicking next next next agree and and that's it right so just download it and uh and double click and actually for windows this is very similar because it's it's also about downloading msc file this time double clicking going through installation process for linux you have to make sure you have a zip installed and this and cal right so cad and and zip are two requirements and this is what you need to type in in your console you can you can essentially use the copy button in here and then just paste it in your terminal on linux to install aws console cool okay so um yeah so now i'm assuming you've done that the setup was successful and what you can do right now is to confirm it with terminal or windows command line right i'm going to show you how to do it on mac but on windows is pretty similar right so i'm going to open my terminal okay make sure it's a new window it is important and what you want to type here is aws minus minus version right if you work on windows use the keyboard windows plus r buttons and then type in cmd enter you should see something similar and then try to type aws space minus minus version if you get something similar to what i've got that means that aws cli has been installed successfully on your computer right like in my case right but yeah i've got it already installed cool okay so we've got this installed but now we have to configure it to configure it you just need to type in aws configure and this is the moment where you need aws access key id and the secret key right i've got them downloaded as a csv file and yeah there are two ways you can open it if you have excel just open it with access it's gonna be easy because you will see columns or you can just do it with the text edit like i'm just gonna do and here what we can see is the columns comma separated columns and then the values right they're going to be different to you uh i've just put x's everywhere this is for the security reasons because these credentials you are not supposed to share with anyone remember that otherwise someone can um for example use these credentials to create a bitcoin mining infrastructure in aws and then you will be charged a lot of money so yeah don't share it with anyone cool okay um what we're going to do now is to locate access key id okay so but by just looking at the columns here the first one is username that's admin then we have password in this case it's empty that's why there is a comma and another comma here and then there is a access key id this is the one that starts with a and that's something that we want to copy and paste here okay i just did a ctrl c ctrl v and then for the secret access key is right next after the access key id so it's gonna be the second value it should have like lower upper case and numbers so i'm gonna paste it in and then press enter and now we've got the default region name for me this is eu west one because i'm based in europe however if you are based somewhere else like in us um you probably want to have a different value in here have a look at the description of the video there is a link to all regions that aws provides you can pick the one that is closest to your location or the location of your clients or customers right essentially the the the consumers of your api right because e west one is the one i would like to use i don't need to provide here anything i can just press enter and the default output format i'm just gonna keep it as none so i'm gonna press enter again and that's it your aws cli is now configured and it should work to make sure the cli is configured properly you can just quickly type in aws s3 ls and if you don't see any errors right it's probably gonna be empty list or something like that or just nothing but if you don't see any errors that means everything went successful cool okay so another step is done let me just go back to to the list of requirements so yeah we've got this covered as well as this one and the last one is node.js okay so for the node.js you just need to follow the link another link from the description of the video and here this is gonna take you to the official node.js page and you just want to pick one of the version can be either of those and just download the either package file or msi file or for linux that there are gonna be instructions and uh just follow all the installation steps to have your node installed right this is important to be able to install serverless framework with npm cool okay so yeah i'm assuming uh you've done this and now the next step which is the last requirement not on that list because uh the next step was how to install serverless globally and that's something we're gonna do just now right so i'm gonna minimize this window and in here i'm just going to type npm install minus g serverless okay when i press enter that's gonna install servers globally as a node module and and yeah this this is gonna be essentially it for the prerequisites okay um in case you have issues with that you may get a you you may see an error or a permission error i'm just add a sudo in front of npm right so before npm just add sudo sudo space npm install minus g serverless and that is gonna that should actually fix the the permission issue okay installed right so now that i have got that done um i'm gonna have to restart my terminal again right there is a new command serverless that i can use but not in this window probably so um ideally that's that's also what you would like to do so you just want to close the terminal and open it again okay and that's what i've done uh just to make sure the serverless is there i think we can do just several as minus minus version and that should give us some information yes so that means the command is there the installation was successful right if it's something wrong that means you probably had an error with the installation of the of the serverless which most likely was the permission error so you probably want to run a pseudo npm install minus g serverless but yeah okay it's all working here and um now i'm going to go to my directory where i keep all of my projects all of my programming projects i've got in the projects directory which is actually in my home directory as well so it's home directory slash projects it's the same one right so i went there and now i'm gonna create a new serverless framework project using serverless commands and this is important to not create the folder yourself right this is going to create the folder with the project that's what i mean right that creates the project the the folder for you don't create it on your own so to do that we're gonna type serverless create minus minus template aws nodejs then path and here you can give the name to your project i'm gonna call it my lambda function that's also gonna be the name of the folder this command is going to create and template is essentially the serverless template for aws node.js project so there's going to be some example files serverless yaml and there's going to be a handler.js as well okay so that's what happened just now we've got that project created if i do cd my lambda function write and do ls you will see you see i've got handler js and serverless yaml but yeah it's not going to be really convenient to edit this first from my from my command line so i'm going to use visual studio code to open that folder so yeah let me just maximize the window and i'm going to go to open projects and there should be my lambda function over here right so i'm going to open the whole folder right and there we go there we go so let me just quickly go through it there is not many files in this in this folder we've got npm ignore this is to ignore node modules which is not here that is not existing here and we're not going to create it today because our code is going to be super simple we're just going to keep what it is in handler.js with a small modification um but we're going to start configuring our lambda from the serverless yaml file okay so yeah what is the serverless yaml files this is where you keep all the infrastructure configuration about aws services that your lambda needs to use right so things like api gateway things like authorizers things like permissions to order aws resources right things like you know resol permissions to s3 permissions to dynamodb and other aws projects are all here are in this single serverless.yaml file right yeah let me just quickly go through it so what we've got um first is the service name right which is my lambda function exactly same name i gave at the beginning like when i when i created it with the command then we have a framework version we're not going to change that a provider which is aws runtime is node.js 12 point x and there is also a lambda hushing version here this is probably also something has to do with the runtime version and a bunch of code that is commented out this one is about the stage in the region region here is quite important to us especially to me because i'm not based in u.s east but europe eu west one again i refer you to the region list link in the description of the video so what i've just done is uncommented the region just make sure this one is the same level as other properties of the provider and i'm going to change us east one which is a default value so if you want to you will you are based close to the u.s east one you just want to keep it as it is i mean comment it out right i uncommented it because i want to change this to uh eu west one right so there's the region i am in right closes in it's also the cheaper one in my area right next commented out code is about the roles we don't care about the roles today it's going to be another video about those environment variables again another video another day package i'm just gonna remove and here we are the functions here are the functions definition what is in at the moment what is in here at the moment is the hello function with the handler handle.hello right hello is the name of the function if you want to change it you can do it that's just going to be the name of your lambda function that is going to be created in aws on aws sorry then the handler this is essentially pointing to the first part of that is pointing to the file to the javascript file js file right so handler points to handler.js if you rename handler js to something else and you keep it like that it's not gonna work right if you rename that something else you have to also rename that file to something else right to the same thing that you put here then hello is the name of the exported function so if you go and open handler.js you will see there is module.exports.hello right so that points to that hello that hello needs to match that hello if you rename that not gonna work if you rename that you also have to rename this one in here okay so very important and um then there is again something commented out and part of it is important because um there is something we want to use which is event section on the on the hello function so you've got handler handler hello and you would like to use events there's going to be just a single event and that event is going to be called http this is to create an endpoint behind api gateway and that http has two properties i mean it can have more but today we're just gonna use two the first one is path and that is essentially the endpoint itself the path to the endpoint itself right so if we do hello we will have something like https some unique api gateway url that is going to be generated once we deploy the lambda we will see that url i'm going to show you once i do it and that endpoint right so this is what the hello is about if i change this to buy then that endpoint is going to be buy okay that's how this works this is that's just the path to the end point that's going to be put at the end of the url to the api gateway okay and the second is a method as a http method of that endpoint i'm going to use get this is also default one so if you don't use it it's gonna be a get as a default but you know just uh for the sake of visibility i have added it okay so now we can remove all of the rest because we're not really gonna be talking about i'm not gonna be talking about those in this video cool okay so that's what we've got in terms of serverless.yml file as you can see it's uh pretty straightforward right what i've done is to set the region to something else which is which wasn't really required but because i'm by base somewhere else not in the us i change that and the functions this is where i actually need to do a change i just added an events section to the to the handler events property and under the events we've got http path hello and method get okay so that's it from the hta from the serverless.yml file and now the javascript code so the javascript code is essentially just a function right lambda is just a function that function takes an event it takes an event and this is what you're getting from api gateway okay i mean it really depends what kind of events you're using for your for your lambda functions function but because this video is about api gateway and we have http section under the events that event here when someone hits the end point right when the client hits the end point that event here is going to be api gateway event it's going to contain all the request information so there's going to be request headers there's going to be request body uh request request params right like a query params and and things like that are going to be on the events plus some extra information from api gateway i will show you in a second once we deploy it i'm going to call that endpoint and you will see it as a part of the response body and speaking about the response this is what you return right as simple as that you have a function it takes an event that has a request data and that function is supposed to return something written an object that is going to be the response right so you written something that's going to be processed by api gateway and turned into an actual http response that is going to be sent back to the client okay and that is the structure that needs to be sent back that's the structure that needs to be detained from the function we have two fields that are required we've got the status code that is 200 it's http status code by the way 200 is http okay but you can put something else you can put 201 you can put error status code if if if you have like if you if you pass the event and there might be something wrong with the body you may want to written a status code 400 which is a bad request right it's going to be it's essentially up to developer how that function is going to be implemented and then a second member here is a body that has to be a string and this is what is going to be written as a response body to the client right and what we're returning here is a json stringified json right because the requirement for the body is a string but we want to written a json right i mean this is a default example but i i want to do it so that's why i stringify and essentially what what the the client is going to see is a json with the message property go serverless v1 your function executed successfully and the input which is the event right so we will be able to see how the event looks like there's going to be the whole big structure whole big object right but stringify.json event so you'll be able to see it but let me before i deploy because we can we can essentially deploy it like that right now it's all gonna work but i would like to be a bit more original so instead of go serverless i'm gonna do something like i'm gonna change the text to hello from lambda okay you can put whatever you want it doesn't have to be a hello from lambda i'm going to add another one another property here which is going to be status code same one 200 just to be able to see like that uh whatever you put in here is gonna also be included in the in the response body and i'm gonna keep the input as it is okay so that's essentially it uh from the uh in terms of modification right of this lambda function and now we're going to deploy it and to deploy it i just click i just open the new terminal in my visual studio code and i'm going to type in because i'm already in this directory in my lambda function directory i'm just going to type in 7s deploy and press enter this is going to take a while because it has to create all the clouds things like you know all the things like cloud formation stack and so on but after a few minutes you should see the output with the url to api gateway endpoint so yeah let's give it a few minutes and we should see it in a second i'm going to show you where this is located in that output that we're going to get okay done our function is deployed and um here in the output under the endpoints section we've got the the url that we can just follow in the browser so i'm gonna copy it and i'm just gonna open my browser and paste that url and there we go this is it we've got message hello from lambda status code which is 200 that's what i put there and the input which is a bit complicated here there's lots of stuff in the event itself you see headers this is coming from the browser we've got http method um resource path and uh all the stuff like your other headers and uh yeah request context and all the other all the other information right so yeah have a look what is in inside this is essentially the part of the of the event and uh yeah let me actually show you something so i'm gonna go back to my console to my command line i'm just gonna use scale and add my header here x x test one two three and i'm gonna paste that url and we should see that header here right x text one two three so that's that's that is on the event right that is on the event the event that we put as a input on the response body that's why i've got it in here cool so this is how you deploy lambda function with the serverless framework pretty straightforward right so now you can play around with this you can modify the code you can try to modify serverless yaml if you are brave enough um maybe just rename the handler right don't forget to rename that in the handler.js you can change the path or the method to post for example and then try with the postman to see if the end point is working um yeah essentially it's up to you what you would like to do you can also experiment with the event the data that you that you're getting there once the function is executed and yeah whenever you change something and you want to deploy your changes right you want to deploy it to aws you just need to do serverless deploy and that is gonna do the diff of the changes and it's just gonna upload the changes that you did either to the infrastructure in the service serverless.dm or in the code in the handler.js it's also gonna be picked up automatically and deployed for you cool okay um yeah this is it uh for today thanks for watching um if you enjoyed the video don't forget to give me a like and uh subscribe to stay up to date with my channel and get notified about any update or any new video any piece of content i create thanks again and see you until next time
Info
Channel: Tomasz Tarnowski
Views: 6,324
Rating: undefined out of 5
Keywords: aws lambda, aws, serverless, serverless framework, getting started with serverless framework aws lambda, lambda, serverless tutorial, getting started with serverless, serverless framework tutorial, getting started with aws lambda, aws lambda and serverless framework, aws lambda tutorial, how to use serverless framework with aws lambda, serverless computing, getting started lambda, deploy to aws lambda using serverless framework, aws lambda deployment using serverless framework
Id: JL_7Odb7GLM
Channel Id: undefined
Length: 35min 18sec (2118 seconds)
Published: Thu Jul 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.