php editor Xinyi introduces you to a common problem: "exec format error in go aws-lambda and terraform". When creating functions using aws-lambda and terraform, you may encounter issues with Exec format errors. This may be due to errors in the function code or terraform configuration. This article will help you understand and solve this problem so that you can successfully create and run aws-lambda functions.
I actually encountered a very rare problem when calling golang lambda using terraform. So basically I use terraform to deploy all my resources like lambda, api gateway with golang. The problem is that when I deploy golang lambda binary .zip file to lambda using terraform, when checking the request using exec route, it returns exec format error. All code for terraform is nice and formatted as I tested simple js lambda function and it works fine. I'm guessing it's a binary architecture issue, but I'm using the same binary architecture as the lambda I use on aws. If anyone can help, I also had the same issue using Provide.al2 and got an exec format error.
Some debugging information
1-Directory structure
- infra -- helloGO -- main.go -- main // binary file -- terraform -- main.tf -- hello.zip // with main binary file
2- terraform lambda function resource
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
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 will create the build file main and package it as main.zip for use by the lambda terraform file. (amd64 is the executable file of x86_64)
I tried to provide solutions such as building with amd64 but not knowing lambda calling go says the binary cannot be executed with this executable
Your environment The settings are correct, but you set them for the wrong command.
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 cd ./helloGo/ && go build -o ./ main.go
This behavior sets GOOS, GOARCH, and CGO_ENABLED for the cd
command, not for the go build
.
Try this:
cd ./helloGo/ && GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o ./ main.go
The above is the detailed content of exec format error in go aws-lambda and terraform. For more information, please follow other related articles on the PHP Chinese website!