AWS SAM python hello world application not importing 'requests' module on local debug

Viewed 95

I was trying to debug the hello world SAM template locally in Visual Studio Code.

I was successful at doing so when not importing any module mentioned in requirements.txt

My .vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "aws-sam",
            "request": "direct-invoke",
            "name": "Invoke Lambda",
            "invokeTarget": {
                "target": "code",
                "lambdaHandler": "hello_world/app.lambda_handler",
                "projectRoot": "${workspaceFolder}"
            },
            "lambda": {
                "runtime": "python3.9",
                "payload": {
                    "json": {}
                }
            }
        }
        
    ]
}

My hello_world/app.py

import json
import requests


def lambda_handler(event, context):
    print('hello')
    return {
        "statusCode": 200,
        "body": json.dumps({
            "message": "hello world",
            # "location": ip.text.replace("\n", "")
        }),
    }

After commenting out import requests, I was able to debug it successfully with breakpoints.

Just to be sure, I tried to do sam build and checked the generated .aws-sam folder. It does have requests module downloaded under .aws-sam/build/HelloWorldFunction folder.

sam local invoke works without any import error.

Project structure:

.aws-sam \
    build \
        HelloWorldFunction \
            modules*
            app.py
        template.yaml
    build.toml
.vscode \
    launch.json
events \
    event.json
hello_world \
    __init__.py
    app.py
    requirements.txt
tests \
    ...
__init__.py
...
template.yaml

What am I doing wrong?

1 Answers

Apparently the debugger looks for requirements.txt in the folder you mentioned as projectRoot in launch.json

Copying requirements.txt from {projectroot} / hello-world to {projectroot} fixed the issue

Related