'error "fork/exec /var/task/main: no such file or directory" while executing aws-lambda function

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:

  1. Written code in go language with VSCode in windows platform
  2. Build project with : go build main.go
  3. Convert main.exe to main.zip
  4. Uploaded main.zip with handler name main aws-lambda fuction using aws console account
  5. Created test event to test lambda function
  6. 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


Solution 1:[1]

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.

AWS Lambda function

Solution 2:[2]

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 3:[3]

There are two reasons can happen:

  1. You did't use GOOS=linux GOARCH=amd64 with go build, so try:

    GOOS=linux GOARCH=amd64 go build -o main main.go

  2. You used to build this programm some CI function with golang-alpine base image, so try to use full golang image instead.

Solution 4:[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) enter image description here

Let me know if this helps.

Solution 5:[5]

In my embarrasing case I was zipping the folder from outside so the path in the zip I was uploading was 'folder/main'

Solution 6:[6]

In my case it was related to my Go setting.

As I use Go Version Manager, I had to run the following to specify the right Go path before compiling my project.

gvm use go1.x.x

Proceed to compile then deploy.

Solution 7:[7]

My case was a strange one, but I'll leave it here in case someone needs it.

I built my executable in a Docker container and then used Docker cp to put the file in a local directory. Then I zipped the executable and deployed it.

Apparently, Docker cp creates a symlink-like file rather than an actual file, so it just wouldn't work.

My solution was just to use sudo cp and copy the executable as another file, which created a physical file, and deployed that instead.

Solution 8:[8]

This error has nothing to do with the program; it's a configuration issue. Please try to give the complied go file name in the lambda console under Go Runtime settings -

In my case my go lambda compiled file is test so configured Handler as test and it worked for me.

Example Configuration

Solution 9:[9]

In my case, the solution is very simple.

I was wrong with the command build. what I did was go build . instead of go build main.go

the zip file should be main.zip.

and the handler info section in console AWS should be named main. not handler because lambda calls the main function first

Solution 10:[10]

If deploying with SAM cli, make sure to run "sam build" before the "sam deploy"

Solution 11:[11]

In my case this had to do with the image I was using to build the go binaries. I switched from using alpine to using amazon linux 2 and the problem was solved.

Solution 12:[12]

setting environment variable CGO_ENABLED to 0 solved my problem, this is when using sam local start-api to locally test endpoint