aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

Bug: `sam local start-api` on macOS with Go runtime causes `fork/exec /var/task/main: no such file or directory: PathError`

Open hexcowboy opened this issue 8 months ago • 1 comments

Description:

When trying to invoke a serverless function locally on the sam local start-api, I'm seeing this error: fork/exec /var/task/main: no such file or directory: PathError

Steps to reproduce:

./template.yaml

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: >
  AWS SAM Template for two Go-based Lambda functions.

Globals:
  Function:
    Timeout: 30
    MemorySize: 512

Resources:
  HelloFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: handlers/hello/
      Handler: main
      Runtime: go1.x
      Events:
        HelloAPI:
          Type: Api
          Properties:
            Path: /hello
            Method: get

./handlers/hello/main.go

package main

import (
	"context"

	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
)

func Hello(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
	return events.APIGatewayProxyResponse{
		StatusCode: 200,
		Body:       "Hello",
	}, nil
}

func main() {
	lambda.Start(Hello)
}

Observed result:

sam local start-api --debug

2025-04-09 18:29:32,572 | No config file found in this directory.                                                       
2025-04-09 18:29:32,573 | OSError occurred while reading TOML file: [Errno 2] No such file or directory:                
'/Users/jack/repos/juno-mobile/services/lambda/samconfig.toml'                                                          
2025-04-09 18:29:32,574 | Config file location: /Users/jack/repos/juno-mobile/services/lambda/samconfig.toml            
2025-04-09 18:29:32,574 | Config file '/Users/jack/repos/juno-mobile/services/lambda/samconfig.toml' does not exist     
2025-04-09 18:29:32,575 | Using SAM Template at /Users/jack/repos/juno-mobile/services/lambda/template.yaml             
2025-04-09 18:29:32,584 | OSError occurred while reading TOML file: [Errno 2] No such file or directory:                
'/Users/jack/repos/juno-mobile/services/lambda/samconfig.toml'                                                          
2025-04-09 18:29:32,585 | Using config file: samconfig.toml, config environment: default                                
2025-04-09 18:29:32,585 | Expand command line arguments to:                                                             
2025-04-09 18:29:32,585 | --template_file=/Users/jack/repos/juno-mobile/services/lambda/template.yaml --host=127.0.0.1  
--port=3000 --static_dir=public --layer_cache_basedir=/Users/jack/.aws-sam/layers-pkg --container_host=localhost        
--container_host_interface=127.0.0.1                                                                                    
2025-04-09 18:29:32,625 | local start-api command is called                                                             
2025-04-09 18:29:34,711 | No current session found, using default AWS::AccountId                                        
2025-04-09 18:29:34,712 | No Parameters detected in the template                                                        
2025-04-09 18:29:34,722 | There is no customer defined id or cdk path defined for resource HelloFunction, so we will use
the resource logical id as the resource id                                                                              
2025-04-09 18:29:34,723 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will
use the resource logical id as the resource id                                                                          
2025-04-09 18:29:34,723 | 0 stacks found in the template                                                                
2025-04-09 18:29:34,724 | No Parameters detected in the template                                                        
2025-04-09 18:29:34,731 | There is no customer defined id or cdk path defined for resource HelloFunction, so we will use
the resource logical id as the resource id                                                                              
2025-04-09 18:29:34,731 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will
use the resource logical id as the resource id                                                                          
2025-04-09 18:29:34,731 | 2 resources found in the stack                                                                
2025-04-09 18:29:34,732 | Found Serverless function with name='HelloFunction' and CodeUri='handlers/hello/'             
2025-04-09 18:29:34,732 | --base-dir is not presented, adjusting uri handlers/hello/ relative to                        
/Users/jack/repos/juno-mobile/services/lambda/template.yaml                                                             
2025-04-09 18:29:34,736 | Found '1' API Events in Serverless function with name 'HelloFunction'                         
2025-04-09 18:29:34,737 | Detected Inline Swagger definition                                                            
2025-04-09 18:29:34,737 | Parsing Swagger document using 2.0 specification                                              
2025-04-09 18:29:34,737 | Found '0' authorizers in resource 'ServerlessRestApi'                                         
2025-04-09 18:29:34,737 | Lambda function integration not found in Swagger document at path='/hello' method='get'       
2025-04-09 18:29:34,737 | Found '0' APIs in resource 'ServerlessRestApi'                                                
2025-04-09 18:29:34,738 | Authorizer not found or disabled, returning early                                             
2025-04-09 18:29:34,738 | Removed duplicates from '0' Explicit APIs and '1' Implicit APIs to produce '1' APIs           
2025-04-09 18:29:34,738 | 1 APIs found in the template                                                                  
2025-04-09 18:29:34,740 | Mounting HelloFunction at http://127.0.0.1:3000/hello [GET]                                   
2025-04-09 18:29:34,740 | You can now browse to the above endpoints to invoke your functions. You do not need to        
restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. If you used  
sam build before running local commands, you will need to re-run sam build for the changes to be picked up. You only    
need to restart SAM CLI if you update your AWS SAM template                                                             
2025-04-09 18:29:34,741 | Localhost server is starting up. Multi-threading = True                                       
2025-04-09 18:29:34,741 | Setting SIGTERM interrupt handler                                                             
2025-04-09 18:29:34 WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:3000
2025-04-09 18:29:34 Press CTRL+C to quit
2025-04-09 18:29:38,021 | Constructed Event 1.0 to invoke Lambda. Event: {'httpMethod': 'GET', 'body': None, 'resource':
'/hello', 'requestContext': {'resourceId': '123456', 'apiId': '1234567890', 'resourcePath': '/hello', 'httpMethod':     
'GET', 'requestId': '664161e6-d143-4edd-a394-99f608ef2967', 'accountId': '123456789012', 'stage': 'Prod', 'identity':   
{'apiKey': None, 'userArn': None, 'cognitoAuthenticationType': None, 'caller': None, 'userAgent': 'Custom User Agent    
String', 'user': None, 'cognitoIdentityPoolId': None, 'cognitoAuthenticationProvider': None, 'sourceIp': '127.0.0.1',   
'accountId': None}, 'extendedRequestId': None, 'path': '/hello', 'protocol': 'HTTP/1.1', 'domainName': '127.0.0.1:3000',
'requestTimeEpoch': 1744244972, 'requestTime': '10/Apr/2025:00:29:32 +0000'}, 'queryStringParameters': None,            
'multiValueQueryStringParameters': None, 'headers': {'Host': '127.0.0.1:3000', 'User-Agent': 'curl/8.7.1', 'Accept':    
'*/*', 'X-Forwarded-Proto': 'http', 'X-Forwarded-Port': '3000'}, 'multiValueHeaders': {'Host': ['127.0.0.1:3000'],      
'User-Agent': ['curl/8.7.1'], 'Accept': ['*/*'], 'X-Forwarded-Proto': ['http'], 'X-Forwarded-Port': ['3000']},          
'pathParameters': None, 'stageVariables': None, 'path': '/hello', 'isBase64Encoded': False}                             
2025-04-09 18:29:38,024 | Found one Lambda function with name 'HelloFunction'                                           
2025-04-09 18:29:38,025 | Invoking main (go1.x)                                                                         
2025-04-09 18:29:38,025 | No environment variables found for function 'HelloFunction'                                   
2025-04-09 18:29:38,026 | Loading AWS credentials from session with profile 'None'                                      
2025-04-09 18:29:40,039 | Resolving code path. Cwd=/Users/jack/repos/juno-mobile/services/lambda,                       
CodeUri=/Users/jack/repos/juno-mobile/services/lambda/handlers/hello                                                    
2025-04-09 18:29:40,043 | Resolved absolute path to code is /Users/jack/repos/juno-mobile/services/lambda/handlers/hello
2025-04-09 18:29:40,044 | Resolving code path. Cwd=/Users/jack/repos/juno-mobile/services/lambda,                       
CodeUri=/Users/jack/repos/juno-mobile/services/lambda/handlers/hello                                                    
2025-04-09 18:29:40,045 | Resolved real code path to /Users/jack/repos/juno-mobile/services/lambda/handlers/hello       
2025-04-09 18:29:40,046 | Code /Users/jack/repos/juno-mobile/services/lambda/handlers/hello is not a zip/jar file       
2025-04-09 18:29:41,159 | Local image is up-to-date                                                                     
2025-04-09 18:29:41,192 | Checking free port on 127.0.0.1:7323                                                          
2025-04-09 18:29:41,211 | Using local image: public.ecr.aws/lambda/go:1-rapid-x86_64.                                   
                                                                                                                        
