The Problem
I am currently working in some AWS Lambda Functions and testing it locally. Usually, the command sam build
takes a lot to finish, so I searched a way to optimize it and found the sam build --cached --user-container
.
When running it in the terminal, it works perfectly fine, creating the following folder structure:
2021-12-22 18:56:07 [INFO]: Preparing to debug locally: Lambda ""
2021-12-22 18:56:07 [INFO]: Building SAM application...
2021-12-22 18:56:08 [INFO]: Running command: (not started) [/usr/local/bin/sam build --build-dir /tmp/aws-toolkit-vscode/vsctkgHZxzf/output --template ./template.yaml --use-container --manifest /tmp/aws-toolkit-vscode/vsctkgHZxzf/debug-requirements.txt -u --cached]
2021-12-22 18:56:09 [INFO]: Starting Build use cache
2021-12-22 18:56:09 [INFO]: Starting Build inside a container
2021-12-22 18:56:10 [INFO]: Traceback (most recent call last):
File "/usr/local/Cellar/python@3.8/3.8.12_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pathlib.py", line 1288, in mkdir
self._accessor.mkdir(self, mode)
FileNotFoundError: [Errno 2] No such file or directory: '.aws-sam/cache'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/bin/sam", line 8, in <module>
sys.exit(cli())
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/click/decorators.py", line 73, in new_func
return ctx.invoke(f, obj, *args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/samcli/lib/telemetry/metric.py", line 157, in wrapped
raise exception # pylint: disable=raising-bad-type
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/samcli/lib/telemetry/metric.py", line 122, in wrapped
return_value = func(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/samcli/lib/utils/version_checker.py", line 41, in wrapped
actual_result = func(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/samcli/cli/main.py", line 87, in wrapper
return func(*args, **kwargs)
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/samcli/commands/build/command.py", line 174, in cli
do_cli(
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/samcli/commands/build/command.py", line 231, in do_cli
with BuildContext(
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/samcli/commands/build/build_context.py", line 105, in __enter__
self.set_up()
File "/usr/local/Cellar/aws-sam-cli/1.36.0/libexec/lib/python3.8/site-packages/samcli/commands/build/build_context.py", line 138, in set_up
cache_path.mkdir(mode=BUILD_DIR_PERMISSIONS, parents=True, exist_ok=True)
File "/usr/local/Cellar/python@3.8/3.8.12_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pathlib.py", line 1292, in mkdir
self.parent.mkdir(parents=True, exist_ok=True)
File "/usr/local/Cellar/python@3.8/3.8.12_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/pathlib.py", line 1288, in mkdir
self._accessor.mkdir(self, mode)
OSError: [Errno 30] Read-only file system: '.aws-sam'
2021-12-22 18:56:10 [WARN]: "sam build" failed
However, when I try to debug by using the launch.json
in VSCode, it gives me this error:
launch.json
{
"configurations": [
{
"type": "aws-sam",
"request": "direct-invoke",
"name": "Invoke Lambda: FunctionX",
"justMyCode": false,
"invokeTarget": {
"target": "template",
"templatePath": "template.yaml",
"logicalId": "FunctionX",
},
"sam": {
"containerBuild": true,
"buildArguments": [
"-u",
"--cached",
]
},
"lambda": {
"timeoutSec": 9999
}
}
]
}
So as you can see, the build fails when I try to debug it.
What I Tried to Do
My first attempt was to manually input the cache directory adding the --cache-dir
parameter:
launch.json
{
"configurations": [
{
"type": "aws-sam",
"request": "direct-invoke",
"name": "Invoke Lambda: FunctionX",
"justMyCode": false,
"invokeTarget": {
"target": "template",
"templatePath": "template.yaml",
"logicalId": "FunctionX",
},
"sam": {
"containerBuild": true,
"buildArguments": [
"-u",
"--cached",
"--cache-dir",
"${workspaceFolder}/.aws-sam/cache"
]
},
"lambda": {
"timeoutSec": 9999
}
}
]
}
However, the same error keep happening. Then, I created a samconfig.toml
file and configured by default the sam build
command as sam build -u --cached
. Still no effect.
In this context, I looked over GitHub Issues and Stack Overflow questions, but no one has ever had this kind of problem. It seems to me that no one tried to use the cache configuration with the launch.json, only by terminal running.
Could someone give me a hint if this is even possible to do, please?
Thanks in advance to everybody that reads and tries to help me!