Deploying a AWS Serverless REST API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone welcome back to another web dev junkie video my name is cody seibert and in this video i want to show you how to spin up a serverless api in amazon using the serverless framework so if you've heard about the buzzword serverless it's just a way to pretty much run small individual functions on the cloud to do some processing and you can host like apis i think there's stuff i read about hosting like react or vue or i think next any serverless function that have it actually host your ui so you can do that too if you're interested so let's just go and get started i first of the first things you got to do before you even try to run any amazon stuff on your machine is you need to make sure you have the amazon command line set up and if you go to this url so amazon.com cli on the right here you have your different distributions i went ahead and installed the 64-bit of windows which is just an installer you run it and installs it and that is going to put a folder here c program files amazon adb aws cli v2 so when this folder is created i did have to go and set up my environment variables so if you don't know how to do that you type in enb here you can say edit and set up environment variables you click edit variables and then in your path you click that and then down here you see that it's pointing to that folder that was created so you have to be able to do that in order to run cli commands so notice down here after you set that up you may have to restart your vs code um but now i can run aws and that will actually like spit out some stuff so we are set up in that aspect now the next thing i want to show you is the serverless framework we're going to be using so if you go to serverless.com on this page they have like different examples so if you want to deploy view app you could just okay you could just go over here and click on deploy view app and they'll give you some examples they have like react but what we're doing is we're going to try to deploy a api and i'm going to do it the way that i know i'm not going to actually use these pre-made things so let's just go to the docs and follow their instructions here so if you go to their docs uh let's see is there like a getting started here getting started guide so the first thing it's going to tell you to do is that you need to install it the the serverless framework that is so you could run this command npm install g surplus and that will install it globally so that you can actually run it and deploy your applications let's just go ahead and do that um in a second note i do have my amazon account set up and i'm signed in here because we're going to be kind of diving through the ui so definitely sign up for a free tier account um you have to enter a credit card or debit card or whatever but you're going to be needing this in order to be able to deploy your serverless functions so and we're just going to be deploying a really simple rest api we are not going to be like connecting to a database we're just going to have an endpoint or two that returns some json or takes in some json and that's about it you can use your imagination and expand upon the ideas that i'm presenting in this video and there's tons of tutorials out there as well but i just want to kind of show you how to hit the ground running with serverless just show you that it's pretty straightforward so once this is done installing which you just finished down here you can type in i think it's just serverless so yeah type in serverless and it's going to ask you a couple of questions it's going to ask you like what folder do you want to yeah do you want to create a project yes what is a project you want to create i'll say aws no js what do you want to call this project i'll say serverless example that should install or create a folder called serverless example inside of the whatever folder you're in uh would you like to enable this stuff i'm gonna say no i don't even know what that means would you like to set them up now it says you need aws credentials to be able to run this so you could go ahead and say yes and it's probably going to ask you for credentials which i haven't created yet so i don't know i had to enter twice so yes do you have an a to do this account yes please enter to continue after creating your aws user with the access key so that will just go ahead and open up a tab it looks like where you can create your user so i'm gonna go ahead and click next this will create a user with the name serverless with programmatic access next i'll go to permissions and i'll give it administration access i'm going to say be very careful here if you give your account administration access it has the ability to create and destroy anything in your account so with that being said let's just do it and then let's go to review click next create user and at some point it's going to give you a access key which i'm going to make sure i delete before i post this tutorial because i don't want you all creating ec2 instances on my amazon account so you can get the access key and over here you can get the secret key so it says press enter after you're done you can also download it i'm just going to download it so i don't forget about it so i'll press enter and then it's going to ask you where your access key is so you copy that paste it in it's going to ask you what your secret key is so i will copy that paste it in and now it says the credentials are saved to this location okay so we should be good to go hopefully and if i were to cd into that serverless example folder and open that folder up in vs code by the way never don't leak these keys if you leave these keys anyone can just access your amazon account so all right so let us give you an overview of what we're doing so when you create your serverless project why is it not keeping my thing open okay so when you create your serverless project um you'll get a serverless email file and this is where you define like your top level definitions of um all your serverless resources how cloud formation is going to be spun up if you need any additional cloud formation resources but there's a ton of comments here you can read through them i suggest you read through them but i am not going to read through them because this is just too much so let us just get rid of all this stuff so the main thing is when you run your serverless deploy it's going to try to create a bunch of resources if you define them so like for this example sometimes your lambda function needs access to dynamo or access to i don't know an s3 bucket so it's good to define the permissions that your lambda function is going to use here on our project we actually do this as a manual step because we don't want our ci cd pipeline creating permissions and roles for us so you can do that just a good thing to notice so you can define environment variables here for your lambda function if you want here um you can exclude and include stuff into your package let us just go ahead and get rid of um yeah we need this okay down here is where you define like more resources like if you want to create an s3 bucket when you do a serverless deploy you can do it down here and all this is you have pretty much have to look into it because i'm not going to cover it in this tutorial i'm just going to show you how to deploy the bare bones rest api so the main thing though is that there is an events delete all this there is an events section here in the cml file and you can tell which in point your function is going to call so let me give you an overview i have a function called hello and that points to a folder i mean a file called handler which happens to be this one here and then inside that handler file it points to a hello function so if i look at that handler file notice that it exports something called hello right so i could call this my function here and if i could go back and change hello to my function and this could be named whatever you want but as long as the file and the function that's exported inside that file are here you can successfully deploy that lambda function the second thing is you're telling this lambda function that it wants to be triggered by an http event this should create you an api gateway where you can actually access your endpoint so if i were to change this to path i know hello and method git that should deploy a git route at slash hello and i will show that in a second so this is our bare bones serverless file that we're going to be deploying and inside of this let's look at this all this does is it basically returns you a string that says your function executed successfully and uh it also outputs what event was passed in now it says use strict there all right so let's just go ahead and try to deploy this so this is like your baby steps your first serverless project let's just go ahead and deploy it so if i do this all correctly i should be able to type in serverless deploy and that is basically going to package up your folder so like all the contents that are in this folder are going to get put into a zip and placed into a dot serverless folder which you can see here so if you see the zip that basically has my js file it has my serverless yaml and then it also has some json files that kind of describe your cloud formation template um and also your serverless state so if you look here you can see that it has a ton of stuff already created i think it tries to create like a s3 bucket and to put the zip on to and then it deploys stuff so there's a lot of stuff going on behind the scenes that you when you're starting off you don't need to know but at some point you might need to start like learning how the stuff actually works under the hood because you're going to run into issues as your project is larger and larger yeah so cloudformation is what serverless uses to deploy all your resources so if i go to cloud formation if i could type it in formation all right while this is trying to deploy you'll notice that we have a serverless example dev saying status update in progress now the dev comes from something called a stage which happens to be defaulted to dev but when you do your deploy you can do hyphen hyphen stage equals i don't know prod or stage is equal to [Music] qa whatever you want to do it's just a way to denote the different environments that you're deploying and when that is done it'll give you some information down here so if you look here it says that it deployed your service to this stack there's 11 resources associated with it and this is the end point that you can hit it on so if i were to copy this link or just control click it it should open yes let's see so if you notice here we actually got back that message and we got back that input okay and all this input is just basically showing you like what you can play around with so when you get an event from api gateway this thing there's different things you can access you can access your path you can access your path parameters your query string parameters your body your payload um some request context stuff you get a lot of information when the event comes in in case you need to do stuff with it uh but at this point the cloud formation stack is done deploying and you can access your endpoint so that's how easy it was to get running it's it's a lot more configuration like setting up aws getting your credentials installing serverless starting your project um but once you get it set up it's pretty easy to get going one thing i will talk about is that there's a lot of serverless plugins so if you notice that this url it's not a custom url it's just something that amazon generates for you and gives you so if you wanted to use your own custom url there's a plugin called like domain manager i think uh let's see if this is a serverless domain manager yeah so part of the amplify education there is a plugin you can use and if you set this up you can basically get a custom domain like you have to i think have your domain already pointing to amazon i believe because you have to point it to a zone all right it might create the zone for you but let me read through this oh i thought it has something to do with zones let me go back hostess yeah we use zones in our projects but maybe you don't have to but there might be some more steps you have to follow to get a custom domain pointing to your api gateway so let me show you the other things that were created so second thing that was created if i go to api gateway this was another resource that was created from that serverless deploy so you notice here you have a dev serverless example with that same id so you can dive into that and you can see that it has different paths set up so if you see here we have a slash hello resource that takes in a get request and that basically invokes a function okay so api gateway is basically what maps your external urls to your internal lambda functions let's see if there's anything else i can kind of talk about here uh probably not that's like the pretty much the overview um the second thing that was created is a lambda function so if i go to the lambda terminal or whatever dashboard you can see that we have a hello function so if i click onto this anytime you hit this endpoint this lambda function is going to fire off so if i go to you see our codes here you can actually see your code if it's not too too large so we have our code one thing that's nice to know is your monitoring down here so you can actually see when people access your lambda you'll see like a little blue dot when someone tried to hit your lambda tells you how long your lambda took to run so this is two milliseconds any errors throttling stuff like that just to support important metric stuff and then the third thing that's really important is your cloud watch log so whenever this lambda function you know you might have console logs that need to log out certain things oh no why'd i click away from that anyway so if you go to your law groups you'll notice that you have a law group already created now this law group points to that lambda function so whenever your lambda gets created it runs it's going to print out some stuff so you notice here we got a start event for that function and then we got an end event and then there's a report saying that everything ran fine this is how much memory was used this is how long it took stuff like that so those are the main things that were all set up i think let's check s3 if there's anything in s3 that got created yeah so it also creates an s3 bucket this s3 bucket called serverless example deploys or whatever this is where it basically takes that zip that i was talking about so over here you have a zip and it uploads it here so that a lot of folders here it uploads a zip and also a cloudformation template so that a cloud formation knows what json to grab to start deploying your stuff and then b lambda knows what zip to use for your code so this zip is the one that has like your handler.js file and stuff like that um and i'm sure there's other stuff it created so let's go to iam i think when you do a service deploy it will create a role for you maybe let's see all right yeah so no i created this serverless one let's see no okay so maybe it doesn't create anything in iam but you could probably go to cloudformation and check so again cloudformation is a infrastructure as code tool that amazon provides you but if you dive into it you can look at all the resources that was created when you did a service deploy so it looks like it created a lambda execution what is this that's a role so it did create a role a land execution role it created an s3 bucket a bucket policy a log group a lambda version some land of permission so it creates a lot of stuff and as you install more plug-ins you'll notice that it creates a ton more stuff behind the scenes so that is the gist of it i mean you have a endpoint that you can hit now and you returned it returns some json to us which we saw here you just have to make sure you stringify it and you return a status code and you can you know if you want to do a post request you just have to go in and change this method to a post and then make sure you do like event dot body to get the body of the post and stuff like that so it's pretty straightforward to get running the complicated stuff is behind the scenes like what's going on in amazon what resources is it creating all that jazz um now the cool thing is after you're done or if you don't want your serverless thing anymore i think you can do a serverless deploy or destroy no i think it's called a remove so serverlessremove should probably just delete this cloudformation stack which means that it's going to delete all of those resources that were just created here right and i'm doing that because i don't want to get charged for having these resources up especially since this is just a tutorial but sometimes you want to blow away your environment and start fresh and sometimes you could do that with serverless remove and i think one of the selling points is if you're using microservices you could have a bunch of these little serverless email files for every service and deploy them separately as your microservice changes so let's see this actually remove it did so my stack if i go here and refresh my stack is gone alright so my stack is no longer gone or sorry my stack is no longer there if i go to delete it you can see the stack was deleted and then if i go back to all those different like resources like s3 you'll notice that the bucket's gone uh lambda that's probably going to be gone at least i hope it all is so yeah we just fully cleaned up our serverless deploy and we can start fresh if we want to and there is a lot of other stuff that you can figure out and learn there's like configurations deploying resources manually like if you wanted your serverless deploy to deploy a dynamodb database you can do that you can basically have all your stuff managed in one serverless file if you want one thing i'll note is that you're going to hit limits there are limits to how many things you can deploy with cloudformation i think it's like 200 a stack so as you keep adding endpoints and adding plugins and adding resources and stuff you're going to hit [Music] that amazon limit which means you're either going to have to split up your deploys into multiple serverless files or you're going to have to download a plugin called like amazon serverless split stacks both of those solutions are bad in my opinion i think they should just allow you to deploy a lot of stuff from your serverless yaml but that's an amazon limit so there's no getting around that so you basically have to kind of modify the way you're doing stuff just to get over that um which is unfortunate but i mean for a small little example project serverless is pretty awesome but as your project is larger we actually moved away from serverless and we're just deploying a a model lambda using terraform and just a a plug-in where you can basically have one lambda that takes in all of your get requests post requests and then it kind of routes it internally that's that's another discussion but anyway i hope this overview was helpful i hope you learned something about serverless and how to deploy an api sorry i didn't deploy more like i just deployed one endpoint but i hope you you got the idea right if you wanted to deploy more endpoints you just add more functions here right pretty pretty straightforward so you can say like hello2 is a different function that calls my other function and then that's a post request to a hello to endpoint or hello to you could do like parameters here i think as well i forgot the syntax but you can look that up so then if someone hits this path the post request is going to call other function other functions not exported yet so module exports other functions do some stuff here okay so yeah thank you so much for watching again this is a web dev junkie video my name is cody seibert if you have any comments questions or concerns or suggestions of other videos i could make or other things you want to learn about be sure to post it in the comments below if you're new to this channel be sure to subscribe because i'll be posting stuff like this in the future alright have a good day and thanks for watching
Info
Channel: Web Dev Junkie
Views: 3,055
Rating: 5 out of 5
Keywords: aws, serverless, lambda, api gateway, serverless framework, aws cli, iam, cloudformation
Id: 7_YNfFPbydE
Channel Id: undefined
Length: 22min 31sec (1351 seconds)
Published: Wed Aug 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.