[英]Which Golang Lambda Handler do you use to access Serverless Framework Input Key Value Map?
我从另一个问题复制了多个示例,以尝试使用无服务器框架将输入参数传递给 AWS Lambda GoLang function。 function 定义如下:
functions:
schedule:
handler: bin/schedule
package:
artifact: bin/schedule.zip
events:
- schedule:
rate: rate(1 minute)
enabled: true
input:
key: first
- schedule:
rate: rate(1 minute)
input: '{"key": "second"}'
enabled: true
- schedule:
rate: rate(1 minute)
input:
key: 'third'
enabled: true
但是标准的 Lambda Context 似乎没有传递我正在寻找的任何input
值。
那么在调用 function 时,我应该使用哪个 lambda 处理程序 function 来访问first
、 second
或third
值?
我正在尝试通过schedule
function 运行多个 CRON 作业,并且我希望能够区分每晚、每月和每年的作业。 我想使用input
无服务器参数来区分调用。
这是我到目前为止所拥有的:
func Handler(ctx context.Context) error {
fmt.Println(fmt.Sprintf("ctx is [%+v]", ctx))
fmt.Println(fmt.Sprintf("ctx value is [%+v]", ctx.Value("schedule")))
lambdaContext, exists := lambdacontext.FromContext(ctx)
if exists {
fmt.Println(fmt.Sprintf("lambdaContext is [%+v]", lambdaContext))
fmt.Println(fmt.Sprintf("lambdaContext.ClientContext is [%+v]", lambdaContext.ClientContext))
fmt.Println(fmt.Sprintf("lambdaContext.ClientContext.Client is [%+v]", lambdaContext.ClientContext.Client))
fmt.Println(fmt.Sprintf("lambdaContext.ClientContext.Custom is [%+v]", lambdaContext.ClientContext.Custom))
fmt.Println(fmt.Sprintf("lambdaContext.ClientContext.Env is [%+v]", lambdaContext.ClientContext.Env))
} else {
fmt.Println("no lambda context")
}
return nil
}
这是它产生的 output:
2022-09-27T11:39:16.137-05:00 ctx is [context.Background.WithDeadline(2022-09-27 16:39:22.135397493 +0000 UTC [5.998625784s]).WithValue(type *lambdacontext.key, val <not Stringer>).WithValue(type string, val Root=1-63332733-1ebfc3d83a8de600668c194d;Parent=28a5686a7181409c;Sampled=0)]
2022-09-27T11:39:16.137-05:00 ctx value is [<nil>]
2022-09-27T11:39:16.137-05:00 lambdaContext is [&{AwsRequestID:914686cf-e4eb-4e04-a0d8-58f5ea60247b InvokedFunctionArn:arn:aws:lambda:us-east-2:631520639713:function:laguna-seca-staging-schedule Identity:{CognitoIdentityID: CognitoIdentityPoolID:} ClientContext:{Client:{InstallationID: AppTitle: AppVersionCode: AppPackageName:} Env:map[] Custom:map[]}}]
2022-09-27T11:39:16.137-05:00 lambdaContext.ClientContext is [{Client:{InstallationID: AppTitle: AppVersionCode: AppPackageName:} Env:map[] Custom:map[]}]
2022-09-27T11:39:16.137-05:00 lambdaContext.ClientContext.Client is [{InstallationID: AppTitle: AppVersionCode: AppPackageName:}]
2022-09-27T11:39:16.137-05:00 lambdaContext.ClientContext.Custom is [map[]]
2022-09-27T11:39:16.137-05:00 lambdaContext.ClientContext.Env is [map[]]
如您所见,没有提到first
、 second
或third
。 我希望能够在.Env
.Custom
中看到值,但它们都是空的。
好吧,几个小时后我终于想通了。 如果您想了解更多关于此的信息,这是我的思考过程。
Once I saw that it was trigged by CloudWatch to have manual JSON that got me thinking that I need to have a value of json
or string
or map
in one of the AWS Lambda GoLang Event handlers . 问题是没有一个 cloudwatch 事件正确解组了我需要的值。 这让我想到:为什么不试试我自己的处理程序 function 的值呢? 这是AWS Lambda GoLang 处理程序文档中批准的处理程序调用之一。 所以我创建了我自己的 object 并且效果很好。 这是每晚和每月 CRON 计划的无服务器框架 AWS Lambda GoLang function 的最终实现:
无服务器。yaml:
functions:
...
schedule:
handler: bin/schedule
package:
artifact: bin/schedule.zip
events:
- schedule:
rate: rate(1 day)
input:
key: 'nightly'
enabled: true
- schedule:
rate: rate(30 days)
input:
key: 'monthly'
enabled: true
...
main.go:
package main
import (
...
)
func init() {
// connect to your database here
}
func main() {
lambda.Start(Handler)
}
type MyObject struct {
Key string `json:"key"`
}
func Handler(_ context.Context, myObject MyObject) error {
fmt.Println(fmt.Sprintf("event is [%+v]", myObject))
return nil
}
并在调用时打印: nightly
按分钟计划, monthly
按小时计划。
问题未解决?试试以下方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.