AWS SAM Deployment : Error fork/exec /var/task/myfunction : no such file or directory: PathError
Asked Answered
E

1

7

I have developed a set of Lambda functions in Golang and trying to deploy these functions and API gateway using SAM.

I am creating the executables locally, creating zip file for each of these lambda functions and and uploading these zip files to s3 bucket.

I am giving the reference of this S3 bucket in the SAM template file.

My SAM template file looks as below

myfunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: s3://<<my-bucket>>/bin/handlers/myfunction.zip
      Handler: myfunction
      Role: !GetAtt CFLambdaExecutionRole.Arn
      Events:
        Getcfdemoapi:
          Type: Api
          Properties:
            Path: /myfunction
            Method: get
        CreateCustomer:
          Type: Api
          Properties:
            Path: /myfunction
            Method: post
        UpdateCustomer:
          Type: Api
          Properties:
            Path: /customer
            Method: put
        DeleteCustomer:
          Type: Api
          Properties:
            Path: /myfunction
            Method: delete

The deployment was successful.

I invoked the lambda function through API gateway.

I checked the Cloud watch log and saw below error -

Error fork/exec /var/task/myfunction : no such file or directory: PathError

Is there anything wrong in the SAM template, related to CodeUri and handler?

I am creating the build on MacOS and using the below command for build -

GOOS=linux GOARCH=amd64 go build
Enculturation answered 11/12, 2019 at 13:8 Comment(3)
What is the structure of myfunction.zip?Cutshall
Can you add the function code to your question?Benue
Can you include the steps you did to create the ZIP file? And also the directories/files inside?Manhandle
P
1

This particular error can be caused by a number of issues. The three that come to my mind are:

  1. Your binary is not called myfunction (you use set Handler: myfunction so therefore, the binary has to be named myfunction)
  2. The binary is not in the ZIP archive
  3. The binary is in the ZIP archive but in the wrong path.

To solve this you could run the following commands in the directory that contains your Lambda functions code.


Step 1: Build the binary and explicitly set the name to myfunction

GOOS=linux GOARCH=amd64 go build -o myfunction

If you do not use the -o option the binary will be named after the directory you are building in. So if the directory is not itself named myfunction, the resulting binary will have the wrong name (see possible problem #1 above).

Step 2: Create a ZIP archive

zip --junk-paths myfunction.zip myfunction

The --junk-paths option will remove the path and just put the binary in the "root" of the ZIP archive. The man page says:

Store just the name of a saved file (junk the path), and do not store directory names. By default, zip will store the full path (relative to the current directory).

This is to avoid problem #3 I listed above.

To check if this worked as intended, you can run the following command:

unzip -l myfunction.zip

This should show something like this:

Archive:  myfunction.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
  2142872  12-01-2020 19:19   myfunction
---------                     -------
  2142872                     1 file

Step 3: Upload ZIP archive to S3

To upload the ZIP archive to S3 I would recommend you use the AWS CLI. The AWS console would work as well.

Using the AWS CLI, you can run the following command:

aws s3 cp myfunction.zip s3://<<my-bucket>>/bin/handlers/myfunction.zip

Documentation for this cp command can be found here: cp documentation.

Pigeonhearted answered 1/12, 2020 at 18:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.