Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAM Build Not Mounting Lambda Code in /var/task During sam local start api on Windows #7478

Open
pedrocimp opened this issue Sep 13, 2024 · 5 comments
Labels
area/docker blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. platform/windows

Comments

@pedrocimp
Copy link

pedrocimp commented Sep 13, 2024

Description:

When using npm run build and running sam local start api or sam local invoke, SAM builds the Lambda image on the fly. However, it seems that the Lambda code is not being mounted correctly in /var/task, which results in an empty directory. This prevents the Lambda from executing properly.

Context:

I expect SAM to mount the built Lambda sources from dist/ into /var/task inside the container. However, the /var/task directory is empty, making it impossible to execute the Lambda function via sam local start api.

I've tried multiple solutions but couldn't resolve this issue. It might be related to switching from a Linux development environment to a Windows environment.

In my template.yml, I point the CodeUri to the compiled code in the dist/ directory, like this:

CodeUri: dist/lambdas/myFunction/

Expected behavior:

The compiled Lambda code should be mounted into /var/task inside the container to allow the Lambda function to execute properly.

Environment:

  • Operating System: Windows
  • SAM CLI Version: Tested with version 1.120
  • Docker Version: 26.1.0-rd
  • Docker setup: Running on WSL2
  • Custom Lambda Image: Defined in template.yml

Steps to reproduce:

  1. Run npm run build. [npx tsc --project tsconfig.build.json]
  2. Start the Lambda function using either sam local start api or sam local invoke.
  3. Inspect the container, and observe that /var/task is empty.

Possible suggestions or fixes:

  • Ensure that SAM properly mounts the Lambda code into the /var/task directory during execution on Windows.
  • Investigate if the issue is related to file mounting differences between Linux and Windows.

Thank you for your help!

@pedrocimp pedrocimp added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Sep 13, 2024
@aaythapa
Copy link

Looks like a SAM CLI issue, transferring over to them

@aaythapa aaythapa transferred this issue from aws/serverless-application-model Sep 13, 2024
@sidhujus
Copy link
Contributor

Hi thanks for raising this issue! Could you provide the output generated by sam cli when running the commands with the --debug flag?

@sidhujus sidhujus added area/docker platform/windows blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Sep 19, 2024
@pedrocimp
Copy link
Author

pedrocimp commented Sep 30, 2024

Command used:

sam local start-api --warm-containers LAZY --host 0.0.0.0 -p 13000 --docker-network my_network --container-host 127.0.0.1 --template template.yml --env-vars tests/resources/environment.json --log-file logs.txt --invoke-image myprivaterepo/public.ecr.aws/lambda/nodejs:20

SAM CLI Output:

2024-09-30 17:03:27,163 | Found one Lambda function with name 'myFunction'
2024-09-30 17:03:27,165 | Invoking index.handler (nodejs20.x)
2024-09-30 17:03:27,167 | Loading AWS credentials from session with profile 'None'
2024-09-30 17:03:27,229 | Resolving code path. Cwd=C:\my\project\path, CodeUri=C:\my\project\path\dist\lambdas\myFunction
2024-09-30 17:03:27,232 | Resolved absolute path to code is C:\my\project\path\dist\lambdas\myFunction
2024-09-30 17:03:27,233 | Resolving code path. Cwd=C:\my\project\path, CodeUri=C:\my\project\path\dist\lambdas\myFunction
2024-09-30 17:03:27,236 | Resolved real code path to C:\my\project\path\dist\lambdas\myFunction
2024-09-30 17:03:27,242 | watch resource C:\my\project\path\dist\lambdas\myFunction
2024-09-30 17:03:27,244 | Create Observer for resource C:\my\project\path\dist\lambdas\myFunction with recursive True
2024-09-30 17:03:27,248 | watch resource C:\my\project\path\dist\lambdas\myFunction's parent C:\my\project\path\dist\lambdas
2024-09-30 17:03:27,250 | Create Observer for resource C:\my\project\path\dist\lambdas with recursive False

2024-09-30 17:03:43,164 | Code C:\my\project\path\dist\lambdas\myFunction is not a zip/jar file
2024-09-30 17:03:44,214 | Local image is up-to-date
Building image.............................
2024-09-30 17:04:53,053 | Checking free port on 127.0.0.1:6502
2024-09-30 17:04:53,072 | Using local image: samcli/lambda-nodejs:20-x86_64-56f48dd3e28f986d7e755f073.

2024-09-30 17:04:53,075 | Mounting C:\my\project\path\dist\lambdas\myFunction as /var/task:ro,delegated, inside runtime container
2024-09-30 17:04:56,623 | Starting a timer for 300 seconds for function 'myFunction'
2024-09-30 17:04:56,627 | Getting lock for the key 127.0.0.1-6502
2024-09-30 17:04:56,629 | Waiting to retrieve the lock (127.0.0.1-6502) to start invocation
2024-09-30 17:04:58,093 | Unable to find Click Context for getting session_id.
2024-09-30 17:04:58 127.0.0.1 - - [30/Sep/2024 17:04:58] "GET /myFunction/{id} HTTP/1.1" 500 -

Inside the container:
bash-5.2# ls /var/task => empty

@pedrocimp
Copy link
Author

I don't use sam build for building my project and generating the /dist folder, my project is built with npx tsc --project tsconfig.build.json .
Maybe there is a link with my issue ?
Thanks :)

@pedrocimp
Copy link
Author

@aaythapa @sidhujus any news ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/docker blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. platform/windows
Projects
None yet
Development

No branches or pull requests

3 participants