Hi everybody in this video I'm gonna show you how to locally debug your
lambda functions using VS code. Up until this point I've given tutorials using
the UI in AWS console but you don't need to keep doing this for beginners
this was just to learn the basics of AWS but as you move further on in your AWS
journey you'll realize it's nice to test your code before you deploy it. So let's
get started here I have an empty folder in Visual Studio code opened it's called
LocalDebugAWSLambda because that's what we're doing. The first thing we need
to do is install the AWS toolkit, I have this installed it's right here and you
can see the little icon but if you don't have it installed go to your extensions
and type in AWS and there's the AWS toolkit and you can click install it
will show install instead of uninstall right here the next thing that you need
to do is have the SAM CLI installed you can check if you have SAM CLI already
set up on the windows it's ctrl shift P which opens the command prompt or
command shift P on a Mac so if you open the command prompt you can type in AWS
detect SAM CLI you can detective it's there and it'll show that I don't have
any settings necessary to change but if you do not have it setup you will get a
prompt to install the SAM CLI and this will bring you to AWS is walkthrough to
install the AWS Sam CLI choose the version for your operating system and
walk through it so in my case I was on Windows. You do not need to do step one
and two for the local debugging right now, but you need to install docker. We
need to have docker installed in order to debug locally. This is because when
debugging a lambda function the lambda environment is simulated by spinning up
a docker container. So make sure that you install docker as well also make sure
that you have visualization enabled in your BIOS for docker, and if you're using
a Windows machine you also need to enable file sharing on docker
after you install docker you would go to your settings and then if you go under
resources file-sharing add the drive so in my case I need to make sure that they
can access the F Drive and I just gave it to everything on my drive this is a
non production machine so that's why I'm ok with giving everything but if you're
doing this on a more production oriented computer you would not give a blanket at
everything on the drive so keep that in mind we're doing this all local
debugging we're not too worried about those securities but when you are
installing docker on a production server you're not going to do this so once you
have installed the SAM CLI and you've installed docker make sure that you
enable file sharing if you're on Windows 10 and then come back here so before we
just get started let's check that SAM our SAM CLI is enabled and docker is
also available so go to our terminal and we can type in sam - - version and you
should get back the SAM version so in this case I have 0.53.0 so this is the
SAM CLI version we know it's installed the other thing we want to do is docker
- - version and our docker version is available there now that we have this
setup let's create a new Sam application let's start our command palette and we
are going to rights AWS create new SAM application and now
it's gonna ask us the runtime version we're going to do node.js and we're going
to have a simple SAM app and this is gonna be the hello world SAM app and
it's going to go inside my local debug AWS you could select a different folder
and then it will be added to your vs code workspace and now our app has been
created and as you can see this is template . YAML and inside the YAML we
have our hello world function and it has an API set up for us so here's the types
AWS serverless function its properties the URI
what the handler is the runtime environment we chose nodejs and then
here is our API setup with its path to be hello and the method is okay this
will actually create an API gateway for us and tie that gateway to the lambda
function so now let's look at the code so if you expand hello world this is the
app.js code now I'm being highlighted with a couple of problems because I have
jslint on my machine so the linter is going through and letting me know
that there are certain things that it doesn't like you might not see the
jslinter if you do not have that installed but right above our exports
dot lambda handler we have run locally debug locally and configure this is
actually AWS integrating within Visual Studio code and it gives us the ability
to be able to debug locally but before we can start we need to actually
configure our tests so let's click configure so when you click configure
there's gonna be a new folder created dot called .aws inside that it's going
to generate a templates.json so this is a template for our template dot
YAML it has the handler and then our lambda function and now what we need
to generate is the event so that it looks like an API event basically what
we're going to do is invoke lambda with an event source this is our event source
but right now our event is empty so we need to use the SAM CLI to generate the
event so in the terminal let's type sam and then what we want to do is do a
local generate event we're generating an event we're generating a local event
from API gateway using AWS - proxy so this out prints to our terminal
something that you can copy now it's a lot to copy so I'm going to show you a
little trick here if we hit our up arrow so this is the
same thing that we did earlier if you do a pipe now I am on Windows so on Windows
what I'm going to type in is a clip so this means that it's going to copy to my
clipboard if you are on a Mac you would do pipe pbcopy if you're on Linux pipe
xclip so now I'm going to generate that event and it's copied to my clipboard so
I am going to paste over this event I don't need these little brackets pasted
this is my new event if I want to I can reformat this file because it's now kind
of went a little crazy right-click format changes and use all the shortcut
was shift alt f so now we have an event that looks like something that would
come from AWS API gateway to our lambda function
so now we can debug our lambda function locally so I'm gonna save this template
and going back to my app.js so this case what we want to do is debug our
lambda function locally we can add a breakpoint so when you add a breakpoint
you go behind the line number and that's how you can add the breakpoint and now
let's click the bug locally so a docker environment is going to be spun up like
I mentioned earlier to simulate the lambda environment if you get errors when
you're starting out this command and you have installed docker you probably have
not enabled sharing if you're on your Windows machine so make sure that you
make sure that your sharing is enabled so continue through we land it on our
response equals and you can investigate your your variable so if I hover over
event you can see this is the event that is coming in this is actually be
generated event any of our headers and our path and our resources so this helps
us now be able to debug locally so congratulations you learned how to debug
your own AWS lambda functions locally happy debugging if you liked what you
saw please subscribe to my channel for more
AWS tutorials and other tutorials as well