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

我使用 API Gatway 通过代理集成触发 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{}.


我的猜测是,这与运行时接口客户端和引导程序有关。我从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 定义的类型接收事件吗?


慕虎7371278
浏览 105回答 1
1回答

当年话下

发现问题因为处理函数期望interface在我将参数 的类型更改为我的代码自动开始以这种类型接收之后request传递。map[string]interface{}requestevents.APIGatewayProxyRequest
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go