【问题标题】:How to invoke AWS SAM locally using remote docker (as opposed to docker desktop)?如何使用远程 docker(而不是 docker 桌面)在本地调用 AWS SAM?
【发布时间】: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


    【解决方案1】:

    好的,我现在已经意识到我哪里出错了。 此时在 SAM 日志中:

    Mounting C:\Develop\AWS\sam-app\.aws-sam\build\HelloWorldFunction as /var/task:ro,delegated inside runtime container

    AWS SAM 正在尝试将 Docker 主机上的 C:\Develop\AWS\... 目录绑定安装到 Docker 容器中的 /var/task

    我的错误是认为它正在将实际目录安装在我的本地开发机器上。

    我登录到 Docker 主机,可以看到目录结构已经创建:/c/Develop/AWS/...。我将 app.js 从本地开发机器转移到 Docker 主机的目录,然后宾果游戏 - 它现在可以工作了。 :-)

    因此,现在 AWS SAM 开发人员指南中对--docker-volume-basedir的描述更有意义:

    AWS SAM 文件所在的基本目录的位置。如果 Docker 在远程机器上运行,则必须在 Docker 机器上挂载 AWS SAM 文件所在的路径,并修改该值以匹配远程机器。

    所以我想我需要创建一个从我的 Windows 开发机器上的应用程序文件夹到 Linux Docker 主机上的文件夹的 SMB 映射,并通过设置 @ 确保 Docker 主机 (Linux) 文件夹用于运行应用程序987654327@相应。

    【讨论】:

    • msreeves 您是否设法使用开始调试 (F5) 在 VSCode 中调试您的函数
    • 抱歉,没试过 - 我被分配到另一个项目。 :-(
    猜你喜欢
    • 2019-10-02
    • 2021-09-28
    • 1970-01-01
    • 2018-05-19
    • 1970-01-01
    • 2021-02-24
    • 2020-11-11
    • 1970-01-01
    • 2018-12-29
    相关资源
    最近更新 更多