aws-sam-cli: fork/exec /var/task/main: permission denied

I successfully deployed a golang lambda api service using sam package and sam deploy.

And when I test the lambda function, I get the error below. { "errorMessage": "fork/exec /var/task/main: permission denied", "errorType": "PathError" }

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 11
  • Comments: 26 (2 by maintainers)

Most upvoted comments

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)
}

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

@mikeatlas That worked for me! I just added the -j flag to zip for junking the file path.

When I set the CodeUri: main the packaged file has permissions as -rw-r--r--@ but when I set the CodeUri: . to the code directory, the binary has the correct permission when packaged -rwxr-xr-x@

Yes, built using GOOS=linux go build -o main on a Mac -rwxr-xr-x 1 terry staff 8087634 24 Jan 14:37 main

Hey @terrywarwar @PaulMaddox I have a similar situation but I get:

{
  "errorMessage": "fork/exec /var/task/main: no such file or directory",
  "errorType": "PathError"
}

when I run sam local start-api --template sam.yml

Handler is set to main

I think my problem is similar, but I hope to get to the permission problem 😃

I had the same issue using aws cloudformation, I wonder if this causing the issue when CodeUri is set to a file instead of a directory. https://github.com/aws/aws-cli/blob/e354da887af26c3a4546026e743f10bbf851eecb/awscli/customizations/cloudformation/artifact_exporter.py#L212

shutil.copyfile doesn’t copy the meta-data.