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.45k stars 377 forks source link

M1 Mac (Apple Silicon): AWS SAM Golang lambda debugging does not work #2020

Open ashprao opened 2 years ago

ashprao commented 2 years ago

Desktop (please complete the following information):

How to get my AWS SAM initiated local debugging of lambda's written in Go working?

Summary of the problem After creating a plain vanilla AWS SAM application with sam init for golang, and then using the code lens to create an AWS SAM template file based debug configuration, I get the following error when trying to debug the lambda code. Running the code without debugging works.

2021-09-11 10:55:02 [INFO]: The Delve repo was not found in your GOPATH. Downloading in a temporary directory...
2021-09-11 10:55:03 [INFO]: Preparing to debug locally: Lambda "hello-world"
2021-09-11 10:55:03 [INFO]: Building SAM application...
2021-09-11 10:55:03 [INFO]: Running command: (not started) [/opt/homebrew/bin/sam build --build-dir /tmp/aws-toolkit-vscode/vsctkeYZ6BB/output --template /Users/user1/dev/godev/aws/lambdas/hello-world/hello-world/app___vsctk___template.yaml --base-dir /Users/user1/dev/godev/aws/lambdas/hello-world/hello-world]
2021-09-11 10:55:03 [INFO]: Building codeuri: /Users/user1/dev/godev/aws/lambdas/hello-world/hello-world runtime: go1.x metadata: {} functions: ['helloworld']

2021-09-11 10:55:03 [INFO]: Running GoModulesBuilder:Build

2021-09-11 10:55:03 [INFO]: 
Build Succeeded

2021-09-11 10:55:03 [INFO]: 
Built Artifacts  : ../../../../../../../private/tmp/aws-toolkit-vscode/vsctkeYZ6BB/output
Built Template   : ../../../../../../../private/tmp/aws-toolkit-vscode/vsctkeYZ6BB/output/template.yaml

Commands you can use next
=========================
[*] Invoke Function: sam local invoke -t ../../../../../../../private/tmp/aws-toolkit-vscode/vsctkeYZ6BB/output/template.yaml
[*] Deploy: sam deploy --guided --template-file ../../../../../../../private/tmp/aws-toolkit-vscode/vsctkeYZ6BB/output/template.yaml

2021-09-11 10:55:04 [INFO]: Build complete.
2021-09-11 10:55:04 [INFO]: Starting SAM application locally
2021-09-11 10:55:05 [INFO]: AWS.running.command
Invoking hello-world (go1.x)
Requested to skip pulling images ...

Mounting /tmp/aws-toolkit-vscode/vsctkeYZ6BB/output/helloworld as /var/task:ro,delegated inside runtime container
START RequestId: 7c546e9e-8558-4fde-8dd6-fc6946034850 Version: $LATEST
API server listening at: [::]:5858
2021-09-11T00:55:07Z info layer=debugger launching process with args: [/var/task/hello-world]
2021-09-11 10:55:07 [INFO]: Waiting for SAM application to start...
could not launch process: fork/exec /var/task/hello-world: function not implemented
2021/09/11 00:55:07 exit status 1
time="2021-09-11T00:55:07.765" level=error msg="Init failed" InvokeID= error="Runtime exited with error: exit status 1"
time="2021-09-11T00:55:07.773" level=error msg="INIT DONE failed: Runtime.ExitError"
2021-09-11 10:55:08 [INFO]: Attaching debugger to SAM application...

Command stopped: "sam local invoke"

From the output listed above, it seems to me that when using the debugger, it thinks that the function is not implemented. 2021-09-11 10:55:07 [INFO]: Waiting for SAM application to start... could not launch process: fork/exec /var/task/hello-world: function not implemented

Running the above lambda from command line with sam build && sam local invoke works.

Other considerations

Steps to reproduce

  1. Create a new AWS SAM application using Go with sam init.
  2. Select the Hello world template.
  3. In the main.go file, create a debug configuration from the code lens provided by the AWS Toolkit and associated with the handler.
  4. Set a breakpoint and start debugging.
JadenSimon commented 2 years ago

