英文:
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
或其他。
TIn
和TOut
的正确类型是什么 => events.APIGatewayV2HTTPRequest
和events.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特定元数据。
> TIn
和TOut
的正确类型是什么?
这取决于谁调用Lambda函数。当Lambda函数由另一个AWS服务调用时,包括API Gateway时,所谓的TIn
和TOut
是来自lambda event
包的类型。引用自该包的介绍:
> 该包提供了用于处理AWS事件的Lambda函数的输入类型。
对于API Gateway,这将是events.APIGatewayProxyRequest
和Response
,或者对于Payload format version 2
,这将是events.APIGatewayV2HTTPRequest
和Response
- 这些在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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论