jpskgc.v2
jpskgc.v2

Reputation: 43

How to debug application made by AWS SAM in VSCode?

Summary

I made Go application by AWS SAM. Now I try to debug this sample application in VSCode, but It fails so I want to know how to correctly debug it.

Tried

toggl-slack
├── Makefile                    
├── README.md     
├── dlv 
├── samconfig.toml               
├── hello-world                
│   ├── main.go                 
│   └── main_test.go            
└── template.yaml

I put following command on console for debug.

cd toggl-slack
go get -u github.com/go-delve/delve/cmd/dlv
GOARCH=amd64 GOOS=linux go build -o ./dlv github.com/go-delve/delve/cmd/dlv
GOARCH=amd64 GOOS=linux go build -gcflags='-N -l' -o hello-world/hello-world ./hello-world
sam local start-api -d 5986 --debugger-path . --debug-args="-delveAPI=2"
curl http://127.0.0.1:3000/hello

As result, debug does not work and console shows error message.

Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template
2019-12-28 21:23:17  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
Invoking hello-world (go1.x)

Fetching lambci/lambda:go1.x Docker container image......
Mounting /Users/jpskgc/toggl-slack/hello-world as /var/task:ro,delegated inside runtime container
Could not create config directory: mkdir /home/sbx_user1051: permission denied.API server listening at: [::]:5986
2019-12-28T12:23:46Z info layer=debugger launching process with args: [/var/task/hello-world]
2019-12-28T12:23:47Z warning layer=debugger reading debug_info: could not find abstract origin (0x13ed31) of inlined call at 0xfab50

Codes

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Connect to Lambda container",
      "type": "go",
      "request": "launch",
      "mode": "remote",
      "remotePath": "",
      "port": 5986,
      "host": "127.0.0.1",
      "program": "${workspaceRoot}",
      "env": {},
      "args": []
    }
  ]
}

Other code is same as default sample application for AWS SAM.

Full Source Code is here:
https://github.com/jpskgc/toggl-slack/tree/0db02109685ce89f17ed64fdaadd5261e5f61512

Upvotes: 1

Views: 1756

Answers (1)

Rob S.
Rob S.

Reputation: 469

The reading debug_info: could not find abstract origin line tells us that the compile flags got scrambled and the debug info was not actually included. What we need is:

GOOS=linux GOARCH=amd64 go build -gcflags "all=-N -l" -o hello-world/hello-world ./hello-world

Second, VSCode should tell you this, but in launch.json, "request": "launch", is now "request": "attach", for mode remote giving:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Connect to SAM local",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "remotePath": "",
            "port": 5986,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}",
            "env": {},
            "args": []
        }
    ]
}

With the above changes you should be able to attach to the debug server using VSCode (or GoLand/Vim/CLI).

Finally, the Could not create config directory: mkdir /home/sbx_user1051: permission denied. line is safe to ignore.

I am contributing this on behalf of my employer, Amazon. My contribution is licensed under the MIT license. See here for a more detailed explanation.

Upvotes: 2

Related Questions