could not launch process: fork/exec /var/task/hello-world: function not implemented

I believe function not implemented is referring to a binary within the Docker container being unavailable, probably since it's emulating AMD64 on Apple silicon. I'm guessing Delve is trying to use some low-level library that the emulation does not support. There's not much information but I think ptrace is the thing that's missing: https://github.com/docker/for-mac/issues/5191

SAM CLI does not support ARM containers, or else the solution would be to just run the lambda with the native architecture. Similar issue here: https://github.com/aws/aws-toolkit-vscode/issues/1566

justinmk3 commented 2 years ago

see also https://github.com/aws/aws-toolkit-vscode/issues/1566

ashprao commented 2 years ago

Thanks for the responses. Much appreciated.

The background for this is that we are evaluating (once again) Go's suitability for some of our workloads. We are very keen to use, it but would prefer to have an easy workflow from a tooling perspective in coding our Lambda's with Go.

To that end, I am just a bit confused as to where to pursue this issue and make a case for getting a fix. Should I raise a bug here in AWS Toolkit project or is this an issue for the AWS SAM team, in the hope that they can can fix the issue with the docker container, or is it an issue for Docker?

We have standardised on VS Code and from our point of view, would consider moving to Go a show stopper without getting the debugger issue fixed.

JadenSimon commented 2 years ago

Ah sorry, I realized I didn't offer any further steps. I would open up an issue with SAM CLI requesting further guidance in regards to ARM containers. They may also know more about potentially getting Delve to work correctly in an x86 container on an M1 mac. I don't have an M1 mac personally, otherwise I'd try this out myself to see if I could get it working.

It may be possible to build your own base image for ARM, though at that point things start to get nebulous in regards to support from other tooling.

Keep in mind that debugging Go lambdas would work correctly on x86 machines (e.g. an Intel mac), just not M1 macs. You could set-up an x86 host and use remote development as well. This way you can still use VS Code for your workflows, though it'll be a little bit more involved than normal.

edyesed commented 2 years ago

I had this issue. Check your filesharing preferences in docker. I had shared /tmp , though after some update to docker this stopped working. I had to additionally share /private. You can find the file sharing preferences under Docker -> Preferences -> Resources -> File Sharing . Make sure you have /tmp and /private in there. You can test with docker run -it /tmp/aws-toolkit-vscode/vsctkeYZ6BB/output/helloworld:/tmp/goo ubuntu:latest /bin/bash . If ls /tmp/goo inside your container comes up with the empty set, docker filesharing a very probable culprit.

ashprao commented 2 years ago

Hi @edyesed, thanks for your feedback.

I was able to confirm that Docker set up had /tmp and /private entries in the Docker->Preferences->Resources->File Sharing. Additionally running ls /tmp/goo after starting Docker with docker run -iv /tmp/aws-toolkit-vscode/vsctkwDKvtu/output/helloworld:/tmp/goo ubuntu:latest /bin/bash did list the hello-world artefact, so I am assuming that file share is working and perhaps not a contributor to my issue?

Just out of curiosity, it would be great if you could let me know if the list of directories shared, that I had by default in my Docker setting, are what you also have in your docker preferences, in addition to /tmp and /private.

/Users
/Volumes
/private
/tmp
/var/folders
edyesed commented 2 years ago

I concur that file sharing is not the root of the issue you’re experiencing. In my docker file share I had /Users and /tmp . This had been functional for me historically with debugging Sam apps with vscode. This week I need to add /private as well 🤷. I recalled the error surfaced was function not implemented, but perhaps I am mistaken or this error appears for multiple conditions. Either way, file sharing is working for you and not the root of your issue.

jackmcguire1 commented 2 years ago

bump on this issue

OmerBlechman commented 2 years ago

Any updates?

justinmk3 commented 2 years ago

@OmerBlechman are you also getting could not launch process: ...: function not implemented ?

Status

rush51 commented 2 years ago

Any updates on this issue? I am also getting could not launch process: ...: function not implemented on my 2021 Apple M1 Pro.

justinmk3 commented 2 years ago

Status