【发布时间】:2021-09-24 07:12:08
【问题描述】:
我在 Windows 机器上安装了 AWS SAM。我已按照https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-getting-started-hello-world.html 此处的说明创建了一个测试 Hello World 应用程序。 我在单独的(Linux)VM 上运行 docker 服务器。如何在本地调用 AWS SAM?
我尝试了以下方法:
sam local start-api --container-host-interface 0.0.0.0 --container-host 192.168.28.168
192.168.28.168 是运行 docker 服务器的 Linux VM。 (即与我正在开发的 Windows 机器不同)。
但是,我得到“错误:找不到模块”:
PS C:\Develop\AWS\sam-app> sam local start-api --container-host-interface 0.0.0.0 --container-host 192.168.28.168
Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]
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. You only need to restart SAM CLI if you update your AWS SAM template
2021-09-24 07:50:10 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
Invoking app.lambdaHandler (nodejs14.x)
Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-nodejs14.x:rapid-1.27.2.
Mounting C:\Develop\AWS\sam-app\.aws-sam\build\HelloWorldFunction as /var/task:ro,delegated inside runtime container
START RequestId: bd6b8177-56bb-4464-8ead-8c46809e6c6c Version: $LATEST
2021-09-24T06:50:35.674Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js","stack":["Runtime.ImportModuleError: Error: Cannot find module 'app'","Require stack:","- /var/runtime/UserFunction.js","- /var/runtime/index.js"," at _loadUserApp (/var/runtime/UserFunction.js:100:13)"," at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)"," at Object.<anonymous> (/var/runtime/index.js:43:30)"," at Module._compile (internal/modules/cjs/loader.js:1085:14)"," at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)"," at Module.load (internal/modules/cjs/loader.js:950:32)"," at Function.Module._load (internal/modules/cjs/loader.js:790:14)"," at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)"," at internal/main/run_main_module.js:17:47"]}
time="2021-09-24T06:50:35.691" level=panic msg="ReplyStream not available"
SAM 与容器通信正常,START RequestId:… 行证明了这一点。但是,它找不到要运行的 app.js。
我怀疑这与体积映射有关。
我尝试将--docker-volume-basedir 设置为各种值,但似乎没有任何区别。
此页面https://github.com/thoeni/aws-sam-local#remote-docker 上的“远程 Docker”部分建议“项目目录必须预先安装在运行 Docker 的远程主机上”。但是,当我不使用 docker 桌面时,我该怎么做呢? https://github.com/aws/aws-sam-cli/issues/2837#issuecomment-879655277 这里有一些类似的建议,似乎涉及修改 dockerfile 以挂载卷。但是,我没有 dockerfile - SAM 只是在调用时自动拉取图像。
有什么想法吗?甚至可以使用远程 docker 服务器而不是 docker 桌面在本地调用 AWS Sam?
SAM 安装指南https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install-windows.html 的“步骤 3:安装 Docker(可选)”部分描述了设置共享驱动器:“AWS SAM CLI 要求项目目录或任何父目录列在共享驾驶。”但是,很明显,它期待的是 Docker Desktop,而不是在远程服务器上运行的 docker。
也许没有 Docker Desktop 就无法在本地调用 AWS SAM?
【问题讨论】:
标签: amazon-web-services docker remote-server invoke sam