ibrahim jamil
ibrahim jamil

Reputation: 106

exec format error in go aws-lambda with terraform

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.

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

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 creates build file main and package that to root as main.zip for lambda terraform file to use. (amd64 is executable of 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

Upvotes: 1

Views: 389

Answers (1)

erik258
erik258

Reputation: 16302

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.

Try this:

cd ./helloGo/ && GOOS=linux GOARCH=amd64 CGO_ENABLED=0  go build -o ./ main.go

Upvotes: 1

Related Questions