Golang Microservices - How to Make a Golang Lambda Function

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right welcome back everybody today we're going to talk about one of my favorite use cases for golang that basically the way that I use golang Link the most on a day-to-day basis and that is lambdas if you don't know what lambdas are they are a serverless microservice other buzzword function through AWS that allows you to basically modularize your code and run serverless functions which are basically functions that run in a way that is not directly tied to a VPS or any other kind of server so where you would normally have a function or a script run on a VPS through digitalocean AWS or on your own local PC a serverless function is not necessarily tied to any sort of infrastructure AWS basically has like a Handler that runs functions according to different triggers we're not going to talk about like a lot of the intricacies behind AWS lambdas we're just going to show how to set them up on AWS and we'll go into some of the like specifics later on so I've already written this code out mainly because I already released a Blog on this subject if you want to read over the blog I'll leave a link in the description for the video and you can read it over there but let's do a quick walkthrough of the code Let's ignore this comment right here at the top and let's walk through the way the function works so there are probably better ways to do this the way that I typically do it is I will have a main function and then that main function's purpose is essentially either to set things up or to go ahead and start up the main Lambda function this main Lambda function right here basically the purpose of it is going to be to print a specific message and to return a timestamp so that timestamp is just going to be the epoch timestamp it's going to take an input this input is going to be automatically parsed into a specific structure that we've defined here so when we test the Lambda itself on AWS you'll see that you know we can only pass it in you know in this type of Json structure so it's going to parse this out of Json and into our struct it's going to calculate timestamp it's going to print that time stamp out print the ID and the input message and then it's going to return the timestamp itself so I've already deployed it we're going to kind of skip forward a little bit I've deployed it here on AWS and I've already created a test blob right well I guess I haven't I was supposed to so let's go ahead and create the test blob here we are using message and ID so message like And subscribe then ID 69420. so let's go ahead and test this out make sure that it actually works correctly hopefully it will so we've got our time stamp here and if we can look in the logs it's not printed properly but it does show our timestamp our ID and our message so it is technically working correctly now let's walk through the laziest possible way you could do devops on here I'll zoom in a little bit so if you check out this comment right here this is basically how I do all of my golang functions I set up what is essentially a one-liner script that is going to build it zip it up which is what AWS requires unfortunately and deploy it so we can see right here we are building it out to main.xe we are building with go build we're setting the operating system the target operating system in these environment variables so go OS and go Arch we are using Linux and AMD 64. those need to match your configuration on AWS so let's see it should show it down here so we've got our architecture here and I think it's just Linux by default I'm not entirely sure where that said but I think it by default runs on Linux so we're setting up our operating system and our architecture here and we are building out to dot slash um temp main so if you look right here we've got our main.xe that's what we're creating in this first part right here then we are going to zip it now on Linux and Mac OS you can do this with a normal command line like Zip command with Windows because it's a shed operating system you have to download and install 7zip and throw sevenzep into your path so if you're following along at home that is how you're going to have to do that so basically we're zipping up the main.xe into a main.zip now this is the part where things get a little bit complicated you're going to have to set up the AWS CLI on your own that's where you're going to put in all of your secrets your username and you know essentially authenticate to AWS once you've done that all of that authentication information is stored in a specific area on your PC so you want to make sure that a that is somehow protected and B you never show that to anybody so when I run this command it's not going to actually show you anything particularly interesting so I'm using the AWS CLI I'm going to run this on the Lambda service and I'm telling it hey I want to update a functions code right there so you're going to have to create this function via the GUI or via the CLI I usually do it via the GUI and you're going to need to remember the name obviously function Dash name I'm using test function for blog that needs to match this right here so make sure that your names match and I'm specifying a region because I'm using a non-default region because I didn't want to show you all the other lambdas that I've been developing and you are specifying the zip file right here so this is telling it hey we want to upload this ZIP now what is happening behind the scenes is this ZIP file is being uploaded to S3 and then the Lambda function is going to pull from that zip file so that's that's essentially how everything works behind the scenes so if we go ahead and run this let's hope this actually works because it's a pain in the ass to debug it all right so we zipped everything up properly I think it's going through the upload there all right if you get a giant um a giant Json blob that's what you're looking for here quit and the best way to make sure that this did actually update refresh it and you should see last modified 16 seconds ago we can go ahead and run the test again let's hope it did not save our test so let's do message like And subscribe ID 69 420. let's run that test and we get success and it's all good okay so really I mean on its at its base that's it um you know there's there's lots of other things that we're going to end up covering later on but functionally because golang parses things the way that it does and because the Lambda library is actually pretty good for golang um the most simple version of a golang Lambda is pretty simple um you know when you're setting up these lambdas you got to make sure to pick the right run time obviously run time is going to be go 1.x and you have to change the Handler so make sure you change the Handler over to main or whatever your main function Handler is I changed mine to main because I want to run main first um so really yeah that's that's about it there's not too much else to cover on its base but I'm going to be uploading some more like I guess complex iterations of how to do golang lambdas later on because they're just super powerful but yeah if you liked it like it take it easy peace
Info
Channel: Mitch Edwards (@valhalla_dev)
Views: 2,319
Rating: undefined out of 5
Keywords: aws, aws lambda, golang, aws tutorial, aws cloud, aws training, golang lambda functions, aws lambda golang, how to set up golang lambda functions
Id: 2wGKtdc2rKw
Channel Id: undefined
Length: 8min 15sec (495 seconds)
Published: Mon May 29 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.