REST API Tutorial - Build and Deploy Rest API in 900seconds

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Bismillah, bismillah, bismillah. What is going on, guys, Assalamualaikum. Welcome to Amigoscode. In this video, I want to show you one of the fastest ways that you can build and deploy an API using Amazon, AWS using API Gateway and Lambdas. So serverless is one of the technologies that really does excite me because of the ability of you deploying code without having to think about service. If you into my channel, go ahead and literally subscribe to my channel, so I can keep on providing you guys content like this. If you know, part of the Amigos code community, go ahead and join. The community is growing almost 40,000 people and yeah, it should be part of it. So for this video here, what we're gonna do is so in here we're going to have some clients. So this could be your react application, Angular, postman your web browser. And what we're going to do is we're going to basically send an auto request, which then will be intercepted by the API gateway right here. And then this will forward the request to the Lambda. And for the Lambda we're going to use Golang. So you can actually write your Lambda function in any language such as JavaScript, Python, Go, Java, but Go line. It's one language that really excites me because of its simplicity. Then the Lambda basically does all the business logic. And if you want to perform any credit rations, you can actually store data in any of the databases of choice, maybe a relational database or an object database such as DynamoDB. So in this video, I want to focus really is how you're going to basically implement the API gateway and the Lambda. And if you are more interested about videos like this, please do let me know because I'm planning on recording a series on AWS because yeah, it's one platform that you shouldn't know how to use without further Ado. Let me go off this video. So the first thing I wanted to do is to be a folder and then open that folder within the Vs code and within the terminal. Let's create a Go module. So Go mod in it. And then in here, this will be my repo. And inside you see that we have the Go mod. So this now with the structure for us to build and go get some modules. Now we want to get the AWS Lambda go, you can see that's added. And you can see that. Now we have it there. So now let's create a new file called Main Go, and this will contain our code. And for now let's just have the main package, a main function. And for now it's just FMT. And then Hello. And you can see also the import. There we go. If I run this, you can see that that was super quick and nice and easy. This is why I love Co. So now let's basically have a function for our API gateway. The function will receive the API request from the API gateway, and it will return the response as well as any errors. Now let me add the import. So this will be a Slam go and then events. And inside of my function I'm going to return events API response and then have the status code as 200. And then the body for now is don't forget to subscribe. Literally. Don't forget to subscribe. And one more thing that I need to return is the error. And in this case will be nil. So now I need to change the main function. And here we need to basically say Lambda start and then inside of the start method, we take the function that we've just created and in here don't invoke the function. Now let's update the imports. So let's import the Lambda from AWS Lambda. Go. And I don't need FMT anymore. And to be honest, this is it. So you can see that this is really simple code and straightforward. Now let's open up the terminal and I'm going to build this for Linux. So go OS Linux. And then I'm going to also define the architecture. And basically these are environmental variables that I'm setting for Windows. This will be different. So here now I'm going to invoke go build and then O and then made. So this will be the output or press enter. There we go. Now let me just type LS so you can see the outputs in here and you can see that we have main in red. Now what we need to do is to take this main file and this is the binary and zip it. So let me just type zip and then main zip the name of the zip and then main, which is what I want to zip. If I press enter, you can see that's done and have a look. So now we have main zip. So this is what we need to upload now to our Lambda inside of AWS. And you can also see that we have the zip in there. So we've zipped our Lambda. Now let's take the zip file and then upload it to a Lambda function using AWS. So for this video, I'm going to be using Amazon AWS. And this concept really works with any other com providers, such as Google Cloud or Azure. Now Amazon gives you a free tier where you can basically experiment and work with their products for free. And they give you, for example, 750 hours of easy to instance, 1 million free requests per month for Lambda so on and so forth. So you can go ahead and read on this. But basically go ahead and sign up. If you don't have an account, then sign in. And once you are inside, the first step that we're going to do is to build our Lambda. So we're going to build this Lambda right here where we're going to upload our code and then we'll build the API gateway that sits in front of the Lambda. So back to the console. And in here you can search for Lambda and you can see a run code without thinking about service. Now you can see that I do have one Lambda, but let's create a brand new one. So create function. And in here we need to give a function name. So in my case I'm going to say test just for simplicity. But usually you name your functions with a better name. Now in here the runtime. You can build lambdas using various different languages. So in here what we want is to build using Golang. So here you see Go one point X. If you want to use C sharp, you can do it. Java, Eleven, Python, Ruby and you can see all the other different versions in here as well. But in here let's just Select Go and then the architecture. I'm going to leave as default because you saw that I've built using X 86 and 64. Now in here I'm going to leave everything as default, and the Lambda will create an execution role with permissions to upload logs to cloud watch. So here let's just create the function. Alright. So that is done. So in this UI in here this is basically how you can customize your function. But the first thing I want to do here is I want to upload the code. Remember that we have a zip file. So here let's just say upload from and here I'm going to select the zip file that we've just created. You see main zip and then open save and drop. Done. So you can see that you have this message here. So the plumbing package for your Lambda test is so large. So usually you are able to see the actual code in here. But for Golang, it's not possible. But also what I want to do is in here where we have D. So in here where we have the runtime settings, click on edit. And then here the handler. So this handler here will be Main and then save and let me show you in code. So main is this name of the main function basically. So if you name this something else you have to basically say that you handle is the name of that function that has the handle request. So now we have successfully uploaded a Lambda in here using Golang. Now let's configure the API gateway that will handle the requests from the clients that you might have. So back to the console. So in here we can search for API and then gateway. And you can see that I also have this API gateway in here. But let's create a new API. And here there is a couple of choices that you have. So either http rest web sockets on and so forth. And also a private API. And I'm going to leave a link where you can read more about the difference between Http and rest API. So here let's just say build and then let's choose an integration. So the integration will be from our API gateway. We want to have an integration to our Lambda. So I'm going to click on this drop down and then choose Lambda. And then here I need to pick the Lambda. So make sure that you have the correct region in here and then search for the Lambda. So in our case we have test. And to be honest, this is it. So now we also have to give it a name. So here let me just say test as well. I'm going to name this a test. And then I can say next and have a look. So here we have method. So these are the http methods, get post patch, so on and so forth. And you have the resource path. Now in here you could actually say any and then you could have your Lambda handle all the requests. So what I'm going to do is I'm going to choose get and then leave test. So this is for and then test. And this is the Lambda itself. If you want to add a different route. So you could say right. So here this will be post post, for example, test as well. And then the integration target could be also the Lambda itself in here. Right. And I'm going to show you later how to handle basically post. But for now let's just have the get request in here next and then everything as default. So we want this to auto deploy our Lambda whenever we make some changes. Or you could have a stage, maybe one for testing and then the other one for live traffic. But in here I'm just going to have auto deploy next and then let me just review the code. And then if I Scroll down create and we are good to go. So you can see that now in here we have this invoke URL, which is basically how we access our API. So obviously here you could have custom domain names. So you can change this URL. So in here let me click on this URL. And in here we actually get a not found. Now the reason we get not found is because the path that we want is for and then test enter and then have a look in here. Don't forget to subscribe. So this is really awesome. Now one last thing that we need to do is in order for these two. Now now in order for this to work in here where we actually Marshall the struct array in here. So what we need to do is we need to tell that this type struck right here. This name will be represented as a JSON blob. So in here we have backticks and then say JSON call them and then the name of the property. So this will be name and in here no spaces are allowed. And also I need to have this as a big capital. So I need to export this. So if I put it back, I just want to see that look. Would they say struck field name has JSON tag but not exported to export in Golang you say capital now that's dropped on. Let's basically build and zip again. So let's basically build it like this as you see. And then let's zip it. Now let me open up the Lambda page any side of all function. Let me upload, zip upload this zip open save and there we go. And you can see also that the integration for the Lambda is the API gateway or the trigger right here. So now let's go back to the API gateway. So I open up our API gateway test and then the URL in here. If I click on it, have a look. So we have message not found. And this is because we need to say for slash and then test and check this out. So we have now a JSON. Blob. So you can see an array of objects. So name Jamila and then name Alex so you can see that this is really, really easy, super fast and convenient. All right. I hope you had fun throughout this video. As you saw, we built and deployment API in the matter of seconds. And also if you want to send a post request, really, what you need to do is to create a new route so this will be a post route and then you have the integration as the Lambda and the Lambda can actually be responsible for multiple routes. So within it you have the context and within the context, you can actually grab the http method and then you can have an statement. If equals to post, then what you do is you basically take the request body and then you unmarshal it into a struct and then you can store it to a DynamoDB or RDS so on. So forth. Otherwise, if it's get then basically pretty much do whatever we've done. So this is all for this video. If you want to see more videos like this, please do let me know and comment down below and let me know what you thought about this video. If you know a part of getting good community, go ahead and join both the private Facebook group as well as discord. The community is growing and I would love to see you there. this is all for now and I'll catch you on the next one. Assalamualaikum.
Info
Channel: Amigoscode
Views: 9,535
Rating: 4.9644446 out of 5
Keywords: amigoscode, rest api, rest api golang, rest api tutorial, rest api python, rest api django, rest api nodejs, rest api best practices, rest api basics, rest api beginner tutorial, rest api code, rest api crash course, rest api course, rest api deployment, rest api explained, rest api example, rest api for beginners, golang, golang tutorial, go lambda, go lambda example, go lambda api, go lambda tutorial, go lambda functions
Id: AUm23lsBh24
Channel Id: undefined
Length: 16min 46sec (1006 seconds)
Published: Mon Oct 04 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.