aws / aws-toolkit-vscode

Amazon Q, CodeCatalyst, Local Lambda debug, SAM/CFN syntax, ECS Terminal, AWS resources
https://marketplace.visualstudio.com/items?itemName=AmazonWebServices.amazon-q-vscode
Apache License 2.0
1.52k stars 449 forks source link

Local debuggung Python lambda function (using pyenv / virtualenv) #4403

Open mat-patrickschmelter opened 9 months ago

mat-patrickschmelter commented 9 months ago

Problem

I can not get the local Lambda debugger to work

Steps to reproduce the issue

  1. Create Lambda function with sam init. Choose the HelloWorld sample applicaiton, name it HelloWorldFunction

  2. Create Launch configuration

        {
          "name": "HelloWorld template",
          "type": "aws-sam",
          "request": "direct-invoke",
          "invokeTarget": {
            "target": "template",
            "templatePath": "${workspaceRoot}/HelloWorldFunction/template.yaml",
            "logicalId": "HelloWorldFunction"
          },
          "lambda": {
            "runtime": "python3.12",
            "payload": {
                "path": "${workspaceRoot}/HelloWorldFunction/events/event.json"
            }
          },
          "sam": {
            "buildArguments": [
              "--debug"
            ],
            "localArguments": [
              "--debug"
            ]
            },
        },
        {
          "name": "HelloWorld code",
          "type": "aws-sam",
          "request": "direct-invoke",
          "invokeTarget": {
            "target": "code",
            "lambdaHandler": "app.lambdaHandler",
            "projectRoot": "${workspaceRoot}/HelloWorldFunction",
          },
          "lambda": {
            "runtime": "python3.12",
            "payload": {
                "path": "${workspaceRoot}/HelloWorldFunction/events/event.json"
            }
          },
          "sam": {
            "buildArguments": [
              "--debug"
            ],
            "localArguments": [
                "--template",
                "${workspaceRoot}/HelloWorldFunction/template.yaml",
                "--debug"
              ]
            },
        }
  3. add debugpy to requirements.txt

  4. Invoke HelloWorld template debug session Log: https://pastebin.com/QheCQdep

  5. Invoke HelloWorld code debug session Log: https://pastebin.com/KKve3vzq

Expected behavior

Debugging works

System details (run the AWS: About Toolkit command)

OS: Darwin x64 23.2.0 Visual Studio Code extension host: 1.85.1 AWS Toolkit: 2.8.0 node: 18.15.0 electron: 25.9.7

justinmk3 commented 9 months ago

For reference, from your logs:

2024-02-07 14:33:06.607 [info] START RequestId: e703ce28-44c0-4696-adc5-7800faca205d Version: $LATEST
2024-02-07 14:33:06.668 [info] Prepending Lambda task root to path: /var/task
Starting debugger...
2024-02-07 14:33:06.678 [info] Traceback (most recent call last):
  File "/tmp/lambci_debug_files/py_debug_wrapper.py", line 46, in <module>
    runpy.run_module('debugpy', run_name='__main__')
  File "/var/lang/lib/python3.9/runpy.py", line 221, in run_module
    mod_name, mod_spec, code = _get_module_details(mod_name)
  File "/var/lang/lib/python3.9/runpy.py", line 141, in _get_module_details
    raise error("No module named { raw: true }" % mod_name)
ImportError: No module named debugpy

But also, in the target: code case, sam build fails because it was expecting python 3.9?

2024-02-07 14:37:33.882 [info] 2024-02-07 14:37:33,875 | Invalid executable for python at /Users/...path.../.pyenv/shims/python
Traceback (most recent call last):
  File "/usr/local/Cellar/aws-sam-cli/1.107.0/libexec/lib/python3.12/site-packages/aws_lambda_builders/workflow.py", line 85, in wrapper
    valid_path = binary_checker.validator.validate(executable_path)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/Cellar/aws-sam-cli/1.107.0/libexec/lib/python3.12/site-packages/aws_lambda_builders/workflows/python_pip/validator.py", line 52, in validate
    raise MisMatchRuntimeError(language=self.language, required_runtime=self.runtime, runtime_path=runtime_path)
aws_lambda_builders.exceptions.MisMatchRuntimeError: python executable found in your path does not match runtime. 
 Expected version: python3.9, Found a different version at /Users/...path.../.pyenv/shims/python. 
 Possibly related: https://github.com/awslabs/aws-lambda-builders/issues/30

It could be getting confused because of virtualenv or pyenv: https://github.com/aws/aws-toolkit-vscode/issues/1779

Workaround

Try setting containerBuild (documentation) in the launch config. That tells sam to build in the Docker container, which avoids having to worry about python compatibility on your local machine / host system.