pythonvisual-studio-codeaws-lambdaaws-toolkit

Debugging Python Lambda Functions in VSCode


I have successfully been using AWS Toolkit to debug node.js lambda functions for a couple of years now. For my latest project I wanted to write my lambda functions in python. I have followed the directions in the tutorial below.

https://codeolives.com/2020/01/02/vs-code-with-python-aws-lambda-a-complete-tutorial-to-develop-and-deploy-python-lambda-functions-using-vs-code-part-2/

This tutorial really seems identical to what I have been doing to debug node.js. However, when I hit the "run and debug", I get this error.

2021-10-08 17:04:00 [INFO]: Running PythonPipBuilder:ResolveDependencies

2021-10-08 17:04:03 [INFO]: Build Failed

2021-10-08 17:04:04 [INFO]: Error: PythonPipBuilder:ResolveDependencies - {debugpy==1.5.0(wheel)}

2021-10-08 17:04:04 [WARN]: "sam build" failed: C:/path_to_user/AppData/Local/Temp/aws-toolkit-vscode/vsctkYP3zOS/app___vsctk___template.yaml

Obviously it's saying that it can't resolve debugpy but I am just beginning to use python and I am not sure what this is or how to resolve it. A google search didn't turn up anything that resolved the issue. If anyone could enlighten me, I'd really appreciate it


Solution

  • One of the biggest advantages of Python is that lambda_handler functions are just another function in a python file. You can use Pytest and such to Unit Test the actual handler just like any other function without worrying about the Docker/SAM setup for the lambda.

    Now, this is not perfect. Mocking a Context Object can be difficult if you are relying on it at all (If you aren't, passing an empty dict {} to the second paramater is just fine) and its not exactly the same environment obviously. Imports can be different, as well as logging can be weird.

    If you have SAM and Docker already, the mocking library moto can help with this, though it is also not perfect (and honestly, more for mocking lambda calls from within other functions than mocking the actual lambda)

    As for your actual error and question...

    Try removing the ==X.X.X from your requirements.txt and let pip try to get the latest - I know that isn't wonderful but it will let you know if that potential library for some reason is having trouble being pip installed into the docker container.

    If that works, then it is likely a connection issue between your docker set up and pypy - you may have to modify your pip connection data to know where to look or to look somewhere specific.