AWS Lambda function are designed to run in the cloud, which is (by definition) - not your computer.
This fact is both a blessing and a curse - sometimes you want to be able to execute (and debug!) a lambda function locally, without having to redeploy it to cloud every time you'd like to test something.
In this lesson we're going to learn how to execute an AWS Lambda function on our machine using SAM CLI sam local invoke
command 🤩
Instructor: [0:00] We have a SAM application which was already deployed to AWS. As we can see over here, I'm able to execute this endpoint in order to get the "Hello, world" response. I would like to be able to modify this function, for instance, change the response and task this function locally, without having to deploy it to AWS every single time I want to test something new.
[0:18] In order to do that, we're going to run sam local invoke. We would have to provide the name of the function if we had more than one of them. Currently, there's only one, so that is not necessary. I'm going to press Enter. It is going to invoke my Lambda function locally on my machine.
[0:33] Bear in mind that you have to have Docker up and running in order to do that. The first time you're going to execute a Lambda function locally, it will need to pull the Docker container image, which is going to take some time. Nevertheless, we can see the updated result over here.
[0:45] There's one more way for you to do that. If you have the AWS Toolkit extension installed in your IDE of choice -- I have one for Visual Studio Code over here -- in your Lambda function, you can click on Run Locally button. That is going to run your function within your editor. You don't even have to go to the terminal. We can see the results over here.