2025-04-09 18:29:41,212 | Mounting /Users/jack/repos/juno-mobile/services/lambda/handlers/hello as                      
/var/task:ro,delegated, inside runtime container                                                                        
2025-04-09 18:29:41,442 | Starting a timer for 30 seconds for function 'HelloFunction'                                  
2025-04-09 18:29:41,445 | Getting lock for the key localhost-7323                                                       
2025-04-09 18:29:41,446 | Waiting to retrieve the lock (localhost-7323) to start invocation                             
START RequestId: b4b7aef2-9968-45a0-b25f-d5f44985a236 Version: $LATEST
fork/exec /var/task/main: no such file or directory: PathError
null
END RequestId: c63c88fa-afb1-47ab-8b0a-8faf7ea8d81a
REPORT RequestId: c63c88fa-afb1-47ab-8b0a-8faf7ea8d81a	Init Duration: 0.31 ms	Duration: 83.68 ms	Billed Duration: 84 ms	Memory Size: 512 MB	Max Memory Used: 512 MB	

2025-04-09 18:29:41,544 | [Container state] OOMKilled False                                                             
2025-04-09 18:29:41,671 | Cleaning all decompressed code dirs                                                           
2025-04-09 18:29:41,672 | Unable to find Click Context for getting session_id.                                          
2025-04-09 18:29:41 127.0.0.1 - - [09/Apr/2025 18:29:41] "GET /hello HTTP/1.1" 500 -

Expected result:

The local start-api command should just work without errors.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: macOS
  2. sam --version: SAM CLI, version 1.137.0
  3. AWS region: not applicable
➜ sam --info
{
  "version": "1.137.0",
  "system": {
    "python": "3.11.10",
    "os": "macOS-15.3.2-arm64-arm-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "28.0.4",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}

hexcowboy avatar Apr 10 '25 00:04 hexcowboy

Based on this issue https://github.com/aws/aws-sam-cli/issues/4784 it seems you must run sam build before running the local api with compiled languages. I will leave this open as its very misleading for the docs to say "This functionality features hot reloading so that you can quickly develop and iterate over your functions."

Here is a workaround using nodemon:

{
  "name": "@repo/lambda",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "build": "sam build",
    "dev": "nodemon --watch handlers --ext go --exec \"sam build && sam local start-api\""
  },
  "devDependencies": {
    "nodemon": "^3.1.9"
  }
}

hexcowboy avatar Apr 10 '25 19:04 hexcowboy

Thank you, @hexcowboy for the answer. We have to run sam build again to update the resource in .aws-sam. Unfortunately, we do not support watch feature for compile language yet. You may submit a request or +1 an existing feature request to help us prioritize the feature.

vicheey avatar Jun 23 '25 06:06 vicheey