
How To Debug AWS Lambda Python Functions

Debugging is a lifesaver for Software Developers especially encountering a bug or peculiarities. Deprivation of such a great feature for AWS Lambda applications is one of the biggest problems. In this blog, we are going to explain step-by-step how to overcome this by using ServerlessDebugger's VSCode Debugger Extension.
Installing the ServerlessDebugger
After opening the project by Vscode, open the Extension panel from the left side and then type “ServerlessDebugger” onto the search bar and install it.

Getting the ServerlessDebugger's AWS Lambda Auth Key
To configure the ServerlessDebugger, we need to get its AWS Lambda Auth Key. Go to ServerlessDebugger Login Page and login or Sign Up.

After logging into your ServerlessDebugger account, you can get your ServerlessDebugger API key from the screen shown below. Just copy your API key from the modal.

Opening Command Palette and Configuring the ServerlessDebugger
From the Left Bottom side of VSCode, Click the settings gear icon and then, click the “Command Palette…” located top of the popped-up model. After clicking the “Command Palette…”, a modal will pop up at the top-center of VSCode. Typing “ServerlessDebugger: Edit configuration” and press Enter.

After entering the “ServerlessDebugger: Edit configuration”, a tab like below should open. Type or Paste your auth key that is gathered by following the before step to the “<your_auth_token>”.

Configure Your Serverless Yml For Debugging
In order to debug your Lambda, you should integrate the ServerlessDebugger Python agent into your lambda environment. There are multiple ways to do this. In this section, I will just try to explain the serverless yml configuration. If you would like to take a look at other options, check out this link.
Step 1: Get Your API KEY from ServerlessDebugger:
Go to ServerlessDebugger Website and log in. Then, get your debug token from the opening page.

Step 2: Install ServerlessDebugger Plugin to Automatically Wrap Your Functions:
Before running the following instruction, Please make sure you have a package.json file. If not, first make “npm init” to create it.
Step 3: Add ServerlessDebugger Plugin in serverless.yml File and Add ‘ServerlessDebugger’ component to custom:

Step 4: Add the serverlessdebugger_apiKey to Environment Variables under the Provider Section in serverless.yml:

Step 5: Add ptvsd layer to lambda that you would like to debug locally as shown below:
- ${region} => your aws lambda region like eu-west-1, us-east-1 etc.
- Check your timeout value. You might want to increase it for debugging.

Open Your Lambda Function and Put a breakpoint

Start ServerlessDebugger
Please make sure your lambda handler file is opened on VSCode, otherwise ServerlessDebugger throws an exception due to unrecognized runtime. While your lambda file is opened, open the “Command Palette…” and typing “ServerlessDebugger: Start debugger” and press Enter.

Check ServerlessDebugger Connection
After “Start: ServerlessDebugger Debugger” activated, a modal like below will pops up at the right-bottom corner of VSCode. Then, you can check your VSCode and Thundra Broker Connection.

Invoking your Lambda Function
After invoking your lambda function, ServerlessDebugger hit the breakpoint and Voila! Now you can debug your lambda function.

Conclusion
This blog explains step-by-step integration of the ServerlessDebugger for VSCode. If you would like to get more information and see the advanced features of the ServerlessDebugger, you can visit our documentation about “Live Debugging for Serverless Python”. Also, you can check ServerlessDebugger VSCode Marketplace.