I am encountering an issue while trying to deploy my Serverless framework application using the serverless-python-requirements plugin with the following configuration:
custom:
pythonRequirements:
dockerizePip: true
When I run the sls deploy command, I receive the following error:
Serverless: Running docker run --rm -v /home/myuser/.cache/serverless-python-requirements/679f0c7a123e5d80212530da94d545f92ad890ecf43e8288e2b6bc243b4e138b_slspyc:/var/task:z lambci/lambda:build-python3.8 /bin/sh -c 'python3.8 -m pip install -t /var/task/ -r /var/task/requirements.txt && chown -R 0:0 /var/task && find /var/task -name *.so -exec strip {} ;'
Serverless: Stdout:
Serverless: Stderr: ERROR: Could not open requirements file: [Errno 2] No such file or directory: '/var/task/requirements.txt'
It seems like the requirements.txt file is not being found in the /var/task/ directory within the Docker container. I have verified that the requirements.txt file is present in my project directory.
It seems like the requirements.txt file is not being found in the /var/task/
directory within the Docker container.
Has anyone faced this issue before, and how can I resolve it?
The error message indicates that the requirements.txt
file is not being found in the Docker container at the expected location /var/task/
. This is often due to permission issues with the default cache location.
Here are some steps to resolve this issue:
-
Default Cache Directory: The serverless-python-requirements plugin writes dependencies to
~/.cache/serverless-python-requirements/...
before mounting them in the Docker container with -v. If the user running the sls deploy command does not have the necessary permissions to access this cache directory, the/var/task
directory in the container will be empty, causing therequirements.txt
file to be missing. Note that Docker will not trigger an error if the volume source does not exist or lacks permissions. -
Change Cache Directory Permissions: Ensure that the user running the sls deploy command has the correct permissions to access the cache directory. See AWS Lambda troubleshooting documentation: https://docs.aws.amazon.com/lambda/latest/dg/troubleshooting-deployment.html
-
Specify Custom Cache Location: If you cannot change the permissions, you can specify a different cache location where you have the necessary permissions using the cacheLocation option in your
serverless.yml
:
custom:
pythonRequirements:
dockerizePip: true
cacheLocation: /path/to/mycacheLocation
- Disable Global Cache: Alternatively, you can disable the global cache using the useStaticCache option. This will make Docker bind directly to
path/to/serverless/project/.serverless/requirements
:
custom:
pythonRequirements:
dockerizePip: true
useStaticCache: false
- CI/CD Pipeline Issues with Dind: If you are running this in a CI/CD pipeline, there might be issues related to Docker-in-Docker (DinD). For more information on resolving these issues, refer to this GitHub issue.
I hope it can help !