Your First AWS Lambda Function with Go

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone today i'm going to show you how you can build your very first aws lambda function using go hey everyone my name is brian thanks for stopping by the channel in this video i'm going to show you how you can build your first aws lambda function and go this actually kicks off a the first video in a series that i plan to do creating an entire serverless api in aws using a combination of lambda functions api gateway and aws cognito for authentication today we're going to be building a lambda function using go before following this tutorial you should have a very basic understanding of go as well as go installed in your computer an aws account in order to upload your go code to a lambda function and i'll be developing on a windows box but you can you do just the same on mac and linux with very similar steps so uh let's hop into it okay so on my screen all i have is vs code open with an empty folder called hello world lambda that's we're pretty much gonna name our project um so the first thing we need to do is we need to initialize a new go module use the command go mod init and then we'll name this hello world lambda like that okay so we should have our go mod file uh set up for us which contains both the module name and the version of go we're using on this machine so the next thing i'm actually going to do is create a new go file uh we'll say main.go which is the entry point for these and one of the things that i generally like doing before i start a project is just creating a simple hello world function just to log log a message out so we're going to say package main for the main file and then we're going to create a func of main and all i'm going to do is log dots dot print ln and we'll just say hello world now when i save this it should automatically import the go package because i have go tooling set up on my machine okay so here we go let's go ahead and just run the file just to make sure we actually get something so go run main.go and we should get hello world printed out to the console which is good this is the starting point for our function okay so from here i'm actually going to expand my command line because we're going to install a specific we're going to install the go sdk for aws so in your command line type in go get iphone u to update any packages and it's going to github.com forward slash aws aws hyphen lambda hyphen go okay so we can see our product has been added if we go and look at our mod file we can see this has also been required inside the gomod file uh so i'm gonna actually close my command line because we don't need it quite yet and then we can start modifying our code here in order to uh turn it into a lambda function now throughout this series since we're going to be um since we're going to be creating this with kind of the foresight of using an api the specific event that we want to handle is an api uh gateway proxy request um so we're going to create a new function here and we're going to call this handler and this is going to take in a type of uh well first off request is the name of the parameter but the type is going to be events dot api gateway uh let's see proxy request and then we're going to need to return two things here so the first one is events dot api gateway proxy response and then we're also gonna have to turn return an error because the uh the handler the the lambda code that executes this handler expects us to give an error back if something does go wrong i'm gonna go ahead and minimize my uh side pane here so we have a little more room to work with and see um and then inside of here i'm just going to uh copy down my log file or my log entry down here and then we need to return um that uh that response so let's go ahead and create a new response i'm gonna say response equals do uh colon equals to create a new object here and we'll say events dot api gateway proxy response and the only thing we really need to return just that's pretty much required as a status code so we'll return we'll return a status code of 200 uh which in api world means okay and then we're going to return the response and then also we're just going to pass a nil for the errors because we're not really doing a whole lot here okay so i'm going to go ahead and save this sorry as i spelled response wrongs let's go ahead and correct that okay then inside of main all you really need to do is type in lambda.start and same thing if we save this it's going to automatically import the the uh reference for lambda here and then we'll pass in handler uh without any kind of braces or brackets or friends just like that so we're passing in the handler function uh to the the lambda code that starts it off and this essentially is a very basic lambda function what we can expect out of this is when this is executed up in aws it's going to print hello world out to the uh the console output which we'll be able to see when we test it and then it's going to return a status code of 200 which kind of sets us up for the future videos when we start actually tying api gateway incognito into this okay so before we can upload this up into uh into aws we need to do two things we first thing we need to do is we need to build our code into a compiled binary that aws can understand and then we need to zip it up because when you upload a lambda function up in aws provided you're not uh typing it into the co into the um the built-in editor in lambda you need to zip your files up and upload them that way so what we need to do to build this is let's see here we're going to go ahead and open our output and we need to set um because aws runs on linux you need to set the geo the go os environment variable to linux in order to build this otherwise it's going to try and build a windows executable by default so to do this on windows uh actually i guess we'll do it to do it on mac all you need to do is type in gos equals linux like that and then run your command if you are on windows uh since we're using powershell we need to type in dollar sign env colon gos and we're going to set that equal to a string of linux like that so now when go builds our binary we're going to build this uh for a linux operating system so go build main.go provide this functions we should receive no output but if i open up my side panel we're gonna see we have this main file with no extension on the end of it which is is exactly we need to see now in order to zip this up in windows i'm going to right click in reveal and file explorer right click the file and then we're going to send to compressed folder it's going to name it main we could leave it just like that not a big deal the main thing is if i open this up main thing you have your single file of this this main binary inside of here okay so now let's head up into aws and start setting up our function um i have aws open zoom in here a little bit and all i have is uh the console home listed uh now i recently was in lambda so it's it's listed as my recently visited services however if you uh don't have that there using the search bar up at the top you can just type in lambda and you should see this entry here so i'm going to click on lambda to go to my lambda functions you can see i have another a number of other functions in here but i'm going to go ahead and click this create function button we're going to author from scratch as far as our function name you can name this whatever you want but we're going to follow the hello world lambda convention we've set forward so hello world lambda now this is important the run time is what the only other thing here you need to change so we're going to change this to go version one and then click create function okay so on our function has been created i'm going to close this out just to give us as much space as we possibly can uh now if you scroll down into the uh code source section you'll see this button here that says upload from so i'm going to go ahead and select that and then zip file and click the upload button to upload our package uh now this is the same folder it just ironically seemed to pull that up from when writing the article so i'll go ahead and double click on this main zip file and click save the other thing you have to do here in order to configure lambda to look for your function correctly is by default uh it will set the handler to hello here so we need to edit this and we need to change this handler to main because that is the entry point for our code that refers to back in the function code itself this funk here so when lambda starts when when land when aws needs to execute this function it's going to fire off the uh the main function which is part of our our package that we've put in okay so i'm going to go ahead and minimize that and we'll click save just to save that and now let's test this let's head over into the test tab and uh we can we can leave this everything as is you don't need to modify any of this we will get into modifying this and to test out some inputs and outputs later but we can leave this as is go ahead and click test and provided everything worked successfully you should get this execution result succeeded so now if i drop down the details you can see we received a status code of 200 and everything else is pretty much empty we didn't pass anything back uh however i scrolled down a little bit more you can see the log output here is that hello world statement that was actually executed up inside of aws uh that we wrote inside of our code uh so this is a very very uh basic uh uh tutorial on how to create lambda functions for go obviously uh the more you get involved with different things in aws and go the more kind of cool stuff you can do with lambdas and uh there you go that's how you create a very basic uh lambda function using go if you like this kind of content do me a favor uh like the video share it with your friends and subscribe to the channel uh if you want to get in touch with me the best way to do so is on twitter i'm at brian m and dev my dms are open so go ahead and hit me up there and if you want to do uh have some more code discussion learn buildteach.com so you can access the discord server that i'm generally active in and look forward to seeing the next video have a good one bye
Info
Channel: Brian Morrison
Views: 16,281
Rating: undefined out of 5
Keywords: azure, aws, cloud, nodejs, dotnet, developer, go, golang, go aws, Golang aws, go serverless, Golang serverless, go serverless aws, Golang serverless aws, go lambda function, lambda beginners, lambda tutorial, aws tutorial, go aws tutorial, how to create a lambda with go, how to create a lambda with golang, Golang first lambda, Golang lambda beginners, beginner tutorial lambda, first lambda function go, serverless function go, serverless function golang
Id: 13rnse0zYK8
Channel Id: undefined
Length: 9min 26sec (566 seconds)
Published: Tue Mar 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.