GO without SERVERS? How to Deploy Go to Lambda

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to show you how you can take go code and have it bundled up and zip using AWS cdk to then deploy it to a serverless AWS Lambda function all in this video you see companies like for sale at SST recently have really made serverless all the rage I mean everyone has heard of serverless and well rest in peace servers that's how it works right servers are dead because it's serverless right if you've been following along on the channel you know we really like go we really like go it's it's such a good language but one thing that makes it really great is the Simplicity of how to use it in serverless functions like AWS Lambda and AWS is the biggest market share for cloud providers so might as well use it because so many big companies use AWS go code AWS put it together serverless function in one video let's just get straight into the code all right so you can see here I have an empty directory the first thing we're going to do is cdk initial lies a type script project and if you've never used cdk well think of it as infrastructure as code and if you are interested in more about this let me know in the comments section below and I can describe what cdk is or what is infrastructure as code as a whole all right now once that is done go ahead and boot up your favorite IDE I use vs code against sorry Prime sorry teach for many of them user here and you can explore this new directory that you have and you can see here under lib we have this new TS file for AWS Lambda go stack and as this comment says the code that defines your stack goes here and what they mean is if you have a bucket they're going to use you can Define it here but in our case this sections we're going to be defining our Lambda our Gateway and our permissions and roles to deploy our go code to the Lambda and into the cloud so the first thing you want to do is actually make a new directory called lambdas and in CD into the directory and then we're going to make a new file called our main dot go file and next we're going to do a go mod in it you can call this anything you want I'm going to call this AWS Lambda go and then go mod tidy so now we have our go mod file and our main.go file now like I said earlier this is going to be entirely a video about Lambda so the first thing we need to actually get is go get the Lambda package from AWS that allows us to pull in types and different functionalities and Method calls that makes it easier for us to Define our code to play nicely with AWS Lambda so there you can see added it and you can double check that by going into our directory go mod you can see here we have our package perfect okay now that you define your package made go ahead and put an app struck and this is going to have just one field called ID now this part isn't necessary you don't need to have an abstract I personally like to have it because it just makes handling the method calls on my main dot go and my Handler a lot easier to do especially if you can add more functionality down the road all right so now let's define a new function called new app which can take an ID of string it's going to turn an app I'm going to do return capacity pointer reference to app or open this up and do ID is going to be ID Perfect all right next I quickly Define all the inputs that we're going to need which is the built-in hdb stand Library strings encoding Json and then that new AWS Lambda go package which we installed previously in our go dot mod file all right so I'm going to skip a little bit ahead and actually create a funk Main and here is we're going to start our actual Lambda handling but first we need to declare an ID so I'm going to do ID equals sum random string and then we're going to put app is going to be new app past and ID now it's going to complain that's not used which is fine and then here we're gonna do Lambda Lambda Dash start and this is we're going to pass in the actual Handler so here I'm going to Define funk app I'm going to do Handler enter intake nothing and for to return an error for now return nil and here we're gonna populate it with app dot Handler cool so this is a very bare bone implementation let's actually modify our Handler function okay so the first thing that I want to modify is actually the event so it's going to be events dot API Gateway proxy request and our return is no longer just gonna be an error but it's gonna be events dot API Gateway proxy response and an error like so all right let's first create our response body which is going to be a map that's going to take string string and now we're going to use the built-in standard Json library that go has to offer so I'm going to response Json and our error we're going to do uh Json dot Marshall passing response body and if error does not equal to nil we're going to handle this I'm going to return uh from this so I'm going to return events dot API Gateway products response open it up we're going to put our status code AS HTTP that is internal server error we're going to put our headers as a map of string string content type applications.json and then the body error internal server error put a comma there and then here put no perfect so we handle that error now we're going to do response is going to be basically the exact same thing events.api Gateway proxy response we're going to open that up I'm going to put the body as strings response Json our status code is going to be HTTP status okay all right and now we're going to pass our happy path we're going to Define response and that's going to be again the events.api Gateway proxy response we're going to open it up I'm going to do body it's gonna be string response Json we're going to do status code AS http status okay our headers are going to be the exact same with a bit of a difference here so I pasted a bunch of these headers because I didn't want to type them out all one by one but essentially these are what's needed for the most dreaded thing of all cores now we're going to get into chords we need to find our Gateway but remember this is just necessary for handling and not getting an issue when you deploy this and then you have like a react front-end trying to curl it or trying to fetch it and you get that dreaded chorus error message I I hate I've been pulling my hair out for that like trust me add these into your header type and remember if you're gonna do anything production make sure you clamp this Access Control allow origin right now it's wildcard so anything could be allowed but when you run something in production makes you control who can access your API all right so now we're going to switch gears into the actual cdk code of our application because believe it or not the actual go coat is already done pretty easy but you can expand on it the point is that you can get something running pretty fast and deploy and then you can craft it however you want make it as complicated as you really want it to be all right so in that stock AWS land to go stack.ts file I removed the comments and I've also added two new Imports the first import is the Lambda import from the cdk library and the second import is the rest API and Lambda integration constructs from the avws Gateway Library also part of the cdk library we need the gateway to be a layer on top between all incoming calls into our Lambda and then we need to actually Define a Lambda function and point it to where it can get the main.goal code from outlander's directory all right so let's go ahead and Define our const my function that's going to be a new Lambda dot function and we're going to put pass it in this my Lambda open this up and we're gonna pass a few parameters here so the first one is code and it's going to be where the Lambda is going to bundle our go coach we're going to put Lambda dot code Dot from asset and this is going to take a string and this string is going to be the name of the root folder of where our main.go file is so remember I create my root folder as lambdas and that has my main.go file so I'm going to do the same here next we're going to need our Handler and this is going to be the main the name of the folder so it's gonna be called Main and then it needs one more argument which is the runtime and this is going to tell us well what kind of code is is it python code node code or go code so you can do Lambda runtime and then put go 1.x because that's the version go we have and there you go you have declared a Lambda function but it's not doing anything just yet the next thing that we're going to do is actually build our API Gateway which again is a layer between all coming client calls and our Lambda function but also remember we need to make sure that it allows cores just in case you want to actually call this use of like react or something on your local machine or even a deployed machine so here we're gonna do const Gateway is new rest API I'm going to call this call it my Gateway and following the same pattern it's going to take a few arguments that are needed so the main one is this default course pre-flight options and this is absolutely critical I'm going to put allow Origins and that's going to take an array and then allow methods it's gonna be the same thing it's going to be a list and it's going to take get post options just just throw them all down delete and put there you go now we have our Gateway and you may be thinking oh well that's easy I have my function my Gateway I'm happy wrong because you need to tell your gateway what route will the code that you just throw in your main.go file actually run on so the next thing we need to do is actually bind our API to our Lambda and then Define the route for the functionality all right so next thing we're going to do cons integration is going to be new Lambda integration I'm going to pass it in my Lambda function that you have on line 13. next we're going to do the name of your Handler so we named our Handler it's gonna be a test line so you can do test resource Gateway dot root dot add resource and then this is going to be the path of where people can curl your particular main.go file and I'm going to put test here meaning that once we deploy this we will get back our Gateway URL and at the end of that we can append slash test and that is going to hit the particular Lambda function that we defined up here and well to get more specific the main.goal file that we Define with the current single Handler and this is where we can Define multiple different routes and you can split up as multiple different lambdas handling different routes or one giant Lambda that can differentiate between routes that's an architectural decision that you can decide and let me know which one you decide in the comments section below well and then the last thing we need to do is test resource dot add method we need to tell it what kind of method can we expect this so this is going to be a simple get and I'm going to pass it in our integration perfect and there you go now we have a cdk stack that's going to deploy our Lambda function and it's going to deploy a API Gateway and bind it all together now there's one thing that we forgot to mention so you can see here under my lambda's directory I have a go mod a go dot sum and a main.go but that's really not going to work because you need to actually build your main.go file into a binary and that is what is going to be zipped from our function into our Lambda and now you can see we have our main binary file all that's left to do is you have your cdk code you have your go code next you want to do is deploy it so hit it with the cdk deploy and watch the magic you can see here so you know this may be secret related I'm going to click yes always put yes always put that y never put End come on don't check don't validate just send it perfect so you can see here we've had a few things deployed and you can see this address so this is the actual address for our Gateway but let's hop into the AWS console and see really what we just deployed okay so now under AWS under cloud formation you can see here AWS Lambda go stack oh it's so pretty you can click it you can see all the resources and events that were created you can have a Lambda our Gateway all the cdk metadata so everything's working you can actually check it out into lamb that you can go hop into Lambda go here and there is eight of us Lambda go stack my Lambda using runtime go zipped last modified three minutes ago but can we curl it does it work all right so back in the terminal you can see here we're gonna do a curl a get request to the address of our API Gateway now if you somehow lost it don't worry you can get this address back on the AWS console go under API gateways and you'll find it there but one thing I want to just make sure you guys all understand is this is where we Define our get method and where we Define the path this test path here if we actually hop into our main.go file what we can expect to get is a response that says hi you have hit this route if everything works accordingly so go back let's hit this curl you literally went from zero to writing your first main.go file which is going to be Handler for other API functionality to then using it and deploying it to the AWS Cloud into a production ready environment so I hope you all enjoyed this video I know this may have been a bit of a long grid tutorial but there's a lot of aspects here that I wanted to break down and I know it was fairly simple but honestly this shows you the fundamentals that you need to really get your feet in the deep end and start running with some really cool stuff AWS is not going away and I'm more than confident to say whenever you land a job or you change jobs or even at your current job you're probably using some sort of cloud provider and I'll even double down and say the majority of the cases it's going to be AWS and if you're interested in using go but you want to do something that actually tests your knowledge and actually make an application then creating go code bundling it up and deploying it to AWS that's one of the best things you can add to your resume right now in my humble opinion so if you enjoyed this video make sure you comment like click and subscribe we're on our road to 10 000 YouTube subscribers it's going to be a fun one I can't wait for it to happen but I need to leave you guys off with two things so first thing is do you use AWS and did you like this video did you find it helpful did you like this tutorial do you like these kinds of videos I gotta know I'm hungry to know and second thing you guys already know right you know you got a palette
Info
Channel: Melkey
Views: 16,690
Rating: undefined out of 5
Keywords: video sharing, video, sharing, computer science, software engineer, silicon valley, computer programming, coding, learn to code, machine learning, artificial intelligence, cloud, python, javascript, how to code, Data scientist, AI developer, Machine Learning, go lang, go code, aws, aws lambda, serverless, deployment, infrastructure as code
Id: KyDWCyDqMLE
Channel Id: undefined
Length: 15min 47sec (947 seconds)
Published: Fri Sep 15 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.