Terraform资源AWS LAMBDA GO错误: “fork/exec /var/task/main: 没有该文件或目录”

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

Terraform resource AWS LAMBDA GO Error : "fork/exec /var/task/main: no such file or directory"

问题

我有一个Go脚本,我正在使用Terraform资源aws_lambda_function进行运行时配置,如下所示:

  handler     = "main"
  memory_size = 512
  timeout     = 360
  runtime     = "go1.x"

在我的Go代码中,我导入了以下模块:

"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"

以下是ecr-sync.go的代码片段:

 func main() {
        lambda.Start(HandleRequest)
    }
    
    func HandleRequest(ctx context.Context, event event.HandleRequest)(string,error) {
      return string(body),err
}

Lambda函数已部署,但在测试函数时,抛出以下错误:

{
  "errorMessage": "fork/exec /var/task/main: no such file or directory",
  "errorType": "PathError"
}

有人知道如何解决这个问题吗?我看到了这篇帖子https://github.com/serverless/serverless/issues/4710,但我不确定如何通过流水线设置构建配置,因为运行时配置是通过terraform设置的。

英文:

I have a go script and I am making Terraform resource aws_lambda_function with runtime configurations as such :

  handler     = "main"
  memory_size = 512
  timeout     = 360
  runtime     = "go1.x"

In my go code, I have imported the modules :

"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/events"

and a snippet of code for ecr-sync.go

 func main() {
        lambda.Start(HandleRequest)
    }
    
    func HandleRequest(ctx context.Context, event event.HandleRequest)(string,error) {
      return string(body),err
}

The lambda function is deployed but while testing the function, it throws me following error:

{
  "errorMessage": "fork/exec /var/task/main: no such file or directory",
  "errorType": "PathError"
}

Anyone know how to fix this issue? I saw this post https://github.com/serverless/serverless/issues/4710 but I am not sure how I can set up the build confifguration through a pipeline as runtime configs are set up through terraform.

答案1

得分: 1

错误的意思是您的 Lambda 的 zip 文件中没有名为 main 的可执行文件。

在 Lambda 的 Go API 中,处理程序必须位于 main 包中,并且必须在 main() 函数中调用,就像您的代码一样。包名和函数名都不需要在任何地方设置。资源中的 handler 设置是指上传的 zip 文件中可执行文件的文件名

从错误信息中可以看出,您的 zip 文件中没有 main 文件。(/var/task 是 Lambda 内部设置的路径)。

是的,部署函数并不会验证其处理程序配置是否与其 zip 文件匹配。该错误发生在运行时。文件名(包括扩展名)无关紧要,但必须与您在 Lambda 配置中指定的处理程序匹配。

要解决此错误,请检查您的 zip 文件,并更新处理程序以指向可执行文件。请记住,Go Lambda 必须进行编译,并且可执行文件必须提供在 zip 文件中 - 与 JavaScript 或 Python 等解释性语言不同,源代码不会放在 zip 文件中。

英文:

> "fork/exec /var/task/main: no such file or directory"

The error means that the executable in your lambda's zip file is not named main.

In the Go API for Lambda, the handler must be in the main package and it must be called in the main() function, just like yours. Neither package nor function name need to be set anywhere. The handler setting in the resource refers to the filename of the executable in the zip file uploaded.

From the error, it is clear that your zipfile does not have a main. (/var/task comes from the internal setup on the lambda side).

> The lambda function is deployed but while testing the function, it throws me following error:

Yes, deploying a function does not verify that its handler configuration matches its zipfile. That error happens at runtime. Filename including extension is irrelevant, but must match the handler you specify in the lambda config.

To fix the error, check your zipfile, and update the handler to point to the executable. Keep in mind that Go lambdas must be compiled and the executable must be provided in the zipfile - unlike interpreted languages like Javascript of Python, source code does not go in the zipfile.

huangapple
  • 本文由 发表于 2021年12月2日 20:32:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/70199536.html
匿名

发表评论

匿名网友

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

确定