exec format error when running AWS Golang Lambda
Asked Answered
C

2

42

I have a go application, structured like this:

cmd
|
reports
|
main.go

main.go imports internal/reports package and has a single function, main(), which delegates call to aws-lambda-go/lambda.Start() function.

Code is build running the commands (snippet):

cd internal/reports && go build handler.go
cd ../..
go build -o reports ../cmd/reports/main.go && chmod +x reports && zip reports.zip reports

reports.zip is uploaded to AWS Lambda, which in turns throws an error when Test button is pressed:

{
  "errorMessage": "fork/exec /var/task/reports: exec format error",
  "errorType": "PathError"
}

reports is set as Lambda's Handler.

Also, code is build on Ubuntu 14.04 machine, as a part of aws/codebuild/ubuntu-base:14.04 Docker Image, on AWS CodeBuild. There should be no environment issues here, even though the error suggests a cross-platform problem.

Any ideas?

Caryophyllaceous answered 5/6, 2018 at 13:12 Comment(0)
C
39

The issue is that main() function is not declared in main package, which is mandatory by Golang language spec

Caryophyllaceous answered 5/6, 2018 at 13:39 Comment(3)
Thanks, this would have been really tough to catch given the error message.Spillage
I completely missed your comment and wasted hours on this! Thank you for sharing. It was exactly this!Corker
I have the same issue and i also declare main() functionAdept
T
87

You have to build with GOARCH=amd64 GOOS=linux. Wherever you build your binary, the binary for Lambda is run on Amazon Linux.

So , try this build command.

GOARCH=amd64 GOOS=linux go build handler.go

Taboo answered 12/12, 2018 at 3:47 Comment(4)
hmm build in Windows 10 only works for me when "GOOS=", and then I have to set "GOARCH=amd64 GOOS=linux" to get build-lambda-zip.exe to workIbadan
running "go get -u golang.org/x/crypto/md4" fixed the issue for me, not sure why I had to do thatIbadan
docs.aws.amazon.com/lambda/latest/dg/… gave the details I needed on Windows, but this answer pointed me thereAetna
A lot of the Amazon documentation and online walkthroughs skip/ignore the GOARCH=amd64 piece. If you're on one of the new Mac M1 chips this is required.Incisive
C
39

The issue is that main() function is not declared in main package, which is mandatory by Golang language spec

Caryophyllaceous answered 5/6, 2018 at 13:39 Comment(3)
Thanks, this would have been really tough to catch given the error message.Spillage
I completely missed your comment and wasted hours on this! Thank you for sharing. It was exactly this!Corker
I have the same issue and i also declare main() functionAdept

© 2022 - 2024 — McMap. All rights reserved.