Developing AWS Lambda functions forces you most of the time to write, inspect and debug your code directly on the AWS Management Console. If you are in that position, but would like to completely switch to your VS Code IDE for the developing and testing process, this article can bring you one step further with introducing you the VS Code capabilities for debugging AWS Lambda functions.
So enjoy the article and don’t hesitate to leave a comment or reach out to me if you are facing some kind of issues!
Before starting with the actual configuration, make sure you meet the requirements. So install the following tools, which are also very useful for your overall cloud developer journey:
Start with opening VS Code and navigate to the extensions section on the left navigation bar. Then search for the extensions AWS Toolkit and install it.
A new AWS Toolkit icon will appear on the left pane. Click on it and log in to your AWS account with your credentials.
You should see a list of AWS services and all related instances you have set up. Navigate to Lambda, right-click on it, and click on “Create Lambda SAM Application.”
This will open a context menu. Select Python 3.8 > x8664 > AWS SAM Hello World, then choose a folder and give it a name.
Now open the previously created folder and open the file template.yml. On the section Resources, there should be a line visible called “AWS: Add Debug Configuration”. If you don’t see it, make sure Docker is running!
A new launch.json file should be created, which contains all your debugging settings. This is the right place to insert, for example, event inputs.
Open your app.py file inside helloworld and set a breakpoint on the return statement. Next, go to the Run and Debug section on the left navigation bar, make sure the new launch config is selected, which is called in my case “lambda-python-3.8:HelloWorldFunction”, and hit the start Debugging button. Your runtime should stop on the breakpoint now!
Deploy Lambda Function
So, you have set up everything locally, and you are able to debug the Lambda function. But there will be a time where you want to deploy your function to AWS. This can be easily done with the SAM CLI. Open your terminal and navigate to the folder that contains your code. Then type in the command:
sam deploy —guided
This will initiate some configuration questions which you can all accept and stay to the default suggestions. After that, you should see your Lambda function online in the AWS Management Console!
We explored how you can use VSCode to locally debug your AWS Lambda function before deploying it to AWS. With the AWS Toolkit for VSCode and the SAM Local debugger, you can test and debug your Lambda function quickly and efficiently, without the need for constant deployments to AWS. Happy coding! I invite you to follow my blog, where you can find new and useful guides and tutorials related to AWS.