I use API Gatway to trigger Lambda with proxy integration
I build a lambda container image for Golang from public.ecr.aws/lambda/provided:al2 because of depedency that cannot be installed in public.ecr.aws/lambda/go:latest.
PFB for my Docerfile
content
FROM public.ecr.aws/lambda/provided:al2
COPY ./config/yumrepo/dep1.repo /etc/yum.repos.d/dep1.repo
COPY ./config/yumrepo/dep2.repo /etc/yum.repos.d/dep2.repo
RUN yum install -y dep1 dep2
COPY --from=build /main /var/runtime/bootstrap # If I dont copy to bootstrap the lambda is not starting up
CMD [ "handler" ]
The problem I am facing is that the events are in marshalled state. If I make an api call to the lambda the intended function, which expects it as a events.APIGatewayProxyRequest
throws error since the input is of type map[string]interface{}
.
My guess is that this is someting to do with runtime interface clients and bootstrap. I got the following reference from AWS Lambda guide for the same
AWS does not provide a separate runtime interface client for Go. The aws-lambda-go/lambda package includes an implementation of the runtime interface.
The above image get build and with the following code made the API work.
func (h *Handler) HandleRequest(ctx context.Context, request interface{}) (interface{}, error) {
requestMap := request.(map[string]interface{})
_, ok := getMapValue(requestMap, "headers")
if ok {
httpMethod, _ := getStringValue(requestMap, "httpMethod")
resource, _ := getStringValue(requestMap, "resource")
body, _ := getStringValue(requestMap, "body")
requestObj := events.APIGatewayProxyRequest{
Body: body,
IsBase64Encoded: false,
Resource: resource,
HTTPMethod: httpMethod,
}
return h.HandleAPIRequest(ctx, requestObj)
}
return nil, fmt.Errorf("unknown request type")
}
Is this the proper way to build the image and how to recive event in AWS defined types in my code?
Found the issue
since the handler function expectes interface
the request
is passed as map[string]interface{}
after I changed the type of the request
parameter to events.APIGatewayProxyRequest
my code automatically started to receive in this type.