Running and debugging Serverless Framework Python Lambda functions locally in VSCode ☁️

Luke Rogerson
3 min readJul 27, 2021

Various serverless technology logos
How to debug and step-through Python Lambda functions managed by Serverless Framework in VSCode.

For serverless development, I believe you need to have the ability to run and debug individual functions locally.

Serverless Framework has a useful feature for doing just that: the invoke local command.

The issue I have found is while it works for Node.js functions, I have never been able to get serverless invoke local working with VSCode’s excellent Python debugging tools.

There are some great guides available for setting up VSCode to debug Node.js Lambda functions managed by Serverless Framework, but I have not seen many for Python, possibly due to the faff of creating a debugging setup that uses a Node.js script to invoke a Python runtime.

This guide explains how I run and debug my Python Lambda functions locally. My solution makes use of AWS SAM and the AWS Toolkit for VSCode.

Why not just use AWS SAM in place of Serverless Framework (SF) then, I hear some of you asking? Well, for one, I use SF at my work. SF has grown on me, too. It’s a mature, battle tested platform at this point, and its plugin ecosystem provides all sorts of extra functionality for many different use cases not explicitly supported out the box. I am able to leverage the best parts of SF and AWS SAM together.

This setup allows me to do proper local step-through debugging of my Lambda functions by setting breakpoints, inspecting variables, and executing function code one line at a time, while still communicating with real, deployed AWS resource - just as the function would do in a real invocation.

Prerequisites

I am assuming you have Serverless Framework already installed, you use it to manage and deploy Python Lambda functions to AWS, and you use VSCode with the Python extension.

Steps

  1. Install the AWS SAM CLI.
  2. Install the AWS Toolkit for VSCode.
  3. Add the below launch profile to your .vscode/launch.json .
  4. Run and debug as normal! Further config can be found here.
A VSCode debug config for Python Lambda functions

A few things to note

A “preLaunchTask” I use for generating a requirements.txt
  • I use named profiles when interacting with the AWS CLI, which you specify in the aws field.
  • Function environmentVariables and payload s can also easily be customised.
  • You can pass additional runtime debug arguments this way.

That should be all you need to get started! 👟

A screenshot of VSCode’s debugger
A screenshot of VSCode’s debugger

Any feedback? Do you know of way to get sls invoke local playing nice with Python and VSCode’s debugging? Have an alternative solution or just think this is completely stupid? Let me know in the comments!

Sign up to discover human stories that deepen your understanding of the world.

Luke Rogerson
Luke Rogerson

Written by Luke Rogerson

Full-stack software engineer. I like to create, teach and learn. www.lukerogerson.me

No responses yet

Write a response