AWS Lambda in Go - Lambda REST API + API Gateway

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so let's jump in today we're going to be setting up um a lambda behind api gateway to function as a rest api so first thing we're doing just on the aws console here let's jump over to api gateway and we're going to create a new resource here we're going to create a rest api right down here there's a couple different options for api gateway here so http apis very very similar to rest it can do pretty much the same things as these rest apis if you want to see like extensive differences here aws has this little doc page here that kind of talks about choosing between the two apis um kind of has their trade-offs what they can do um and it kind of goes through each of those so the main thing that we want from um from the rest api option is let's see here is uh this one right here the request body transformation so when requests come into lambda the lambda receives an event right it doesn't just receive your like post request or get request body um whatever you send through it receives an event and aws structures and does some fancy things with that event object so using the rest api gives us like a nice transformation out of the box that our lambda knows how to work with so that's why we're using the rest api if you don't know why you would choose http i'd recommend just stick with rest okay so let's select this one to the left here we don't want to choose the one on the right this is for private like internal networks if you have like um internal services behind like a vpc basically then you you would use that but we're talking with the external world right we want to open this up to the public so we choose this rest api here we'll do build and yeah we can just hit ok so there's a couple ways of doing it if you happen to be using swagger or open api 3 you can actually import your swagger spec right here if you have that set up for your api for us we'll just choose new api let's give it we'll do that and and here we just do regional that's fine and we'll hit create api okay so we've set up our api here and now the first screen we see is these methods so these are basically the endpoints that your api exposes so you set up a specific endpoint you could do slash api slash i don't know test whatever you want to do and that route would be um that route could be routed to an individual lambda so you could have a bunch of different endpoints here and you could have lambdas for each one if you'd like for us let's go ahead and do create method and we'll do any so if you want you can choose specific endpoints with specific methods maybe you just want to handle git for this endpoint or post for this endpoint you can choose that for us we want all request types to come through so we'll hit any and let the check mark to create that okay so we're in the next stage of setting up here and we want to set up with a lambda function here there's a couple of different options maybe we'll cover those in later videos but you could have it go to an aws service you could route this to another service or vpc etc but we're going to do lambda and we want to do this proxy integration so remember what i talked about with mapping the request body to the lambda so this right here request will be proxy to lambda with request details available in the event so we'll have like the method that the request have what request has we'll have the body et cetera so we'll choose that land region usms2 and i don't have any lambda set up on this account so let's create a lambda function here and we'll do author from scratch and we'll do dev lambda yep and we'll choose our runtime we're using go so i'll select go architecture if you really want you can choose intel 86 or arm i'm just going to leave it with intel just standard and that's all we need to worry about here so we'll create and then we'll upload the code to this shortly so it'll take a little bit all right so we have our lambda set up here and let's go back to api gateway and let's see do we need to refresh we may need to refresh the page okay so we're back on the screen lambda function we want that proxy integration to map the request body over and we'll choose our function so dead lambda and this right here is the default timeout that you use for these requests for this api gateway it's 29 seconds um and keep in mind you don't have to use this you could set the timeout over on lambda lambda has some timeout limits and you can look into those and choose if you want a separate if you want like a longer timeout or a shorter timeout up to you i'm just going to leave this default timeout so let's save and it says here we're going to give our api gateway permission to invoke our lambda yeah that's fine okay so now we have our lambda set up so requests will come in and they're gonna invoke or trigger our lambda right here so the next thing we need to do is deploy this api gateway so we can go to actions and go to deploy api and then this is where we set up stages so for example a test version of your api can be deployed to a stage named beta so you probably have like a production stage or a beta stage let's do beta just like they said this is the beta stage okay and if you want any you know custom deployments what we did for this specific stage or this this code or this deployment you could put that in i don't really care about that let's hit deploy okay so now we see here invoke url so aws gives us this url right here and if we hit it if we click this and you see it's routed it it routes it to that stage slash beta so if we click this this will actually invoke our lambda we don't have any code deployed to our lambda so this wouldn't really work right now so the next step let's get our lambda setup and let's let's upload it and deploy it here let's hop back to vs code and um from the previous video we had a very basic lambda set up pretty much just had a logger um it had this init function here and it had the main so a nits for lambda is here and it gets run before main so basically if you have like a db if you're using like rds or some other database service you would set up the connection here and you would have like a global variable scope to it so for example to show how that works we're setting up a logger here and if you have any questions about why we're using uberzap or this specific logger check out the previous video in the series i'll link it in the description but pretty much what we need to do is set up our handler so let's do funk my handler and here um we'll pass in a context so it's events dot proxy request and the response is events dot api gateway proxy response and also an error okay so let's do response is going to be a new api gateway uh giveaway response here and here um is where we can set um all of the response values so the ones that we if you want headers you can do that we don't i don't really care about those let's get rid of those i don't care about that so our body can be anything this could be an encoded json payload and that's what we'll end up doing here but the status code for now is http status um we'll do status okay and then we just want to return the response right here that's what i'll do there and let's do my handler we'll pass in my handler as the the functions handler so with the event coming in let's say you have multiple endpoints for your lambda um we can get we can access those with event dot path um so you could set this up to check if the event path is i don't know whatever and then you could handle that specific event path so you could say if event.path is hello we can set this up we can set our response up to return you know like hello world else you can have like a default response here that could uh return something else so if we want to do that you could say this we could put a response up here and we can set the type to be a events.api gateway let's copy this over and then in here we can say we can set the response to be whatever type we'd like so response in this case if it's hello it's hello world otherwise we can do here default end point and what's our error here all right so we fixed that just had the wrong type okay so we have a handler for slash hello if the path is slash hello we also have a handler for just a default handler let's log the request that we receive as well so for all of our endpoints doesn't matter what the path is let's log it and we're going to log the body so this is that json body so whatever your request sends over if it's like a post request or a put if there's a body it's going to be logged here so let's say here let's lock the body and let's also log the path we got let's do the method too sure we have an extra o and it's http method okay all right so this looks good let's go ahead and build this and deploy this here so from our previous video we have this little generate function and this this compiles and builds our executable our go executable and it also zips it up so we can upload it uh through the console so let's do generate function okay so we've generated that and we've put it out to main let's go back to our lambda okay so we want to edit this our handler isn't called hello it's called main that's what we zip the file as let's do main let's save and then let's upload our code from a zip file and i'm gonna upload this here all right so here's our function.zip and let's hit save okay so we uploaded our zip lambda is all set up so now we should actually be able to invoke this endpoint here so i'm going to do this with a post request so let's hop over to vs code and i'm going to go ahead and type out our post okay so i have um this post request here so i'm putting in a header just content type application json methods uh post the request method is post and then i'm just passing in some sample just some random sample data here that i found online um and let's make that request okay so i did jq to pretty print like a json response we don't have a json response yet so we got this little and valid numeric but here we see we got the default endpoint and that's what we're expecting right if the path isn't hello we're going to see this default endpoint so if we do slash hello we see that we get this missing authentication token so there's one piece that i forgot to set up here so let's hop back to our api gateway so this we need to be very precise on how we specify these resources so this will only handle how we have it set up now is only going to handle this default endpoint we can't do like slash hello or slash you know api pets whatever we need to set up a new kind of sub resource so we can hit create resource here and what we need to do is create a proxy resource so this is saying any path is going to get handled by this resource here so if you want to be really strict you can set up individual resources we could have a base resource you could have a slash hello resource pets whatever you want if you don't want to have your api users just calling any endpoint you could do that what we're going to do is set up this proxy resource so we can handle any endpoint we'd like so we're going to call it proxy and this is how we're going to call it here so you can add path parameters using brackets anything you'd like right here so let's go ahead and hit create resource and now let's yeah let's set it up so let's set up this proxy resource here and we'll do lambda function proxy us 2 and let's point it to our same lambda and default timeout's fine yep we'll give permissions so we set up the resource now we need to deploy it so anytime you make changes to this gateway you got to redeploy it so we'll say beta and we'll hit deploy okay so now we have this set up we've got this deployment set up and it handles the base slash endpoint and it also proxies any other endpoints through as well so let's come back let's clear this and let's rerun that request so now we see on the slash hello endpoint we get hello world hello world is sent right back and if we do some other endpoint we get this uh this default endpoint right here okay so there's like a really basic api um using lambda one last thing we'll do i'll show you how to set up just a json response a json response so instead of putting like hello world on the body you could have you know whatever data you want to return if you set up like rds or you're using a database to make some queries um you could format the response and return that uh right here so let's go ahead and do that so we'll say body and we'll just also there's going to be an error that we don't care about we'll say json.marshall so here we're just going to marshal some struct um so we'll create that so we'll give it a status and a message here and all we need to do is say we'll make a new default response and let's build it out status status okay all right and let's bring this in we'll have a different body if it is this endpoint this path we'll have that hello world body otherwise we're going to have this default default path and we just need to cast that to a string okay so let's generate this again generate our function and let's upload it to our lambda here upload from zip file and i'll select my file there we go and let's save it okay so we updated the function um now let's try that one more time all right so now we actually get um some json back and i think we can use jq again yeah so there's um there's our status returns and our default path that gets returned to so there is that's kind of how you set up just a basic lambda api so this is how you can do it you could use a switch statement if you don't want to have a bunch of ifs like this like slash hello pets whatever you could use a switch statement that handles your your different paths or you could set up separate lambdas for each one of those right we could do that and then in the gateway and this gateway here we could we could set the resource to trigger whatever individual lambda we want right remember with these resources we're triggering um we're triggering um a specific lambda right we chose this dev lambda but maybe you want to have like a slash hello lambda or a slash pets lambda so you could create lambdas for each one of your endpoints there and just have a single lambda handle a single endpoint or you could do what we're doing here which is a little bit more messy but it gets the job done where you have a single lambda that handles a bunch of midpoints yep so there is a quick run through there um hope you found this helpful hope you enjoyed this make sure to like comment and subscribe to the channel really helps me out helps the channel grow gets more exposure and lets me know i should keep making these videos and lets me know that you're finding them useful uh thanks for watching have a good one bye
Info
Channel: devtopics
Views: 3,039
Rating: undefined out of 5
Keywords: microservice, golang, microservices, heroku, api, backend, software, docker, docker-compose, docker compose, deployment, deploy, postgresql, postgres, go, aws, lambda
Id: WFLoP0vxfx8
Channel Id: undefined
Length: 19min 54sec (1194 seconds)
Published: Sat Jan 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.