I made my Lambda go faster - Go AWS Lambda Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to this brand new video where we are going to learn how to use go in AWS Lambda step by step so keep watching all right so first what you need is an AWS account I bet you already have that that's why you are watching this video so let's go to my terminal and you also need to make sure you have go uh go install so I'll open my terminal and if I do go dash dash uh version it's weird so yeah you just need to go install what ever go version you have it doesn't really matter make sure it's up to date all right and now what we need is we will create a folder then we will folder or a directory and we will then initialize our go module so first uh if we this TR directory and I'll create file called go AWS Lambda folder called gows Lambda and I'll CD into it and to initialize it I do simply go mod in it whatever name you want to put I'll put lws doio and here it has already created so if I do LS you will see this okay once you have it uh you can uh simply create a go file so I'll show you how to create a hello world in go so from my editor I'll open main do create a file called main.go so here simply you declare this function with uh Funk Main and uh whatever you want you will uh do it here okay so also before that like other uh you know strictly typed languages we need package uh main package main the error is gone and I'll zoom in a bit more so that you can see me and the code as well not just me okay and here I'll import fmt so it is formatting model I believe so uh I can do just print uh Len okay now I can type a message hello from creative J all right and to run this I'll again open the terminal and simply I can do G run dot or you can also put the name of the file like a go uh run run main.go okay it will give you the same output so we can't just put this in AWS Lambda and if you try to create this in AWS Lambda you will you will not be able to write go code directly because it's a compiled language so you'll have to first compile the code and then use it first let's go to the AWS GitHub repo so AWS Lambda go so here is the repo like this is AWS AWS Lambda go all right so so let me just start it quickly which I forgot to do and put it in the goang list and AWS list okay so first you'll have to install this so to do that I'll copy this and then I'll do go get and I'll put the link okay so github.com it will install it and now what we can do is we can import it and call this with lambda. start now uh I can uh have to create another function which will be the function which gets invoked by the Lambda so again uh Funk and like uh Handler I can say and here they have also defined the type is string and error whatever the return type is so they are not telling anything on the input but we do get an input and we will learn later how to do that so I'll uh put uh a string and error so if you are coming from JavaScript uh make sure you don't put uh colon or like comma or anything like that because you will get an error so you see missing return I mean that's a still the error so now we can simply return a string so whatever we were returning hello from loan AWS doio all right oh yeah we need to put comma uh it's the colon we don't want to put all right and this is done so now here we have to call Lambda is start so if I type Lambda it will automatically import so if you are using vs code make sure to install go dependencies it will automatically suggest you so I'll just call the start and then I'll pass the function which is Handler okay this is it and fmt they'll say it is imported and it is not used which is true so that that's why you know go is very useful I can just comment it out for now and if I run this file so what will happen is it will give me an error like the server Port Lambda runtime API these environment variable it looks for all right so we don't want to be bothered with this so now what we have to do is we have to compile this file to be run on Lambda so here uh we will be using uh amd64 so architecture I copied this command if you want to follow this guide you can follow it on my blog learny ws. iblog gows Lambda okay here uh you will see step by step same things which I am doing in this video and a more detailed guide you can take my course if you want to dive more all right so here uh I will just paste this and uh what it will do is it will will uh first let me just show you uh if it builds so I pass the so here I am just passing this uh Flags like I'm setting the envirment variable the go OS architecture like means OS is Linux and architecture is arm 64 which is AMD 64 and then we are running go build and the output file we are naming it as a bootstrap because Amazon Linux looks for the bootstrap binary file and it executes it and then we are saying which file to compile we want to compile main.go so I hit enter and it's compiled so if I do LS you will see this bootstrap file and if I want to check the size so you will see like this is the size of the bootstrap so now what we need to do is we still can't just upload this to AWS Lambda so we will have to zip this so I'll do zip Lambda Handler and and uh first whatever name you want to put you can put uh handler. ZIP as well uh anything works and whatever the name of the binary we created which was bootstrap all right so we have also gotten the zip so here you can see Lambda handler. zip now let's go ahead and deploy this and by creating a Lambda so I am in Ohio Us East 2 it is I think yes and I'll go in my Lambda because Us East one already has so many Lambda function I don't want you to see any of those okay I'll click on create function you can just go to functions as well and click on create function same thing and here I'll call it my go Lambda function and here in the runtime make sure you are selecting Amazon Linux 2023 okay and EM arm 64 like the EMD 64 thing which we did arm 64 is cheaper so that's why we are doing it and the performance benefit is not that much different compared to x86 the price which you'll be paying and in advanced setting we also want to enable function URL so that we can easily test our Lambda function and uh we also want to keep the O To None all right we don't care about this and make sure if you are setting o none anyone can invoke your Lambda infinitely and give you a $300 bill so you don't want to to be happening that so it's very unlikely but yeah don't leave your function URLs open like me all right I'll click on create function it's taking a while so in the meanwhile I will uh open this so that I can drag and drop it and here is my Firefox and here I go scroll my Lambda function is created here I click on zip file so here we simply drag this so I will drag it here the it has compressed so it will again uncompress it and then deploy it so all right so it's done and if you scroll here we also have a function URL here which we enabled when we are we were creating the Lambda so if I go here you will see a response uh oh it's internal server error hi guys turns out we did a pretty silly mistake so instead of amd64 we needed to keep like arm 64 so it's bit confusing AMD and it they don't call it x86 in go architecture so they call it amd64 that's bit weird but let's uh make it arm 64 and build it and we will again zip it again and here uh you go here and uh later if you want to learn how to automate this uh deployment process I'll uh show you later in some video or you can go through the blog and and here uh I go and click on again upload from zip and I'll put uh drag my zip again here and I'll click on Save we reload this page uh we should not be getting error anymore so yeah this is what we are getting and again this is like trying to pass the Json again Firefox because of the header which we are sending so we can fix that easily as well so here we are just getting this if I try to you know uh curl this request and it's pretty simple we get this hello from lews iio the message which we put here so if you want to send uh Json or set headers so you need to uh use the events from AWS Lambda go or if you want to also get the details of the events we get uh Contex so CTX and it is context uh dot context okay now after that uh we also need to pass the event so request here we will put the event from AWS uh repo which we already saw or did we so here you can here here you can see in the lamb uh event model you can see all these uh events which you can use with your go programming language and I will put event so here uh I'll have to get do github.com AWS AWS Lambda go and events okay and now I can use events Dot and here we want is API Gateway event API Gateway V2 HTTP request all right so now we will have all the details of the request and we can also set the a string instead of a string we want to send Json so we can do that as well uh events. API Gateway V2 HTTP response so now this will uh throw an error because it uh needs us to send the proper response to do that again like uh we do events. API Gateway V2 GTP response and inside this uh we put curly brace so here we do body and here now we say something whatever we want uh we will just uh take something from the environment variable and also we need a status code which we can do is 200 all right and we need to put comma everywhere in here because it's go and let's format this a bit so I'll move this thing here this thing here as well here so it's now a bit clean so now you can see the events and uh to get anything from the request so let's construct a body so uh body uh I do this uh this is how you define like colon equals to uh variable here I'll say your method is uh whatever method we send uh your method is uh I'll do so here we can use fmt again dot uh is Sprint F so this will format it's like C okay and I'll un delete this and here also I need to put comma and here uh I'll do percent s and I can get request do HTTP do method and I can also get the path from the request as well so request uh dot raw path all right and here uh what I want is like your method is this your path is again uh I'll put a percent s so now go will give us error that we are not using uh the body so it's very very strict now I'll put body here so this is done and also we are not sending Json so we can customize the headers if we want so uh what we do is um uh we don't write const anymore I do headers so again the type is map is string is string so so now we want a map which is a string of a string so we can say uh content Dash type is equals to text/plain and again we got to put comma and here we got to put header and again put comma so let's uh deploy this again and we do that by running the build [Music] command and we zip it and now we go to Firefox we go to Lambda console we go to upload from zip and back to square one we drop this sorry we drop the zip we don't drop the bootstrap okay and we save it and now if I go and reload this page I see this the method and the path so here if I change the path to be something else like slash hello there so it will show me hello there again like this is encoded you'll if you want to remove it you'll have to decode it right so if you want to send a post request which you can do is with HTTP so I'll copy this okay and put it here so you will see the method is post your path is this this you can use Postman or anything to test this out or even Cur so here if I do put they'll tell me it's a put request okay and path again hello sl12 312 3 so this is how you get the data you uh change the headers you set the status code you can also encode the Json here as well so to encode Json what you have to do is again you have to create a string map like this but instead of a string you keep it an interface uh let me show you how to send Json back so I'll uh duplicate this and I'll make it body so we don't need this anymore and uh instead of uh you can have this you know a string a string as well if you are just sending a string but that might become a problem later so you can make it an interface you can also use a struct but the problem will be is uh you will have to write in title case so that's the one thing I don't like like in go and here now if I say message and again I'll have to keep it a string message like hello the my fried friend fried fried no no no we don't want fried fried we want F friend and I'll say time so I can just import the time module time. now and I can do do format and I can do time. kitchen so it will just give me the you know Am Pm time of the server in UTC and now we have to encode it as well so con not const Jason body we write uh Jason and then uh Jason error so we do this and we import Json from encoding SLG and do we do Marshall so it encodes all the values and here we pass the body nothing else and uh what we do is we just pass the Json here instead of body uh okay okay it's in BTE so I think uh we can simply wrap this in a string uh okay there is this problem we can't keep uh same name Json body so I'll do a string Json body fine so Json error is declared so if there is er in passsing uh we will pass it to the Lambda Handler and Lambda will know that it is an error fine we get rid of fmt again so not a big deal again we do all these steps uh let's not do all these steps I'll show you how to you know do this with a make file uh okay I will not show you that anymore let's you'll learn that in my uh learn AWS course so yeah you got to check that out if you want to be an AWS expert so now what we do is we again compile so I go back here I go I do zip and let's uh upload it again upload from zip file and we drag the zip and we save it you'll notice one uh thing which we missed is we are sending text plane but we are returning the Json so if we do uh curl we should uh get this Json encoded thing in here so I think this is it this video is already uh getting very long so I hope you learned how to build lambdas with go and how to deploy them as well how to you can just connect this with any other AWS service like API Gateway all the events which I showed you here you can read it a detailed article on my blog and again like all the logs go here so if you do fmt print Len so you will see these here and all these events again CF cinis whatever it is even Lex is here S3 and all so you can just import them from the event and yeah so this is it this is how you receive and send a Json from AWS Lambda bye-bye I'll see you in the next video
Info
Channel: CreativeJE
Views: 416
Rating: undefined out of 5
Keywords: CreativeJE, Education, Technology
Id: oa2aKE2Himo
Channel Id: undefined
Length: 22min 39sec (1359 seconds)
Published: Wed Apr 10 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.