skip to Main Content

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

2

Answers


  1. Make sure you are following the recommended guide lines aws provide with building the container image. https://docs.aws.amazon.com/lambda/latest/dg/go-image.html

    your Dockerfile should look like this to work with lambda,

    FROM public.ecr.aws/lambda/provided:al2 as build
    # install compiler
    RUN yum install -y golang
    RUN go env -w GOPROXY=direct
    # cache dependencies
    ADD go.mod go.sum ./
    RUN go mod download
    # build
    ADD . .
    RUN go build -o /main
    # copy artifacts to a clean image
    FROM public.ecr.aws/lambda/provided:al2
    COPY --from=build /main /main
    ENTRYPOINT [ "/main" ]  
    

    Lambda is very strange where if you have the Dockerfile like you would on a local machine then it runs it once, ends, then a second time and crashes with no reason given

    Login or Signup to reply.
  2. 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")
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search