From the course: Building Dynamic Websites using AWS Lambdas

Setting up an API gateway - Amazon Web Services (AWS) Tutorial

From the course: Building Dynamic Websites using AWS Lambdas

Start my 1-month free trial

Setting up an API gateway

- [Instructor] Let's get right to work. I've got the AWS Management Console open, as you should have, and what we need to do first is we need to build out that API gateway, which is literally a gateway to your API. And we've looked at that diagram a few times by now. We've got to make a request to an API gateway and allow it to route that request to the right Lambda and the right piece of code. So, you can look up that service and it's simply called API Gateway, it'll appear right away for you. And you're going to get a screen that looks something like this, especially if you've not used API Gateway before. Now, this is the default and it's going to ask you right off the bat to choose an API type. Now, don't get too bent around this. We just want to use a rest API. A rest API is probably the simplest option here and it allows stateless, in other words, requests that are kind of fire and forget, you don't have to keep up with who made the request, there's no session. And if these terms mean nothing to you, great, rest is perfect. We want a rest API, and you'll notice that it works with Lambda, which is great, HTTP, which is exactly what we're using to move data back and forth from our browser to AWS, and of course our various AWS services. Now, I will tell you, don't use the HTTP API because it's in beta and there are a lot of changes there. And don't use the private rest API, because we want to be able to access this API gateway, this rest API, pubicly. So, click on Build and you're going to get a whole bunch of options. It's going to give you a little bit information, skip that. But now it's going to ask you what protocol, and again we want to use rest. Rest is simple. It supports HTTP, as well as, HTTPS, which is really great, we can add security. And it's very inexpensive both from a network point of view and from an AWS point of view. Now, creating a new API, great. You can leave the example API up, which is going to give you some sort of basic information. You can do that if you want, but let's just go ahead and do a brand new API. Now, you have to give it a name and we want this API to be named, not for a specific piece of functionality, like feedback, but really for what all of the things are it might support. So, in my case, I'm going to call this the WisdonPetMedicineAPI. That's because I'm going to eventually have this service the entire Wisdom Pet Medicine website. And you could even go broader than that, maybe you eventually have a veterinarian API that supports all kinds of different veterinary websites. It's really up to you. For my purposes, though, and for yours for getting started, I recommend you just build one out to support the entire website. So, Wisdom Pet Medicine website support. Leave the endpoint type as regional. The other options here are edge optimized, that's if you're using caching in cloud front, or private, neither one are what we want. So, go ahead and create the API, and now you're going to get this screen where you can build your API out, and that's really what we're going to do. So now, this is important to note. The routing related to your API is all done through API Gateway. But the actual running of your code and defining the code is done in Lambda. That's why, earlier on, even though it was a new concept, I introduced you to API Gateway. You cannot really work with Lambda without getting into API Gateway, because that's what's going to route our requests. Now, you'll notice on the left that what's selected here is Resources. A resource is effectively an endpoint, it's what's called. It's sort of the chunk of functionality, and typically the resource is the noun in what you're doing. For example, we're building out something to handle feedback. Feedback is the noun, and so it is the resource. If you were building out something to allow you to perhaps place an order, order is the noun. So, what we want to do is we want to create a new resource, and we're going to call it Feedback. And again, keep it simple here. Don't make your naming particularly complicated. And then, what's going to happen, you're going to see here, is that the API Gateway console is going to automatically derive the name of this API, what you'd actually put into a browser or a request, from your resource name. Now, if you were to put Feedback Gatherer, it's going to add dashes in, but that's really not a good name. Again, use a noun. And then, we don't need to enable API Gateway CORS, just leave the default. Go ahead and create that resource. And now, you're going to see a visual representation of this new resource. So now, you've got this API gateway, it has a resource called Feedback, and we want to add to that a method, and the method is going to eventually trigger our Lambda. Now, you're going to see that these methods correspond with the HTTP request types. Get gets content and that's typically what's happening when you're pulling down a new webpage. You're getting the HTML and CSS web, it's a Get request. Head is a type of request that gets metadata, Patch, Put, all of these are a little less common than Get. But what we want is we want Post, because we're going to build a form to call this and forms post data. They don't get data, they post data. So, we're going to select Post here, click the check mark, and it's going to set up all of this that you see. So now, it wants to know how are you going to build out the actual code. Well, it's got Lambda function selected, so that's perfect for us. It's got a Lambda region and this is where you're going to run your Lambda, okay. You can choose anything you want here. For whatever reason, not all regions are supported. So, if you're in a region that's not supported by Lambda, just pick the closest region. I'm going to leave this as US-west-1, because that's where I am, I'm in northern California right now. And now, it's going to ask me, what is the Lambda function? But, as you see here, we don't have any Lambda functions to find yet. We're going to need to create that. So, we have a resource, we have a method. We need to pause our work on the API Gateway, go and build out a Lambda function, and then we can come back here and tie it to our particular action on our particular resource.

Contents