简体   繁体   中英

How to fix "exec format error" in AWS lambda

I'm writing a little POC and trying to work the best way to organize some lambdas. A service has 3 lambdas:

  1. API
  2. Message handler
  3. Cron

After deploying to AWS, when I test any of the lambdas, I get the following error:

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

In the logs, I find this:

START RequestId: 53a7feac-3a6d-4d18-8909-9c8bf659b727 Version: $LATEST
fork/exec /var/task/cron: exec format error: PathError
null
END RequestId: 53a7feac-3a6d-4d18-8909-9c8bf659b727

This is my project structure:

  • At the root, I got main.go, go.mod and go.sum (it does nothing but it's my attempt to group the packages together and share some dependencies).
  • Then I got a few packages
    • api
    • messagehandler
    • cron
    • shared

Each of these packages (except shared) is called main. See below: 项目结构

Here's how I build the packages:

GOOS=linux
GOARCH=x86_64
echo "GOOS=$GOOS, GOARCH=$GOARCH"
go build -o out/main ./main.go
go build -o out/api ./api/index.go
go build -o out/messagehandler ./messagehandler/index.go
go build -o out/cron ./cron/index.go

The output is what I expected: 导出目录

What I confirmed:

  • can run the apps in my bash console.
  • the lambda handler matches the file name inside the zip.
  • the files are built with environment settings GOOS=linux and GOARCH=x86_64

Lambda 处理程序文件

What is wrong here?

Is your OS Windows?

If so, it is suggested to use the following tool github.com/aws/aws-lambda-go/cmd/build-lambda-zip to prepare the binary to deploy to AWS Lambda.

in cmd.exe:

set GOOS=linux
set GOARCH=amd64
set CGO_ENABLED=0
go build -o bootstrap main.go
%USERPROFILE%\Go\bin\build-lambda-zip.exe -o lambda-handler.zip bootstrap

or in Powershell :

$env:GOOS = "linux"
$env:GOARCH = "amd64"
$env:CGO_ENABLED = "0"
go build -o bootstrap main.go
~\Go\Bin\build-lambda-zip.exe -o lambda-handler.zip bootstrap

More details here: https://github.com/aws/aws-lambda-go

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM