Create Your First API in AWS - Updated Nov 2021- API Gateway and Lambda in the console

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
are you learning aws and want to learn how you can build your very first api well in this video that is exactly what i'm going to show you how to do [Music] hi guys my name is sam with complete coding where our aim is to make you into the best aws and serverless developers that you can be in this video we're going to be looking at how we can create our very first api with lambdas and api gateway in the console this is a great way to learn how the pieces fit together and it's actually an updated version of a video i did a couple of years ago the reason i need to update it is that aws has changed the way that the console looks so this video will make sure that it is up to date so let's jump onto the computer and we can see how to set everything up to create our api so now that we're all logged in to our aws console we can start creating our api now what we need to do is go up to the search window at the top and type api and click on api gateway this will take us to the api gateway landing page and if you've never created an api before this is what it will look like here we have four different types of api the http and the rest ones are what we'd expect for a normal api and there's also web socket apis if you're interested in doing a websocket api i do have a video on that as well so the difference between a http api and a rest api is that http is slightly cheaper but a rest api can integrate with more aws services than the http api in our case and in honestly most cases a http api will be absolutely fine and that's what we're going to build today so if we click on build we go through a little setup process so the first thing we need to do is create and configure any integrations and set up our api we're going to add our integrations later so to start with we need to give this a name so i'm going to call this shopping dash api and hit next now we can configure some routes but we need to do this slightly later so for now just hit next stages i'm going to leave a stage a default stage and i'm going to set this to be live and auto deploy so anytime we make a change to our api config that will be represented at this live stage if i hit next again we have to confirm all of this review that all the settings are right and the only thing really is the shopping api api name and the stage of live so if we now hit create this will actually create our shopping api so we do have a url but at the moment we don't have any paths behind that so this api won't do anything to add some paths we need to go into routes and in here we can click create so on this basic api that we're going to use as an example today we're going to have two different methods one is going to be getting an item by its id and then one is going to be saving a cart so here we're going to first do the get item so we need to set the method to get and then the path is going to be forward slash item forward slash and then in here we want to be able to pass up the id of the item we want to get to have that set dynamically what we need to do is we need to pass in curly braces and then the variable name in our case it's going to be item i d if we now click create we can now see that we have some routes for our api we're going to do the same thing for a create cart this time is going to be a post request and it is going to be a post to cart now that we have both of these set up we can start talking about how we integrate this with aws lambda so that we can actually run some logic and interact with other services so if we click on item d b on the get item here we can click on an integration at the moment we don't have any and if i click on create and attach an integration we can select aws lambda or a lambda function but then we have to choose the lambda function at the moment there are some default lambda functions but none of them are the what the api that we want to use to do that we actually have to create a lambda so if we go into the search again and search for lambda and what i'm going to do is i'm going to open this in a new tab and in here i'm just going to need to move my video a little bit so in here we can now click create function we want to leave it as author from scratch because this is going to be a relatively simple function and the function name we're going to create two functions the first one is get item we want to leave it at nodejs14 although if you work normally with python or other languages there are a range of different languages you could use but we're going to stick with node for this we can leave the architecture as is and same with the permissions we can leave all of those exactly the same and now what we can do if i move myself back out of the way is we can go and click on create function this takes a little a little bit of time to create a lambda function where we can then enter our code once this has finished we'll then get this view and as long as the code tab is selected we can scroll down and by default they give us a little bit of code to help us get started what we're going to do is extend this and add some extra logic so after this to do what we want to do is we want to have a small store of items that the user can select from so we do that by saying const items is an object and then we're going to have an object id which 1234 is going to be the first object id and inside this object we're going to have a description and the description is going to be like a red t-shirt it's going to also have a color of red we're going to set the size to be m and finally we're going to set a price and this price is going to be 14.99 so this is one item and what i'm going to do now is just copy this data for the first item and create a second one so this one's going to be called 5678 and if i paste in this we can then change something so instead of it being a red shirt i'm going to change it to being a blue shirt and in theory you could add as many different items here as you wanted in the future if you wanted to make this a little bit more robust you could actually use a database like dynamodb to store all of your products and do a database look up here but that's for a different video so now that we have the items we need to get the item that the user has requested we do that by getting that path parameter which if we jump into api gateway and into our roots we can see it is the item id now the casing on this is very important so we know that it's item with a capital i and a capital d at the end so here we say that const item id equals event because this function gets past an event on that event there are path parameters and the path parameter we're looking for is item id with this item id we can get the correct item so const item equals items and then we're dynamically going to pass in that item id to get the correct one and then one thing we need to do is check that that item actually exists so if there is not an item because you've passed in an item id that doesn't equal one of these two then what we're going to do is we're going to return an error so here you can see that as a response you need to return a status code and a body so here we're going to return an object with status code and this is going to be a 404 for an object not found and the body is going to be json.stringify object and in here we're going to put a message and that message is going to be item not found for id of and i'm actually going to use template strings here so if we change those from double ticks into template strings we can then use dollar sign and curly braces to add that item id into the message so if there's no item we're going to return a 404 error and if there is an item it's going to go past here and here we're going to return a status code of 200 but instead of stringifying hello lambda we are going to stringify the item that they have requested if we save this and hit deploy we've now saved and deployed our first lambda we can now go back to api gateway and under the item get we can attach an integration we need to create it and say that it's going to be a lambda function and now when we click on here we have the get item lambda that we can select we need to leave this ticked as we want to allow api gateway the permission to trigger that lambda so we definitely want to leave that and then hit create now we have here that whenever someone calls item slash item id it will automatically call this lambda function called get item now we might actually want to test this so the way we do that is if we go back to our shopping api we can copy our invoke url open a new tab paste in our url forward slash item forward slash one two three four which is our first item id and when we do that we get back if i zoom in a little bit we get back our item of red t-shirt if i change this from one two three four to five six seven eight we now get back our blue t-shirt which is exactly what we expect if we enter a random number like 9999 and send this we actually get a failed response saying item not found for id of 9999 so that is basically our first ever endpoint created now that we've done that we want to do one for a post request that takes a body so if we go back to lambda and go back to the home page we want to again i'll move myself out of the way click on create a new function and now that we have that we can create a function this time called create cart and again leaving node.js and all of the other settings as are and hit create function again this takes a little while but what we're going to do in this function is we're going to console.log out the object that is passed up as part of the cart we're not actually going to write this to a database or anything like that but we're going to add some add a line of code saying that that this is where we would add the database writing if we wanted to add that later so this function is going to be a lot simpler so after the to do what we want to do is actually get the body so const body equals and now by default the body is stringified so we're going to jason dot pass and it's going to be on event dot body now that we have this we can console.log the body passed as part of the create cart was and then the body so that's going to console log out the body that gets passed up and here if you had a database this is where you would add the database dot write logic so in our case we're not actually going to hook up a database in this video as that would make this video way too long but in the response we now want to return a message saying that the cart was created so the default status code for a created response is actually 201 and here instead of stringifying the message of hello from lambda we want to stringify an object with message and this message is just going to be cart created again if we save this and hit deploy we've now deployed the changes for this function which is the create cart function so now we go back to api gateway find our routes and this fine fine this time find the post request on cart here we attach the integration and now if you've got lots of integrations and multiple endpoints call the same function you could do that but in our case we have a single function for each of our endpoints so we need to create a new integration this again is going to be a lambda function integration and in here we want to select the create cart function and hit create this is now all hooked up so that the get request goes to the lambda called get item and the post request to slash cart triggers our create cart lambda so you can't actually do a post request very easily inside a browser so i've opened up postman so what i'm going to do is again go back to our api and copy our url and postman just allows us to very easily make requests that are more than just a get request so here we can say i want to do a post request paste in our url forward slash cart and on here we can say i want the body to be a raw type and i'm going to set it to jason and in this i'm going to say that there is going to be a cart id and this is going to be some random number the cart is going to have an owner and that owner is going to be me sam as well as that we're also going to have items this is going to be an array and i'm going to have one item of one two three four a second item of one two three four and a third item of five six seven eight so now that we have all of that what we can do is we can go and click on send which is just behind my head so now if we hit send we are sending that response and if we scroll down across to the response that we got we got a response of 201 which is exactly what we expect with a message of cart created to see the body if we go back into aws look at create cart and in here if we scroll up to the monitor section there is a button here for view logs in cloudwatch and that is what stores all of our logs so if we open that up we will see that we can see all of the logs for the create cart lambda and if we click into this log stream we can see that in here the body passed as part of the create was and then the body that we've sent up from our request this is exactly what we expect to see which is really good because we could now save that into a database in this video we've looked at how we can use api gateway and aws lambda to create our own api in aws hopefully this video has helped explain how the different parts work together to create your api and that will allow you to go on and build more apis of your own if you've liked this and are looking to go into a career in aws and serverless then i recommend checking out the serverless series that i have on aws and that will allow help you to write your apis and create things like we've done today but using the serverless framework the serverless framework allows you to write code which defines how your api looks as well as other things including dynamodb databases s3 storage buckets and much more so if you're looking to get into software as a career i recommend checking out that playlist and i'll have it linked in the video and also in the description below you can also subscribe to our youtube channel where we release regular content on aws and serverless whether you're a new developer who's trying to get into the market or you're an experienced aws or serverless developer who wants to learn how the best practices are applied and designing big or complex systems using the serverless framework so hopefully you subscribe and then i'll see you in the next video
Info
Channel: Complete Coding
Views: 810
Rating: undefined out of 5
Keywords: AWS, serverless, Serverless Framework, NodeJS
Id: X5dM2rlOKfw
Channel Id: undefined
Length: 23min 0sec (1380 seconds)
Published: Wed Nov 17 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.