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

20,664

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.

AWS Lambda function

Solution 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

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

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

Share:
20,664
Pooja K Bhatt
Author by

Pooja K Bhatt

Was working as Sr. Software Developer at Hex Wireless Pvt Ltd

Updated on January 03, 2022

Comments

  • Pooja K Bhatt
    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:

    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
    
  • David Fulton
    David Fulton over 4 years
    While 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
    Pooja K Bhatt over 4 years
    Yes you are right, that the executable has to be called "main", not just the function in the code.
  • Poh Zi How
    Poh Zi How over 3 years
    this can now be found under "Runtime Settings"
  • Conor
    Conor over 3 years
    using the standard serverless framework boiler code, this solves the error
  • coder kemp
    coder kemp over 3 years
    Naming the Handler in AWS lambda function 'Handler' is a important step, once the steps mentioned above have been followed.
  • Nigel Savage
    Nigel Savage over 2 years
    you did your best but this does not rely add anything that is not in the other answers
  • Admin
    Admin over 2 years
    Please provide additional details in your answer. As it's currently written, it's hard to understand your solution.
  • Charlie Araya
    Charlie Araya over 2 years
    After 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
    pyFiddler over 2 years
    why 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
    colm.anseo over 2 years
    Yes the executable name is key - but the name doesn't strictly need to be main. One can change the Handler name on the AWS side to another executable name.