2

问题

我目前正在使用一些AWS Lambda 函数并在本地对其进行测试。通常,该命令sam build需要很长时间才能完成,所以我搜索了一种优化它的方法并找到sam build --cached --user-container.

在终端中运行它时,它工作得非常好,创建了以下文件夹结构:

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

缓存结构

但是,当我尝试使用launch.jsonVSCode 进行调试时,它给了我这个错误:

启动.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
        }
    }
]

}

如您所见,当我尝试调试它时构建失败。

我试图做什么

我的第一次尝试是手动输入缓存目录添加--cache-dir参数:

启动.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
        }
    }
]

}

但是,同样的错误不断发生。然后,我创建了一个samconfig.toml文件并默认将sam build命令配置为sam build -u --cached. 还是没有效果。

在这种情况下,我查看了 GitHub Issues 和 Stack Overflow 问题,但从来没有人遇到过这种问题。在我看来,没有人试图将缓存配置与launch.json一起使用,仅通过终端运行。

如果这甚至可以做到,有人可以给我一个提示吗?

提前感谢所有阅读并试图帮助我的人!

4

0 回答 0