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

huangapple go评论83阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2021年11月30日 21:27:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/70170093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定