Debugging AWS Lambda Locally: Step-by-Step Guide with AWS SAM CLI and VSCode (Part 1)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to another episode of tech talk with Eric today we're going to look at how to debug an AWS flame to function using uh the Sam CLI and vs code all this is going to happen local on your machine so no need to provision any resources in AWS so what do you need to get started you're going to need vs code python the AWS CLI the AWS Sam CLI and Docker you don't really need to know much about the AWS CLI Sam or Docker at this point those are tools that are just going to be needed within vs code to launch the debugger Docker is basically going to be used by the Sam CLI in order to load up the AWS Lambda function environment to allow you to inject your function into that environment and then walk through it locally so again you really don't need to know much about it but they need to be there in order for this to work so what are we going to do we're going to create a very simple Lambda function and then we're going to wire that up through the launch configuration inside of vs code and once that's ready and that's working correctly we're going to be able to basically launch our debug environment and let the land execution environment load our code and we can walk through it line by line it's a very nice tool to have if you're trying to figure out what's working and how it's working with inside of the Lambda execution environment essentially you're you're running what would normally be in AWS on your local machine okay uh so at this point we just want to fire on vs code dot I'm going to come in here and we're going to keep this very simple we're going to keep a very simple directory structure we're going to start off with just our source and we're going to create a single Lambda event handler here if you have fairly complex event handlers and structures and functions common packages that you're using and maybe using Lambda layers we're not going to cover that in this one because I do want to give a basic overview on how to wire this up that's going to be covered in two episodes from now in episode five we're going to look at Lambda packages and layers and how to wire all that up so that you can debug that locally as well but just so we can get our feet wet and to kind of do the crawl walk run we're going to start off very simple and we're going to have our app.py which is a standard naming convention for our Lambda function and then we're going to have our actual Lambda Handler and in here we have our two parameters we have our event parameter and our context parameter these are the kind of these are the main requirements that we have for our our Handler the event is going to contain the payload information that you send into it uh if it is being called by another resource such as API Gateway or sqs you're going to have some of that information in there as well with your event context we're going to go over that one in the next episode so we're going to kind of build on these here we're going to do some very basic very simple and then we'll take it to the next level for a more complex uh event patterns and payloads and then we'll get into the more complex directory structure by episode 5 and look at Lambda layers as well but for now let's just take a look at when we if we execute this we'll have our event and we'll do our event here go ahead and print this here again and this will be we'll print out what the context is and then we'll do a quick response um and we'll do the standard pattern of what our status code is and that'll be by default we're going to set this 200 that assumes everything went well and then what is the body of our message and we're just going to say hello from Lambda something simple which I believe is the kind of the full one that happens when you create a a Lambda function from the AWS console as well okay so that's all this is going to do it's going to print out the event it's going to print out the context and uh then it's going to create a response okay so that's pretty simple next thing we need to do is looking at how we do our launch configuration so there's a few ways we can do the launch configuration one is if you don't have one already you can come in over here to your debug you can say create a file create a Json launch configuration and then it will create that file you could say you know select the python file and then that way it's a it's kind of default configuration that allows you just to run any python file that you're actively working with inside the editor um if you don't have it already installed or if you don't have a launch configuration file already created again you can do it here you can also sometimes do it from here as well we're going to need to do two things to make sure that this shows up for you we're going to have to do our command prompt and then we're going to look for AWS Sam CLI and we're going to see if it detects the same CLI now if you don't see this information right now there's a couple things you need to do one is you need to install the AWS CLI you don't necessarily have to have the same CLI installed first but if we come in here and if you've got AWS version if we look here if this comes back and you're good and you see it but you still didn't see what I saw a moment ago you need to make sure that you install the extensions here so you just come over to your extensions you search for the AWS toolkit and you'll need to install that once that's there and it's it's part and it's also installed and it's enabled then you should from the command palette be able to see the AWS options here now one thing you can then do is say let's detect to see if the Sam CLI is available and it should come back depending if you've got the Sam seal installed it's going to say okay I see it we're good if not it should give you the option to go ahead and install it from here it's got some pretty cool built-in features for them if for some reason that does not work then you can follow the example repo that we have labeled as episode zero that shows you how to manually install all these tools okay so from that point let's say hoping fingers crossed that you've got everything there that you've got the AWS mcli and the awcli to where you can open up your command prompt again and you can come in here and you can say add Sam debug configuration uh sometimes this works for me and sometimes it doesn't and so uh and you know it's hard to say if it's gonna work for you or not but let's see what happens here I'm going to go ahead and select it it did not on this particular one um I don't know why that happens other than it just doesn't seem to want to play nice sometimes and I could literally open up another project and probably get this thing going try one more time to see if anything happens in here nope uh no I just maybe I did the detection there I'll try one more time and to add and it does not okay if it worked a wizard would pop up then it would ask uh what function do you want to link it to and it would have seen this it probably even said the line number and we'll just selected that and then it would ask what runtime language do I want and then I would probably you know select like python 3.10 or 3.9 and once that once you did those selections it should then open up the launch configuration file but if that doesn't work or if you already have a launch configuration file and that didn't work then you can come over here and you can say go ahead and create one and go ahead and just create the default python configuration file that means that any file that's a python file I can run this command against this debugger command and it would run that python file so it looks something like this this is not something the same oh this is just getting a configuration I'm going to come back over here and see if maybe now that I've got the configuration if it'll figure that out or not let's just try this again and if it doesn't we're going to show you how to manually set it up so create there we go add Sam to bug configuration nope don't know why so things you have to learn to live with yeah come over here I'm going to select my launch configuration I'm going to say add configuration and I'm going to say I've got a few options here for Sam I've got the API Gateway I've got a direct Lambda Handler and a template bass based uh Lambda invoke we're going to do this one later on as well as this one later on today we're focusing on the direct Lambda Handler so as soon as I selected that it um it created this for me and this is just basically the configuration to launch Mylanta Handler if I save this real quick it's going to show up over here on my list so I've got the current one that it already created now I've got this invoke Lambda Handler which is this guy right here so let's assume that my Lambda Handler I'm going to create it and my function is probably going to be at my login function okay so I'm just going to name it friendly here so I can easily find it and navigate to it over here when I want to launch it so that's the first part of it if you were to try to execute this now we should probably get an error so um you know we can just go ahead and see what happens here let's look I'm going to get some Sam errors oh it's actually trying to do the current file let's do which you can't run a current file to a Json Okay cool so we're getting some errors here it doesn't know what's going on one the first thing it says oh I don't know what your target run time is and so here's a list of the runtimes that are available um if you were actually able to run this from within the python file and it created this for you would have only listed python runtimes but since we're inside of a configuration it really doesn't know you know what file we're going to be pointing it to yet so we can just say we can select from the pythons that are available I'm going to select a 3.10 here and that goes right here in this runtime still need to configure these two here so but I'm going to see what happens if we trying to bug this now just so in case you get these same errors you can see and follow along don't get any error here but it looks like oh it is actually trying so it's trying to start the debugger right now should probably get some errors yeah so here we're going to get bad Handler function Handler not enough values to unpack it has no idea what this is because it's looking at this guy what we need to do is we need to map the Lambda Handler to an actual function and the Handler just here just needs to be the file name and whatever the Handler name is that's kind of the standard convention for this so again Lambda Handler needs to be app and then the function name so whatever you name this file name that this file name was Foo this would be Foo if your function name was bar this would be bar so if you had Foo and Bar in this case I'm following the standard naming convention I've got amp I've got Lambda Handler so I use the app we don't use the py extension and Lambda Handler next we need to say where is our project root this can be a little bit um deceiving because it's so project root is here but it really needs to know all the way down to the path of where this file exists so in our case it's in source so it's under our workspace folder and source all right so this is pretty much everything we need we also have a payload here that we can pass some information in and we have two options here for this payload we can either do a Json or a path we're going to Json just to start and we're going to show that you know what this looks like is just a basic Json now as you get a lot more Json going on and you start working with bigger payloads I like to use the path and I'll show you how to do that in just a few moments but in this case we're just going to say we're going to pass in again this is going to be a login so we're expecting a username and again if you've been following Along on my other ones you know I'm a big fan of Marvel so we're going to use Tony Stark and his password is going to be very little Tony Stark does not have a good password policy okay here we are this is basically what we need to do and so next thing we need to do is simply launch it and the moment of truth comes to see if we actually have this set up correctly what we want to do now before we do launch I want to just set a break point in here uh so we can launch it and debug it and see what's going on so that being said let's go ahead and kick this off we're going to set the login one that we have here and we'll just make sure we have it selected at the top here yep good to go and we'll debug now it does uh need to it's basically doing a cold start so it's going to kind of load that Lambda and or the docker environment if you get a Docker error here you might see that a Docker is not installed or started so you do need to have Docker up and running uh it you don't need to really know anything about Docker all this is going to be handled for you behind the scenes it just needs to be able to load that image run that container and then that's where it's pretty much going to be injecting your Lambda function Handler into that runtime environment and wiring those guys up for you okay so what we have over here we have our event that's coming in over here so those are the event values that we set up in our launch configuration you can also see some of that information over here too if you're used to these local variables and debuggers same with the context I think it's easier sometimes just to hover over here and find them so you can see we got a lot of cool information coming in we can now uh see what the request ID is uh if any Cognito information is being sent into it we can look at the function Arn where it's coming from hypothetical we have a very we have a fake account here if this is being sent to a law group we have this information the memory limit that's being set to on here right now so a lot of pretty cool things that are going on uh with inside here that we can now debug locally um so again right now very vanilla function I do have an event I have a context I have a status code and I'm just going to get this information I'm going to print it out oop and I went ahead and stepping one at a time I'm actually now inside of that uh the event engine uh Handler the request Handler so I don't really need to be in here I just want to show you that if you if you do step into this all this extra code um you can do that um but you you know if you really just want to focus on your code you just kind of get out of there but if you really want to see the understandings of how the Lambda event handler works you can kind of walk through that code yourself okay so what we did we printed out our event our context which we see down here the event and this information in it context set all this information in it and then we were able to uh you know respond back with our hello world and so pretty cool um now if we want we can do a little more information on that if we need to say you know get some of that information out of it so maybe we need to get our username out of it we can do our event and a get on it and uh we will pass in Let's see we need we have a username is the key and we'll maybe default that to none and then we can do the same thing with the password as well and these are just this is my variable name this is the name from my Jason payload so just to show you this they are different so if we have maybe to the PWD passes one but so say a reserved word in here so we can say password so again variable names here Json payload names here which will match with whatever I'm passing in down here so Json and the event this isn't giving us any intellisense because it doesn't you know it's just a parameter of event uh the event is a type of dictionary so if we want to give it a little bit of a uh syntact syntax help we can say it's going to be a dictionary and then this will light up yellow and vs code or whatever color that indicates um for you that it is actually a known uh function versus um just didn't know what it was before um so now we could you know attempt to say you know if we had a login function somewhere we could actually pass in the username and password let's say um and you know maybe it was a success would be something like you know if it worked or result whatever how you wanted to do that particular function and based on that then we could say okay they they did log in or they didn't log in in this case we're just going to say hello and we'll put in their username from Lambda so again we're going to keep this very simple just so you can see how this is wired up okay so now let's uh go ahead and we can test this out and we can rerun this again and again um mine's gonna it does the cold start does take a couple of seconds your first time doing this could take longer especially if it needs to download uh the image but uh you know just have the little patience grab some coffee okay so I printed out that now we're gonna get the username and password see if I did that correctly yep I got Tony Stark I got the password of one two three four Z and now I'm just going to spit out a response here and again this is a very very simple example and I just wanted to show you how to wire this up um so that way you can start playing around with debugging Lambda functions locally and you don't even need an AWS account you're not provisioning any resources all this is happening on your local machine um last thing I just want to show you that if you do start getting some more complex payloads uh you know such as this one's pretty simple but if they start getting pretty big pretty fast um just want to show you that you can offload those into say another um into a file so let's just do this and I'm going to copy the exact same thing and then I'm just going to say that this is Lambda login to for whatever reason uh lack of good naming conventions here I can copy this let's say you also reuse some of these same payloads you know in a test directory so this would be a good place where if you had something and you had tests going on and maybe you put in some payloads here and again we'll do a Tony Stark login or something like that so maybe just to show you that's a little bit different it's reading from the file we'll do added a little bit more to this login we have this still pointing to the same Handler we're just passing a different payload here um but we're not doing that yet we can do that now so again this right here they're the same we need to just change this to path oops path and it needs to just go to a specific path uh you can do like a you know dot test and then everything but uh depending on your environment Dot may or may not work the best so it's always good to grab the workspace folder and then you just follow that path so where's it's going to test payloads and then the Tony Stark login test payloads and spill then our Tony starklogin.json okay so assuming we did everything correct there we can come back make sure we got our break point yeah we got one here we're good our debugger should now have two Lambda functions that we can invoke so we got our normal one and we have our one with our new payload that is pointing to a file and again it takes a couple seconds for this this is basically a cold start every time we do this and we're coming in and we can look at our event and we have the pass one two three four Z which is the one we're loading in from the file uh so again just uh the reasons to do that is right now um this is super simple as far as the payload is concerned but in our next episode when we start looking at um API Gateway payloads maybe sqs payloads we're gonna see that they are really big and I just like trying to keep these configuration files as small as possible because imagine if you just had a ton of parameters in here granted you can come in and collapse these but they just get kind of unwieldy after a while if you have um too many Json payloads in here when you could otherwise reduce it by pointing to a file okay so again this is a keeping it simple and we're going to wrap this episode up with this hey thanks for watching uh hopefully you learned something new please like And subscribe that way you'll be notified when new episodes are dropped uh speaking of which our next episode we're going to take a little bit deeper dive into what we were looking at today now that you've got some fundamentals we'll take a look at a little more complexities in the launch configuration we'll also take a look at complex payloads that you can pass into your Lambda event handlers and how to parse that more complex payload so please like And subscribe and I will see you next time
Info
Channel: Tech Talk With Eric
Views: 2,826
Rating: undefined out of 5
Keywords:
Id: RDzFA8ssAws
Channel Id: undefined
Length: 22min 15sec (1335 seconds)
Published: Mon Jul 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.