英文:
How to use unmarshal events to AWS defined types in lambda container image for go1.x with provided as base
问题
我使用API Gateway来触发具有代理集成的Lambda函数。
我使用public.ecr.aws/lambda/provided:al2构建了一个用于Golang的Lambda容器镜像,因为在public.ecr.aws/lambda/go:latest中无法安装某些依赖项。
以下是我的Dockerfile
内容:
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 # 如果我不复制到bootstrap,Lambda函数将无法启动
CMD ["handler"]
我遇到的问题是事件处于编组状态。如果我对Lambda函数进行API调用,预期的函数将以events.APIGatewayProxyRequest
类型接收输入,但会抛出错误,因为输入是map[string]interface{}
类型。
我猜测这与运行时接口客户端和引导有关。我从AWS Lambda指南中找到了以下参考资料:
AWS不为Go提供单独的运行时接口客户端。aws-lambda-go/lambda包包含了运行时接口的实现。
上述镜像已构建,并使用以下代码使API正常工作。
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")
}
这是构建镜像和在代码中接收AWS定义类型的事件的正确方式吗?
英文:
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?
答案1
得分: 0
发现了问题
由于处理程序函数期望 interface
,在我将 request
参数的类型更改为 events.APIGatewayProxyRequest
后,我的代码自动开始以这种类型接收。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论