Search code examples
amazon-web-servicesvisual-studio-codevscode-debuggeraws-samaws-sam-cli

VSCode Debugger unable to resolve non-existent files while locally debugging AWS Lambda


I have a AWS Lambda function that I am attempting to step-through debug with VSCode. I am running into an issue where the behaviour of the debugger and VSCode does not make sense, claiming it cannot resolve non-existent files from paths that it should not be looking for these packages at.

The lambda function has been tested locally using the aws-sam-cli's sam build and sam local invoke functionality. The lambda correctly takes a JSON event with -e, and returns the expected response. This has been tested with the following setup for its SAM template:

Resources:
  MyLambdaFunction:
    Type: AWS::Serverless::Function
    Metadata:
      DockerTag: python3.8-v1
      DockerContext: .
      Dockerfile: Dockerfile
    Properties:
      PackageType: Image

The launch.json, referenced from https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/serverless-apps-run-debug-config-ref.html, is configured as follows:

{
    "configurations": [
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "MyLambdaFunction",
            "invokeTarget": {
                "target": "template",
                "templatePath": "${workspaceFolder}/path/to/template.yaml",
                "logicalId": "MyLambdaFunction"
            },
            "lambda": {
                "runtime": "python3.8",
                "payload": {
                    "path": "${workspaceFolder}/path/to/input.json"
                },
                "environmentVariables": {}
            }
        }
    ]
}

When attempting to debug via Run > Start Debugging in VSCode, the docker build completes successfully, and the debugger attaches as per the AWS Toolkit logs. The Dockerfile in question is as follows:

FROM amazon/aws-lambda-python:3.8

COPY index.py requirements.txt ./
ADD mymodules ./mymodules
RUN python3.8 -m pip install -r requirements.txt -t .

CMD ["index.lambda_handler"]

After setting a breakpoint on the first line of index.py, which is import time, the debugger stops as expected and all is fine. When I begin to step through the lambda code, there is another import from one of my modules where I am importing from elasticsearch import Elasticsearch. This is where the first issue appears. VSCode throws an error window in the bottom right with the following message:

Unable to open 'socks.py': Unable to read file
'/Users/me/path/to/app/dir/lambdacode/urllib3/contrib/socks.py' 
(Error: Unable to resolve nonexistent file 
'/Users/me/path/to/app/dir/lambdacode/urllib3/contrib/socks.py'

Where lambdacode is the root directory containing the lambda code, module directories, requirement.txt, etc.

I am unable to determine why the debugger is looking for these packages at this workspace path. It seems as if the debugger is not utilizing the docker container at all, since it is looking locally for the packages. For reference, urllib3 is installed system wide on my machine, installed in my virtual environment, and installed in the docker container in question.

Why is the debugger attempting to look exactly here in my workspace folder for the package, and is not utilizing the docker container? Is this a behaviour that I can change somehow? Even after stepping over this import and ignoring the error, there continues to be identical errors for other libraries such as dateutil etc, which are also installed everywhere that makes sense.

I have confirmed this is an issue with this simple import statement, as I pulled an AWS SAM example with sam init, and chose the Hello World Image Example for Python3.8, confirmed it debugged as-is, added the import and the requirement for elasticsearch, and encountered the same error when I tried to run the debugger again.

Any insight or help would be appreciated.


Solution

  • Solved. It relates to debugpy. See more here:

    https://github.com/aws/aws-sam-cli/issues/3347