英文:
How can I get the AWS Lambda Alias in my Go function?
问题
我想在主函数开始执行之前运行一些特定于Lambda别名的代码。目前代码如下:
func init() {
// 在这里尝试获取Lambda函数别名
}
func main() {
adapter = chiproxy.New(r)
lambda.Start(lambdaHandler)
}
func lambdaHandler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
c, err := adapter.ProxyWithContext(ctx, req)
return c, err
}
在init()
函数中,我不知道如何获取lambdaHandler
中的req
参数。
英文:
I want to run some Lambda alias specific code before my main function starts executing. It currently looks like this
func init() {
// Trying to get Lambda function alias here
}
func main() {
adapter = chiproxy.New(r)
lambda.start(lambdaHandler)
}
func lambdaHandler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
c, err := adapter.ProxyWithContext(ctx, req)
return c, err
}
req
in lambdaHandler
has the info I need but I don't know how to get it in init()?
答案1
得分: 1
我认为在函数传递请求之前,你无法获取别名。Lambda会自动提供一些环境变量,但别名不是其中之一。
这可能是因为别名只是指向版本的指针;你可以有多个别名指向同一个版本,但相同的函数运行时可以为它们所有的别名初始化。因此,在初始化时提供任何特定的别名是没有意义的。
我想在主函数开始执行之前运行一些特定于Lambda别名的代码。
但函数的运行时不是特定于别名的,而是特定于版本的。实际上,你可以在版本实例化之后创建一个新的别名,并且可以使用相同的缓存运行时。
我可以理解为什么你希望调用者使用不同的别名调用相同的函数,但我不确定使用别名来实现你想要的目标是否会很容易。考虑一下是否可以创建一堆不同的函数,它们可以有相同的代码库,并且可以根据函数的名称而不是别名来选择适当的处理程序或执行其他初始化操作。
英文:
I don't think you can get the alias before the function has been passed a request. lambda automatically provides a number of environment variables but alias is not one of them.
That's probably because the alias is just a pointer to the version; you could have many aliases pointing to the same version, but the same function runtime could be initialized for all of them. So it wouldn't make sense to provide any particular alias at initialization time.
> I want to run some Lambda alias specific code before my main function starts executing
But the function's runtime isn't specific to an alias, it's specific to a version. In fact you could create a new alias after the version had been instantiated, and the same cached runtime could be used.
I think I can see why you might want to have invokers call the same function with different aliases, but I'm not sure it's going to be a low friction path to achieve what you want with aliases. Consider whether you might instead want to create bunch of different functions - which could have the same codebase, and have init choose a proper handler or do other init based on the function name instead of its alias.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论