Search code examples
goaws-lambdaaws-lambda-go

AWS Lambda GO PathError (Windows 10)


I've done the following in PowerShell in VSCode:

PS D:\Project Resume> set GOOS=linux
PS D:\Project Resume> set GOARCH=amd64
PS D:\Project Resume> set CGO_ENABLED=0
PS D:\Project Resume> cd .\dist\events\
PS D:\Project Resume\dist\events> go build -o main main.go
PS D:\Project Resume\dist\events> ~\Go\Bin\build-lambda-zip.exe -o main.zip main
2020/11/14 14:05:13 wrote main.zip

This seems to zip the file fine, and I changed the handler to "main" in the Lambda console as well. However, I'm still getting this:

START RequestId: 42d5f7d6-3c9d-457d-ab2f-5030b39a5bce Version: $LATEST
fork/exec /var/task/main: exec format error: PathError
null
END RequestId: 42d5f7d6-3c9d-457d-ab2f-5030b39a5bce
REPORT RequestId: 42d5f7d6-3c9d-457d-ab2f-5030b39a5bce  Duration: 0.43 ms   Billed Duration: 100 ms Memory Size: 512 MB Max Memory Used: 24 MB  Init Duration: 2.84 ms  

If this matters, here's the code I have (I followed a tutorial on YouTube, using "main.go" as file name):

//Lambda Function Go Code
package main

import (
    "errors"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func main() {
    lambda.Start(HandleRequest)
}

//HandleRequest the request handler for our lambda stuff
func HandleRequest(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    if request.HTTPMethod == "POST" {
        var stringResponse string = "Success :)"
        APIResponse := events.APIGatewayProxyResponse{Body: stringResponse, StatusCode: 200}
        return APIResponse, nil
    }
    err := errors.New("Method Not Allowed")
    APIResponse := events.APIGatewayProxyResponse{Body: "Method Not OK", StatusCode: 502}
    return APIResponse, err

}

Out of curiosity, I decided to check the permissions of the zip file as well. Could this have something to do with it, possibly???

main.zip permissions Windows 10


Solution

  • In my case, I was following documentation, thinking the COMMAND to set the GOOS env variable was something like this:

    set GOOS=linux
    

    However, according to the following documentation:

    https://mcpmag.com/articles/2019/03/28/environment-variables-in-powershell.aspx?m=1

    Since I was running in PowerShell, I SHOULD'VE used THIS command:

    $env:GOOS = "linux"
    

    As many docs will say elsewhere, you'll know you've set the env variable correctly when you use the following in PowerShell, whether from VSCode or outside of that IDE:

    go env
    

    I hope this helps someone out compiling in Windows down the road.