Search code examples
amazon-web-servicesgoaws-lambdaerror-handlingruntime-error

lambda running twice, and Runtime exited without providing a reason


I got a lambda written in Go running on a container, the image was built with alpine-golang and run with alpine.

When testing i noticed from the logs the lambda is ran twice before exiting with the following:

Error: Runtime exited without providing a reason Runtime.ExitError

From my local system this the code runs fine without errors, i earlier tried running without a container but still faced runtime issues. The only error handling and logging mechs in my code is log.Println and fmt.Printf. Anyone got an idea of what is going on?

EDIT:

I trapped the exit code, which is turns out to be 0 but lambda exits with

 Runtime exited with error: exit status 1 Runtime.ExitError

Solution

  • I really suggest going for the "without container" path. Just pack your executable into a .zip archive. Don't forget to compile with GOOS=linux for your code to be compatible with AWS Lambda.

    On Linux you can use the following commands to get your archive:

    GOOS=linux go build -o executableName path/to/main.go
    zip archive.zip executableName
    

    Note that you have to set Handler to be executableName in function's Runtime settings.

    For handling lambda function, you have to use github.com/aws/aws-lambda-go/lambda package and in main start the handler function like lambda.Start(handler).

    Full code example:

    package main
    
    import (
        "context"
        "log"
    
        "github.com/aws/aws-lambda-go/lambda"
    )
    
    func main() {
        lambda.Start(handler)
    }
    
    func handler(ctx context.Context) {
        log.Println("successfully executed")
    }