Jared Zwick
Jared Zwick

Reputation: 11

Trivial GO lambda in IMAGE fails with /bin/sh: ./lambda-handler: Permission denied

I am using the aws provided docker file to build a SAM template. The sam build and deploy works as expected, however when calling the endpoint I get Internal Server Error with /bin/sh: ./lambda-handler: Permission denied

The file is referencing is the main entrypoint of the image:

FROM public.ecr.aws/docker/library/golang:1.19 as build-image
WORKDIR /src
COPY go.mod go.sum main.go ./
RUN go build -o lambda-handler
FROM public.ecr.aws/lambda/provided:al2
COPY --from=build-image /src/lambda-handler .
RUN chmod -R o+rX ./lambda-handler
ENTRYPOINT ./lambda-handler

Event breaking it down to it's most bare minimum functionality fails. Here is the code

package bye

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

func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {

    return events.APIGatewayProxyResponse{
        Body:       "Bye",
        StatusCode: 200,
    }, nil
}

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

Fails on sam build & sam deploy as well as sam local start-api

Further logs:

Invoking Container created from byeworldfunction:provided.al2-v1                                                                                                            
Reuse the created warm container for Lambda function 'ByeWorldFunction'                                                                                                     
Lambda function 'ByeWorldFunction' is already running                                                                                                                       
START RequestId: 15e2e7cc-c7e8-4c4a-b0ee-18e0669172f8 Version: $LATEST
/bin/sh: ./lambda-handler: Permission denied
07 Jun 2023 05:53:43,903 [ERROR] (rapid) Init failed error=Runtime exited with error: exit status 126 InvokeID=
/bin/sh: ./lambda-handler: Permission denied
END RequestId: e3fceeba-f052-4439-908c-b3f84164c9a4
REPORT RequestId: e3fceeba-f052-4439-908c-b3f84164c9a4  Init Duration: 0.36 ms  Duration: 5.16 ms       Billed Duration: 6 ms   Memory Size: 128 MB     Max Memory Used: 128 MB
Invalid lambda response received: Lambda response must be valid json       

I tried adjusting permissions to the directory with chmod as per the aws troubleshooting guide: chmod -R o+rX ./lambda-handler

https://docs.aws.amazon.com/lambda/latest/dg/troubleshooting-deployment.html

to no avail,

I am expecting to be able to hit the endpoint and get a valid response.

Upvotes: 0

Views: 49

Answers (1)

PRATHEESH PC
PRATHEESH PC

Reputation: 1981

As mentioned in the comment, It is an issue with the package name. Because the AWS Lambda runtime searches for a main package with a main function as the entry point.

package main: In Go, the package containing func main() must always be named main.

See AWS Lambda function handler in Go

Upvotes: 0

Related Questions