error "fork/exec /var/task/main: no such file or directory" while executing aws-lambda function
Solution 1
Run following commands in command prompt
set GOOS=linux
set GOARCH=amd64
set CGO_ENABLED=0
After this , build your project and upload zip file to aws console lambda
like this
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o main main.go
Reference Link : https://github.com/aws/aws-lambda-go
Solution 2
In my case the problem was default setted handler to 'hello' function.
Needed to change it to 'main' via AWS Lambda view panel -> Basic Settings -> Edit.
Solution 3
There are two reasons can happen:
-
You did't use GOOS=linux GOARCH=amd64 with go build, so try:
GOOS=linux GOARCH=amd64 go build -o main main.go
You used to build this programm some CI function with golang-alpine base image, so try to use full golang image instead.
Solution 4
Recently I was facing similar issue, and I solved it. As the error says it s finding executable with handle name, so you should name your executable same as handler.
Follow these steps and your will not get this error, I am using PowerShell
> go.exe get -u github.com/aws/aws-lambda-go/cmd/build-lambda-zip # If you do not this have utility earlier
> $env:GOOS = "linux"
> $env:GOARCH = "amd64"
> $env:CGO_ENABLED = "0"
> go build -o .\Handler .\main.go # considering your are in the same directory where your main.go or handler code is present
> ~\Go\Bin\build-lambda-zip.exe -o .\Handler.zip .\Handler
Upload this code, and change handler name to Handler(name of the executable that you created above)
Let me know if this helps.
Solution 5
In my embarrasing case I was zipping the folder from outside so the path in the zip I was uploading was 'folder/main'
Pooja K Bhatt
Was working as Sr. Software Developer at Hex Wireless Pvt Ltd
Updated on January 03, 2022Comments
-
Pooja K Bhatt over 2 years
Getting
error fork/exec /var/task/main: no such file or directory
while executing lambda function.I am using windows platform to run and build code in Go.
I have done following steps to deploy go aws-lambda handler:
- Written code in go language with VSCode in windows platform
- Build project with : go build main.go
- Convert main.exe to main.zip
- Uploaded main.zip with handler name main aws-lambda fuction using aws console account
- Created test event to test lambda function
- Got error "fork/exec /var/task/main: no such file or directory while executing lambda function"
package main import ( "fmt" "github.com/aws/aws-lambda-go/lambda" ) // Request represents the requested object type Request struct { ID int `json:"ID"` Value string `json:"Value"` } // Response represents the Response object type Response struct { Message string `json:"Message"` Ok bool `json:"Ok"` } // Handler represents the Handler of lambda func Handler(request Request) (Response, error) { return Response{ Message: fmt.Sprint("Process Request Id %f", request.ID), Ok: true, }, nil } func main() { lambda.Start(Handler) }
build command
go build main.go
Detail Error in AWS console
{ "errorMessage": "fork/exec /var/task/main: no such file or directory", "errorType": "PathError" }
Log Output in AWS console
START RequestId: 9ef206ed-5538-407a-acf0-06673bacf2d7 Version: $LATEST fork/exec /var/task/main: no such file or directory: PathError null END RequestId: 9ef206ed-5538-407a-acf0-06673bacf2d7 REPORT RequestId: 9ef206ed-5538-407a-acf0-06673bacf2d7 Duration: 0.64 ms Billed Duration: 100 ms Memory Size: 512 MB Max Memory Used: 31 MB Init Duration: 1.49 ms
-
David Fulton over 4 yearsWhile it wasn't a problem in this example, I think it might be worth pointing out that the executable has to be called "main", not just the function in the code. I found this out the hard way when following the instructions above.
-
Pooja K Bhatt over 4 yearsYes you are right, that the executable has to be called "main", not just the function in the code.
-
Poh Zi How over 3 yearsthis can now be found under "Runtime Settings"
-
Conor over 3 yearsusing the standard serverless framework boiler code, this solves the error
-
coder kemp over 3 yearsNaming the Handler in AWS lambda function 'Handler' is a important step, once the steps mentioned above have been followed.
-
Nigel Savage over 2 yearsyou did your best but this does not rely add anything that is not in the other answers
-
Admin over 2 yearsPlease provide additional details in your answer. As it's currently written, it's hard to understand your solution.
-
Charlie Araya over 2 yearsAfter the first step I only had to do 'go build -o main main.go', zipped the main file that appeared and uploaded to AWS. That solved my issue at least
-
pyFiddler over 2 yearswhy in the F is the default handler set to "hello" on a new Lambda function? Even the basic tutorial has you name everything "main"!
-
colm.anseo over 2 yearsYes the executable name is key - but the name doesn't strictly need to be
main
. One can change theHandler
name on the AWS side to another executable name.