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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat
  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.

Hamit Burak Emre user avatar by
Hamit Burak Emre
·
Jan. 28, 19 · Tutorial
Like (4)
Save
Tweet
Share
22.43K 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.

Popular on DZone

  • Handling Automatic ID Generation in PostgreSQL With Node.js and Sequelize
  • Image Classification With DCNNs
  • A Guide To Successful DevOps in Web3
  • Tips for Optimizing PHP Code for Better Performance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: