AWS SAM CLI 抛出错误:构建 docker 映像时出错

Mat*_*lde 9 python docker aws-lambda aws-sam-cli serverless

我正在尝试在我的 M1 Mac 上使用 SAM CLI。

\n

我按照这些文档中概述的步骤进行操作:

\n
sam init\ncd sam-app\nsam build\nsam deploy --guided\n
Run Code Online (Sandbox Code Playgroud)\n

我没有修改代码或 yaml 文件。\n我可以按预期启动本地 Lambda 函数:

\n
\xe2\x9e\x9c  sam-app sam local start-api\nMounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET]\nYou 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. If you used sam build before running local commands, you will need to re-run sam build for the changes to be picked up. You only need to restart SAM CLI if you update your AWS SAM template\n2023-01-23 17:54:06  * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)\n
Run Code Online (Sandbox Code Playgroud)\n

但一旦我达到终点:

\n
curl http://localhost:3000/hello\n
Run Code Online (Sandbox Code Playgroud)\n

Lambda RIE 开始抛出错误并返回 502。

\n
Invoking app.lambda_handler (python3.9)\nImage was not found.\nRemoving rapid images for repo public.ecr.aws/sam/emulation-python3.9\nBuilding image...................\nFailed to build Docker Image\nNoneType: None\nException on /hello [GET]\nTraceback (most recent call last):\n  File "/opt/homebrew/Cellar/aws-sam-cli/1.70.0/libexec/lib/python3.11/site-packages/flask/app.py", line 2073, in wsgi_app\n    response = self.full_dispatch_request()\n               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/opt/homebrew/Cellar/aws-sam-cli/1.70.0/libexec/lib/python3.11/site-packages/flask/app.py", line 1518, in full_dispatch_request\n    rv = self.handle_user_exception(e)\n         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/opt/homebrew/Cellar/aws-sam-cli/1.70.0/libexec/lib/python3.11/site-packages/flask/app.py", line 1516, in full_dispatch_request\n    rv = self.dispatch_request()\n         ^^^^^^^^^^^^^^^^^^^^^^^\n  File "/opt/homebrew/Cellar/aws-sam-cli/1.70.0/libexec/lib/python3.11/site-packages/flask/app.py", line 1502, in dispatch_request\n    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/opt/homebrew/Cellar/aws-sam-cli/1.70.0/libexec/lib/python3.11/site-packages/samcli/local/apigw/local_apigw_service.py", line 361, in _request_handler\n    self.lambda_runner.invoke(route.function_name, event, stdout=stdout_stream_writer, stderr=self.stderr)\n  File "/opt/homebrew/Cellar/aws-sam-cli/1.70.0/libexec/lib/python3.11/site-packages/samcli/commands/local/lib/local_lambda.py", line 137, in invoke\n    self.local_runtime.invoke(\n  File "/opt/homebrew/Cellar/aws-sam-cli/1.70.0/libexec/lib/python3.11/site-packages/samcli/lib/telemetry/metric.py", line 315, in wrapped_func\n    return_value = func(*args, **kwargs)\n                   ^^^^^^^^^^^^^^^^^^^^^\n  File "/opt/homebrew/Cellar/aws-sam-cli/1.70.0/libexec/lib/python3.11/site-packages/samcli/local/lambdafn/runtime.py", line 177, in invoke\n    container = self.create(function_config, debug_context, container_host, container_host_interface)\n                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/opt/homebrew/Cellar/aws-sam-cli/1.70.0/libexec/lib/python3.11/site-packages/samcli/local/lambdafn/runtime.py", line 73, in create\n    container = LambdaContainer(\n                ^^^^^^^^^^^^^^^^\n  File "/opt/homebrew/Cellar/aws-sam-cli/1.70.0/libexec/lib/python3.11/site-packages/samcli/local/docker/lambda_container.py", line 93, in __init__\n    image = LambdaContainer._get_image(\n            ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/opt/homebrew/Cellar/aws-sam-cli/1.70.0/libexec/lib/python3.11/site-packages/samcli/local/docker/lambda_container.py", line 236, in _get_image\n    return lambda_image.build(runtime, packagetype, image, layers, architecture, function_name=function_name)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n  File "/opt/homebrew/Cellar/aws-sam-cli/1.70.0/libexec/lib/python3.11/site-packages/samcli/local/docker/lambda_image.py", line 164, in build\n    self._build_image(\n  File "/opt/homebrew/Cellar/aws-sam-cli/1.70.0/libexec/lib/python3.11/site-packages/samcli/local/docker/lambda_image.py", line 279, in _build_image\n    raise ImageBuildException("Error building docker image: {}".format(log["error"]))\nsamcli.commands.local.cli_common.user_exceptions.ImageBuildException: Error building docker image: The command \'/bin/sh -c mv /var/rapid/aws-lambda-rie-x86_64 /var/rapid/aws-lambda-rie && chmod +x /var/rapid/aws-lambda-rie\' returned a non-zero code: 1\n
Run Code Online (Sandbox Code Playgroud)\n

我发现这个Github 问题,有人建议执行以下操作:

\n
 docker run --rm --privileged multiarch/qemu-user-static --reset -p yes\n
Run Code Online (Sandbox Code Playgroud)\n

但它没有产生任何结果。

\n

有谁知道我做错了什么,或者如何解决这个问题以便 docker 容器可以正确构建?谢谢。

\n

lou*_*uie 8

在 template.yaml 中,将以下行更改为

  Architectures:
    - x86_64
Run Code Online (Sandbox Code Playgroud)

  Architectures:
    - arm64
Run Code Online (Sandbox Code Playgroud)

这样做的原因是sam init默认为 x86_64 架构。由于您拥有 M1 Mac,因此使用 arm64 架构的 Docker 映像性能会更好。您也可以通过查看提到的错误消息来判断情况是否如此aws-lambda-rie-x86_64。对于 M1 Mac,aws-lambda-rie-arm64一旦您更改上面的这些行,然后重新运行 sam 命令,您将需要它会找到它。

或者,如果您想从头开始,您可以传递参数-a来指定架构,如下所示:

sam init -a arm64

这也能解决你的问题。

请注意,这会对使用 Graviton 处理器将 Lambda 函数部署到 AWS 中的 arm64 产生副作用。如果您不想这样做,您可以在https://github.com/aws/aws-sam-cli/issues/3891中尝试其他解决方法