亚马逊 API 网关 HTTP API:在 Go 中的 Lambda 函数中使用自定义类型

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

Amazon API Gateway HTTP API: Custom types in lambda functions in go

问题

我有点困惑如何在使用golang编写的Lambda函数中通过HttpApi传递自定义类型。

考虑以下go lambda处理程序,它几乎是从文档中的示例复制而来的。

type MyRequestType struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

type MyResponseType struct {
    Message string `json:"message"`
}

func handler(request MyRequestType) (MyResponseType, error) {
    log.Printf("received request: %v", request)
    return MyResponseType{Message: fmt.Sprintf("Hello %s, you are %d years old!", request.Name, request.Age)}, nil
}

func main() {
    lambda.Start(handler)
}

结果消息始终如下。

{
    "message": "Hello , you are 0 years old!"
}

我有一种模糊的感觉,在Amazon API Gateway HTTP API中可能无法实现这一点。
但我也没有找到任何指出这是不可能的文档。所以我真的很想知道,我是否做错了什么?

文档还提到了有效的签名:

例如 func (context.Context, TIn) (TOut, error)

如果我在Payload format version 2中使用HTTP API

context.Context是正常的golang context还是特殊的东西?我在考虑events.APIGatewayV2HTTPRequestContext或其他。

TInTOut的正确类型是什么 => events.APIGatewayV2HTTPRequestevents.APIGatewayV2HTTPResponse

英文:

I am a little bit confused about how to get custom types passed into my Lambda function using golang and sitting behind a HttpApi.

Consider the following go lambda handler, which is almost a copy of the example from the documentation.


type MyRequestType struct {
	Name string `json:"name"`
	Age  int    `json:"age"`
}

type MyResponseType struct {
	Message string `json:"message"`
}

func handler(request MyRequestType) (MyResponseType, error) {
	log.Printf("received request: %v", request)
	return MyResponseType{Message: fmt.Sprintf("Hello %s, you are %d years old!", request.Name, request.Age)}, nil
}

func main() {
	lambda.Start(handler)
}

The resulting message is always the following.

{
    "message": "Hello , you are 0 years old!"
}

I have the dump feeling that this is not possible in Amazon API Gateway HTTP API.
But I also haven't found any documentation pointing out, that this is not possible. So I really wonder, if I am doing something wrong?

The documentation says also something about the valid signatures:

For example func (context.Context, TIn) (TOut, error)

If I am using a HTTP API with the Payload format version 2:

Is the context.Context the normal golang context or something special?
I am thinking of events.APIGatewayV2HTTPRequestContext or others.

What would be the right type of TIn and TOut => events.APIGatewayV2HTTPRequest and events.APIGatewayV2HTTPResponse?

答案1

得分: 1

> context.Context是正常的Golang上下文吗?

是的。

但是你可以使用lambdacontext.FromContext来获取Lambda上下文,其中包含了额外的Lambda特定元数据。

> TInTOut的正确类型是什么?

这取决于谁调用Lambda函数。当Lambda函数由另一个AWS服务调用时,包括API Gateway时,所谓的TInTOut是来自lambda event包的类型。引用自该包的介绍:

> 该包提供了用于处理AWS事件的Lambda函数的输入类型。

对于API Gateway,这将是events.APIGatewayProxyRequestResponse,或者对于Payload format version 2,这将是events.APIGatewayV2HTTPRequestResponse - 这些在v1.16.0中添加。

更多文档(但不多)可以在github仓库的README中找到。

英文:

> Is the context.Context the normal golang context

Yes.

But you can get the Lambda context with lambdacontext.FromContext which contains additional lambda-specific metadata.

> What would be the right type of TIn and TOut

It depends on who invokes the Lambda. When the Lambda is invoked by another AWS service, including the API Gateway, the so-called TIn and TOut are types from the lambda event package. Quote from the package introduction:

> This package provides input types for Lambda functions that process AWS events.

In case of the API Gateway, that would be events.APIGatewayProxyRequest and Response, or presumably for the Payload format version 2 the events.APIGatewayV2HTTPRequest and Response — which were added in v1.16.0.

Further documentation (but not much) in the github repo README

huangapple
  • 本文由 发表于 2022年1月19日 23:53:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/70773681.html
匿名

发表评论

匿名网友

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

确定