一个Go二进制文件中可能有多个lambda函数吗?

一个Go二进制文件中可能有多个lambda函数吗?

问题描述:

I am experimenting with Go on AWS lambda, and i found that each function requires a binary to be uploaded for execution.

My question is that, is it possible to have a single binary that can have two different Handler functions, which can be loaded by two different lambda functions.

for example

func Handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    fmt.Println("Received body in Handler 1: ", request.Body)

    return events.APIGatewayProxyResponse{Body: request.Body, StatusCode: 200}, nil
}
func Handler1(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    fmt.Println("Received body in Handler 2: ", request.Body)

    return events.APIGatewayProxyResponse{Body: request.Body, StatusCode: 200}, nil
}

func EndPoint1() {
    lambda.Start(Handler)
}
func EndPoint2() {
    lambda.Start(Handler1)
}

and calling the EndPoints in main in such a way that it registers both the EndPoints and the same binary would be uploaded to both the functions MyFunction1 and MyFunction2.

I understand that having two different binary is good because it reduces the load/size of each function.

But this is just an experimentation.

Thanks in advance :)

我正在使用AWS lambda上的Go进行实验,我发现每个函数都需要上传一个二进制文件才能执行。 p>

我的问题是,是否可能有一个可以具有两个不同的 Handler code>函数的二进制文件,而该函数可以由两个不同的lambda函数加载。 / p>

p>

  func Handler(request events.APIGatewayProxyRequest)(events.APIGatewayProxyResponse,error){
 fmt.Println(“ Received 处理程序1中的主体,“,request.Body)
 
返回events.APIGatewayProxyResponse {Body:request.Body,StatusCode:200},nil 
} 
func Handler1(request events.APIGatewayProxyRequest)(events.APIGatewayProxyResponse,error  ){
 fmt.Println(“处理程序2中的接收主体,”,request.Body)
 
返回事件。APIGatewayProxyResponse{Body:request.Body,StatusCode:200},nil 
} 
 
func EndPoint1  (){
 lambda.Start(Handler)
} 
func EndPoint2(){
 lambda.Start(Handler1)
} 
  c  ode>  pre> 
 
 

并在 main code>中调用 EndPoints code>,以这样的方式注册两个 EndPoints code> 并且将相同的二进制文件上传到函数 MyFunction1 code>和 MyFunction2 code>中。 p>

我知道拥有两个不同的二进制文件是件好事,因为 p>

但这只是一个实验。 p>

在此先感谢:) p> DIV>

I believe it is not possible since Lambda console says the handler is the name of the executable file:

Handler: The executable file name value. For example, "myHandler" would call the main function in the package “main” of the myHandler executable program.

So one single executable file is unable to host two different handlers.

you can do the workaround to have same executable to upload to two different lambda like following

func main() {
switch cfg.LambdaCommand {
case "select_lambda_1":
    lambda1handler()
case "select_lambda_2":
    lambda2handler()