AWS Serverless how to use "sam local start-api" to debug .net core 3.1 applications

Viewed 341

I would like to start serverless application locally and then debug it using Visual Studio. I see command line arguments --debug-port, --debugger-path, --debug-args and --debug-function, but no example of how these can be used for .net core.

1 Answers

This is what I'm using for Visual Studio Code. I'm on Windows using dotnetcore3.1.

Firstly, I had to download the Linux vsdbg debug files (yes, Linux, as these files will be mounted in the SAM docker container)

https://vsdebugger.azureedge.net/vsdbg-17-0-10712-2/vsdbg-linux-x64.tar.gz

Unzip them into a folder, e.g. C:\vsdbg

I have a task to launch SAM. My tasks.json looks like:

{  
    "version": "2.0.0",  
    "tasks": [{
        "label": "sam local api",
        "type": "shell",
        "command": "sam",
        "args": [
            "local",
            "start-api",
            "-d", "5858",
            "--template", "${workspaceFolder}/template.yaml",
            "--debugger-path", "C:\\vsdbg",
            "--warm-containers", "EAGER"
        ],
    }]  
}  

IMPORTANT:

** --debugger-path points to the linux debug files folder. the sam cli will mount the files for you.

** I had to use --warm-containers EAGER to keep the container from closing after every request

launch.json looks like this:

{
    "name": "sam local api attach",
    "type": "coreclr",
    "processName": "dotnet",
    "request": "attach",
    "pipeTransport": {
        "pipeCwd": "${workspaceFolder}",
        "pipeProgram": "powershell",
        "pipeArgs": [
            "-c",
            "docker exec -i $(docker ps -q -f publish=5858) ${debuggerCommand}"
        ],
        "debuggerPath": "/tmp/lambci_debug_files/vsdbg",
        "quoteArgs": false
    },
    "sourceFileMap": {
        "/var/task": "${workspaceFolder}"
    }
},

This bit: $(docker ps -q -f publish=5858) gets the id of your docker container by filtering on the port that you're using.

This took quite a bit of fiddling to get working, I'm surprised it's not easier or at least some decent documentation on it.

Related