如何在 go1.x 的 lambda 容器映像中使用 AWS 定义类型的解组事件并提供为基础

How to use unmarshal events to AWS defined types in lambda container image for go1.x with provided as base

我使用 API 网关通过代理集成触发 Lambda

我从 public.ecr.aws/lambda/provided:al2 为 Golang 构建了一个 lambda 容器镜像,因为无法在 public.ecr.aws/lambda/go:latest.

我的 Docerfile 内容的 PFB

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" ]

我面临的问题是事件处于编组状态。如果我对 lambda 进行 api 调用,预期函数将它视为 events.APIGatewayProxyRequest 抛出错误,因为输入的类型为 map[string]interface{}.

我猜这与运行时接口客户端和 bootstrap 有关。我从 AWS Lambda guide 获得了以下参考文献

AWS does not provide a separate runtime interface client for Go. The aws-lambda-go/lambda package includes an implementation of the runtime interface.

上面的图像得到构建并使用以下代码使 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 定义类型的事件?

找到问题

因为处理函数期望 interface 在我将 request 参数的类型更改为 events.APIGatewayProxyRequest 我的代码后 request 作为 map[string]interface{} 传递自动开始接收此类型。