Solvedaws sam cli fork/exec /var/task/main: permission denied
✔️Accepted Answer
Not sure if this is the case here, but I found this issues page while debugging the fork/exec /var/task/main permission denied
issue so thought I'd post here to help others in a similar position.
The issue was that I'd neglected to create a main function and my go package name wasn't main.
package main
import (
"github.com/aws/aws-lambda-go/lambda"
)
func main() {
lambda.Start(MyHandler)
}
Other Answers:
Just leaving this here for anyone who happens to stumble across this:
My zip file had some of my full build path in it from my Makefile. I needed to add -j
switch to zip
to flatten out the file path inside the archive:
build:
${GOBUILD} -ldflags="-d -s -w" -o ${BINPATH}/hello ./cmd/hello/main.go
chmod +x ${BINPATH}/hello
zip -j ${BINPATH}/hello.zip ${BINPATH}/hello
This works when the Lambda handler is specified as hello
.
Here is a nifty little tool to zip up properly: https://github.com/aws/aws-lambda-go#for-developers-on-windows
If anyone got here seeing the same error @neoadventist had
{
"errorMessage": "fork/exec /var/task/main: no such file or directory",
"errorType": "PathError"
}
Also make sure that your are building on glibc if you are using os, runtime or net.
Lambdas are executing with glibc runtime.
This could be relevant if you were building on alpine
for example, which has musl
instead of glibc
.
This case you can also try to link your libs statically instead of dynamically.
This worked for me to build on alpine and run on aws.
(your musl-gcc might be in a different location)
CC=/usr/bin/x86_64-alpine-linux-musl-gcc GOOS=linux go build -x \
-ldflags '-linkmode external -extldflags "-static"' -a -tags netgo \
-installsuffix netgo -o main main.go
@neoadventist I just forgot about building step.
Run this (replace $1 to your actual filename without extension):
GOOS=linux go build -o $1 $1.go
I successfully deployed a golang lambda api service using
sam package
andsam deploy
.And when I test the lambda function, I get the error below.
{ "errorMessage": "fork/exec /var/task/main: permission denied", "errorType": "PathError" }