DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a Serverless Application on AWS With AWS SAM
  • Keep Your Application Secrets Secret
  • Automating Storage Tiering and Lifecycle Policies in AWS S3 Using Python (Boto3)
  • Python Async/Sync: Advanced Blocking Detection and Best Practices (Part 2)

Trending

  • From APIs to Actions: Rethinking Back-End Design for Agents
  • Tactical Domain-Driven Design: Bringing Strategy to Code
  • Microservices: Externalized Configuration
  • The Network Attach Problem Nobody Warns You About
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Monitoring and Observability
  4. Debug Your Python Lambda Functions Locally

Debug Your Python Lambda Functions Locally

Learn how you can debug your lambda functions in Python.

By 
Hamit Burak Emre user avatar
Hamit Burak Emre
·
Jan. 28, 19 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
25.7K Views

Join the DZone community and get the full member experience.

Join For Free

While developing your lambda functions, debugging may become a problem. As a person who benefits a lot from step-by-step debugging, I had difficulty debugging lambda functions. I got lost in the logs. Redeploying and trying again with different parameters over and over... then, I found the AWS Serverless Application Model (SAM) Command Line Interface (CLI). The AWS SAM CLI lets you debug your AWS Lambda functions in a good, old, step-by-step way.

If you don’t know AWS SAM CLI, you should definitely check it out here. Basically, using SAM CLI, you can locally run and test your Lambda functions in a local environment that simulates the AWS runtime environment. Without the burden of redeploying your application after each change, you can develop faster in an iterative way.

Here, we will specifically debug a Python Lambda function. Before you proceed make sure that you installed AWS SAM CLI, let's move on:

1. Add a Visual Studio Code launch configuration

In order for the VS Code debugger to be attached to the simulated AWS runtime environment, we should add a launch configuration. You can use the following launch configuration:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "SAM CLI Python Hello World",
            "type": "python",
            "request": "attach",
            "port": 5890,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/var/task"
                }
            ]
        }
    ]
}


2. Install the Python Tools for Visual Studio Debug (PTVSD) package

If you don’t have, install PTVSD server to your Lambda application. You can install it by using the following command in the root of your Lambda application:


pip install ptvsd -t .


3. Add PTVSD code

Next, we should add the following lines of code to the file that contains our Lambda handler. Basically, it makes our code wait until we connect to the debugger using VS Code.

Also, let’s add a breakpoint where you want the debugger to be stopped. However, make sure that the breakpoint is added somewhere after the initialization of PTVSD :

4. Invoke your function with the AWS SAM CLI

Let’s invoke our function using SAM CLI. Here, we should specify the port that we want to connect by specifying `-d` or `--debug-port`. By using the `-e` option, you can also specify the path of event.json that you want to call your Lambda function with :

sam local invoke -e event.json -d 5890


When you run this command, AWS SAM CLI sets up the environment and waits for the VS Code debugger to be attached.

5. Start debugger and connect to PTVSD

When you start the debugging tool in VS Code (F5 in macOS), it will connect to the port specified in the launch configuration file. Once VSCode debugger connects to the ptvsd run on simulated AWS runtime environment, our Lambda function that waits for us to connect will continue and hit the first breakpoint. We now have our good old step-by-step debugger waiting on the breakpoint! You can now step in, step over, add watches, and see variables just like we do while debugging regular applications.

Voila! We configured VS Code and AWS SAM CLI to locally debug your Python Lambda functions. However, there is an important point left. When you decided to deploy your Lambda function, be sure that you removed or commented out the following lines in your Lambda application:

ptvsd.enable_attach(address=('0.0.0.0', 5890), redirect_output=True)
ptvsd.wait_for_attach()


When you call ptvsd.wait_for_attach(), your Lambda function execution waits on that line and does not proceed further. Leaving this code in your program would cause your function to be stopped at that line and would cause your function to time out.

You finished local debugging and you want to see your function running on AWS. At this point, you will need to observe how your function is actually working with third-party libraries and AWS resources like SNS or DynamoDB. You can add Thundra layer in minutes with ease and start understanding how your function is interacting with system resources.

To sum up, it is handy to test your function locally and it is perfectly achievable with AWS SAM and VS Code. However, how would you test your functions on AWS? Thundra comes in at this point. Plugging handy tools when needed for your development process eases our job at Thundra drastically. Hope it also helps to the serverless community.

Command-line interface Debug (command) Visual Studio Code Python (language) AWS application Sam (text editor)

Published at DZone with permission of Hamit Burak Emre. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a Serverless Application on AWS With AWS SAM
  • Keep Your Application Secrets Secret
  • Automating Storage Tiering and Lifecycle Policies in AWS S3 Using Python (Boto3)
  • Python Async/Sync: Advanced Blocking Detection and Best Practices (Part 2)

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook