I am actually facing a very rare issue in golang lambda invocation using terraform. So basically i am deploying all my resources using terraform like lambda, api gateway with golang. issue is when i deploy my golang lambda binary .zip file to lambda using terraform upon checking request with route it returns exec format error. all the code of terraform is fine and formating as i tested simple js lambda function it works fine. i guess its an issue with binary architecture but i am using same binary arch as of lambda using on aws. i also use provide.al2 same issue with exec format error if anyone can help.
实际上,我在使用terraform调用golang lambda时遇到了一个非常罕见的问题。因此,基本上我使用Terraform(如lambda)部署我所有的资源,API网关和Golang。问题是,当我使用terraform将我的golang lambda二进制.zip文件部署到lambda时,在使用route检查请求时,它返回exec格式错误。terraform的所有代码都很好,并且在我测试简单的js lambda函数时,它工作得很好。我猜这是二进制架构的问题,但我使用的是与AWS上使用的lambda相同的二进制拱门。我也使用provide.al2同样的问题与exec格式错误,如果有人可以帮助。
some info for debugging
一些可供调试的信息
1- dir structure
一维结构
- infra
-- helloGO
-- main.go
-- main // binary file
-- terraform
-- main.tf
-- hello.zip // with main binary file
2- terraform lambda function resource
2-terraform lambda函数资源
resource "aws_lambda_function" "hello" {
function_name = "hello"
filename = "../hello.zip" // taking filename from root ./hello.zip
runtime = "go1.x" # nodejs16.x go1.x
handler = "main" # function.handler
role = aws_iam_role.hello_lambda_exec.arn
timeout = 3
}
3- build command
3-构建命令
buildGO: cleanGO
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 cd ./helloGo/ && go build -o ./ main.go
cd ./helloGo/ && chmod +x main
cd ./helloGo/ && zip ../hello.zip main
This creates build file main and package that to root as main.zip for lambda terraform file to use.
(amd64 is executable of x86_64)
这将创建构建文件main,并将其打包为要使用的lambda terraform文件的根目录为main.zip。(AMD64是x86_64的可执行文件)
i tried provided solutions like build with amd64 but dont know it lambda invocation go says binary not executable with this executable seems like
我尝试了提供的解决方案,如使用AMD64构建,但不知道它的lambda调用Go说二进制不可执行的这个可执行文件看起来像
更多回答
优秀答案推荐
i guess its an issue with binary architecture but i am using same binary arch as of lambda using on aws
You've got the right environment settings but you're setting them for the wrong command.
您获得了正确的环境设置,但您为错误的命令设置了它们。
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 cd ./helloGo/ && go build -o ./ main.go
This line sets GOOS, GOARCH, and CGO_ENABLED for the cd
command, not for go build
.
该行为cd命令设置GOOS、GOARCH和CGO_ENABLED,而不是GO BUILD。
Try this:
试试这个:
cd ./helloGo/ && GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o ./ main.go
更多回答
Thanks for the help i actually missed that part also i was not using hash to generate new s3 bucket but real problem was that you mentioned thanks a ton
谢谢你的帮助,我实际上错过了这一部分,而且我没有使用散列来生成新的S3存储桶,但真正的问题是你提到了很多谢谢
我是一名优秀的程序员,十分优秀!