To make the most of this tutorial series, create a Serverless Framework PRO account for free
In order to really appreciate what the serverless framework does for us, in this video, we're going to be developing our own API Gateway endpoint manually by using the AWS console to put together this API endpoints that will trigger a Lambda function.
So let's log into our AWS console and let's go find the API Gateway service. Once you open the API Gateway service, if you have never deployed a a API Gateway endpoint before, you'll see a screen like this. We'll just click the get started button to get us started and I'm just going to accept this as a default demonstration that API Gateway gives us just so we can move on and create our own.
So I'm going to continue with fail on warnings and import and just let it finish creating this API Gateway for us. So what I want it to do at this point is I want to click on APIs here. I want to go back to the beginning.
I'm going to create myself a new API for us to play with create API. This one is going to be a rest API. We choosing new API as the option, and I'm just going to name this a, give it a very simple name, it doesn't really matter what this is. At this point I could give a description about what this API is. I'm going to leave it as is and regional as the end type should be fine. And let's go ahead and create the API.
Once our API is created, we need to go ahead and add ourselves a new resource. So let's go open up the actions button there, select create resource on the new child resource page, which can make it very simple. Hello resource. That's going to return a very generic response to our API request. So let's just name this whatever you want to name it, I'm just going to call mine, Hello. And then I'm on my endpoints to also reference the same of similar endpoints. I'm just gonna make that hello as well.
And with that complete, the proxy resource, we don't have to worry about at this point. This allows us to do things such as allow our Lambda functions to rather handle the routing. We're going to ignore this for now and we'll look at it probably later. And API Gateway of course is a feature we'll look at later. Right now we're not going to make any API requests from a browser that requires us to enable CORS, so I'm going to create resource.
Once you create the resource, you should see that we have it available here / hello, However, we don't yet have an endpoint to actually make a request to. So now we need to add a method to that to allow us to actually query it. So this is where we go here we say create a method and here we need to choose the GET method. We just got to make a simple GET request to / hello.
And once we are happy with that, we just select that option. We don't yet have a Lambda function to integrate to which is what we're going to do next. So for all intents and purposes, we are complete. Once we have our level of function created, we will then add it in here. So let's go ahead and create our lambda function. To do that, let's click the services link at the top and search for Lambda.
Once we open up the Lambda page, we just want to go ahead and create our first new functions. So let's go ahead and click on create a function. We went to author one from scratch. We're going to create one all the way from the beginning ourselves, but we do need give it some kind of name and I'm just going to keep it consistent with our API Gateway endpoint and call this Hello. Once we finished creating our function name, the runtime will keep as Node 10.X and we need to take a look at our execution role here because now we need to decide on permissions for our Lambda function. Luckily for us, we don't need anything fancy. We're not accessing any other AWS services so we don't need any thing, advanced yet. We're just gonna ask here to create a new role with basic Lambda permissions, which is more than enough for us.
So let's go ahead and create our function. After a few seconds of waiting for the function to create, you should see a screen such as this one where we can now continue to configure what our function contains. So let's just scroll down because we want to add in our actual code that is going to handle our hello. So you can see here we've got a function called export start handler. The function has an event object and this is what the interior of a Lambda function tends to look like.
We have a function that receives an event object with this event object we can then do things like return responses, analyze the event object for data that might've come with it, like a data in our body or parameters in a URL. In our case, we have it, we have a very simple, API endpoint here that doesn't pass any data. So what we are going to do is just modify this, this automated message that got created for us and we're going to say hello from serverless just to make it unique. Once we finished editing this, we can go ahead and save our function and now that we've actually got this function, we can go ahead and look at adding it to our API Gateway endpoint. So let's go ahead and head back to API Gateway by clicking on services and searching for API Gateway.
Once we are back at API Gateway, our test demo API is waiting for us. So I'm just going to click on that. I'm going to select the GET a method that we had earlier and now we can complete this lambda function field and actually choose the lambda function that we had before. So I'm just going to search for it by typing hello and there is our Lambda function and now if I save, we should have an API Gateway endpoint that executes our Lambda function and this will automatically add the permission to our lambda function to be executed by API Gateway.
And with that save complete, we can now go ahead and execute our endpoint by clicking on GET here we should be able to get to a screen where we can run a test on our API endpoint. So I'm just gonna click there and we don't have any parameters to pass to our API endpoints so I'm just going to click the test button and we should see immediately here is the response from our Lambda function. Hello from service. So here we can see our API endpoint point worked, we have an API end point that will return essentially hello from serverless. So this is how you would manually create API Gateway endpoints and link them to them with functions manually.
But again, there are some downsides. Unfortunately with this method, there's no way for me to easily extract the configuration of my endpoint and the configuration on my Lambda function as well as the link between them. In order to share that with another developer in my team, I have no way to allow him to create this on his end so that he can make edits and he has no way to give me those edits back so I can incorporate them into what I'm working with to continue development on my side.
What if you had, 20 endpoints and 40 Lambda functions that had things in various ways that you had to manually maintain and control through this AWS console? This might not seem like such a big deal now when you're just dealing with a very simple API Gateway endpoint and a very simple Lambda function, but things can get complex really quickly, especially when you start mixing in things like Lambda functions triggered by other AWS services like S3, SNS, SQS, DynamoDB, and the many, many, many other services in AWS that can trigger Lambda functions.
For that purpose, using the AWS console in this way becomes rather limited and slow to use and very error prone because now you have to configure everything manually and hope that you don't break things every time you come in to make changes. The other side of this is that you cannot share any of this again with any developers. So when things get really complicated, everybody has to work off of the same console, and you cannot test things in isolation before you put them through into your live systems.
So obviously there's some issues here, and this is where we will now look at the Serverless Framework to see how this can help us rather manage these, these types of development practices in a cleaner and more portable way.
Validating user JWTs